twitter-scrapper/util.go

34 lines
647 B
Go
Raw Normal View History

2020-02-12 10:45:19 +02:00
package twitterscraper
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
func getHTMLFromJSON(req *http.Request, field string) (*strings.Reader, error) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response status: %s", resp.Status)
}
ajaxJSON := make(map[string]interface{})
err = json.NewDecoder(resp.Body).Decode(&ajaxJSON)
if err != nil {
return nil, err
}
htm, ok := ajaxJSON[field].(string)
if !ok {
2020-02-14 16:21:05 +02:00
return nil, fmt.Errorf("field not found in JSON")
2020-02-12 10:45:19 +02:00
}
return strings.NewReader(htm), nil
}