Skip to content

Commit daccc47

Browse files
authored
fix: headers for reuters requests (#37)
this does not fix reuters web api for japan, just for mobile. refactored the HttpGet function to take headers rather than a useragent string
1 parent 24409c9 commit daccc47

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎news/ansa/json.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ type Item struct {
3131
GUID string `xml:"guid"`
3232
}
3333

34+
var Headers = map[string]string{
35+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
36+
}
37+
3438
func (a *ANSA) getArticles(url string, topic news.Topic) ([]news.Article, error) {
3539
// Fetch RSS XML
36-
data, err := news.HttpGet(url, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
40+
data, err := news.HttpGet(url, Headers)
3741
if err != nil {
3842
return nil, err
3943
}
@@ -93,7 +97,7 @@ func (a *ANSA) getFullArticle(articleURL string) (content string, location *news
9397
return "", nil, nil
9498
}
9599

96-
data, err := news.HttpGet(articleURL, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
100+
data, err := news.HttpGet(articleURL, Headers)
97101
if err != nil {
98102
log.Printf("Failed to fetch article content from %s: %v", articleURL, err)
99103
return "", nil, nil
@@ -200,7 +204,7 @@ func (a *ANSA) extractThumbnail(html string) *news.Thumbnail {
200204
return nil
201205
}
202206

203-
imageData, err := news.HttpGet(imageURL, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
207+
imageData, err := news.HttpGet(imageURL, Headers)
204208
if err != nil || len(imageData) == 0 {
205209
return nil
206210
}

‎news/reuters/json.go‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ import (
1010
"strings"
1111
)
1212

13+
var Headers = map[string]string{
14+
"content-type": "application/json",
15+
"user-agent": "ReutersNews/7.48.0.1790290736 Mozilla/5.0 (Linux; Android 14; SM-A135F Build/UP1A.231005.007; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/153.0.8010.36 Mobile Safari/537.36",
16+
"accept": "application/json",
17+
"x-datadog-tracked-by": "react-native",
18+
}
19+
1320
func (r *Reuters) getMobileArticles(url string, topic news.Topic) ([]news.Article, error) {
14-
data, err := news.HttpGet(fmt.Sprintf("https://www.reuters.com/mobile/v1%s?outputType=json",url), "ReutersNews/7.6.0 iPad8,6 iPadOS/18.1 CFNetwork/1.0 Darwin/24.1.0")
21+
data, err := news.HttpGet(fmt.Sprintf("https://www.reuters.com/mobile/v1%s?outputType=json", url), Headers)
1522
if err != nil {
1623
return nil, err
1724
}
@@ -53,7 +60,7 @@ func (r *Reuters) getMobileArticles(url string, topic news.Topic) ([]news.Articl
5360
// The mobile API is much easier to parse.
5461
articlePath := story.URL
5562
articleURL := fmt.Sprintf("https://www.reuters.com/mobile/v1%s", articlePath)
56-
articleData, err := news.HttpGet(articleURL, "ReutersNews/7.6.0 iPad8,6 iPadOS/18.1 CFNetwork/1.0 Darwin/24.1.0")
63+
articleData, err := news.HttpGet(articleURL, Headers)
5764
if err != nil {
5865
return nil, err
5966
}
@@ -93,7 +100,7 @@ func (r *Reuters) getMobileArticles(url string, topic news.Topic) ([]news.Articl
93100
}
94101

95102
func (r *Reuters) getWebArticles(url string, topic news.Topic) ([]news.Article, error) {
96-
data, err := news.HttpGet(fmt.Sprintf("https://jp.reuters.com/pf/api/v3/content/fetch/articles-by-section-alias-or-id-v1?query={\"fetch_type\":\"collection_or_section\",\"orderby\":\"last_updated_date:desc\",\"section_id\":\"%s\",\"website\":\"reuters-japan\"}", url))
103+
data, err := news.HttpGet(fmt.Sprintf("https://www.reuters.com/pf/api/v3/content/fetch/articles-by-section-alias-or-id-v1?query={\"fetch_type\":\"collection_or_section\",\"orderby\":\"last_updated_date:desc\",\"section_id\":\"%s\",\"website\":\"reuters-japan\"}", url))
97104
if err != nil {
98105
return nil, err
99106
}
@@ -116,7 +123,7 @@ func (r *Reuters) getWebArticles(url string, topic news.Topic) ([]news.Article,
116123
r.oldArticleTitles = append(r.oldArticleTitles, title)
117124

118125
articlePath := story.CanonicalURL
119-
articleURL := fmt.Sprintf("https://jp.reuters.com/pf/api/v3/content/fetch/article-by-id-or-url-v1?query={\"website_url\":\"%s\",\"website\":\"reuters-japan\"}", articlePath)
126+
articleURL := fmt.Sprintf("https://www.reuters.com/pf/api/v3/content/fetch/article-by-id-or-url-v1?query={\"website_url\":\"%s\",\"website\":\"reuters-japan\"}", articlePath)
120127
articleData, err := news.HttpGet(articleURL)
121128
if err != nil {
122129
return nil, err
@@ -206,7 +213,7 @@ func getThumbnail(thumbnail ReutersThumbnail) (*news.Thumbnail, error) {
206213
return nil, nil
207214
}
208215

209-
data, err := news.HttpGet(thumbnail.URL, "ReutersNews/7.6.0 iPad8,6 iPadOS/18.1 CFNetwork/1.0 Darwin/24.1.0")
216+
data, err := news.HttpGet(thumbnail.URL, Headers)
210217
if err != nil {
211218
return nil, err
212219
}

‎news/utils.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ import (
2323
_ "image/png"
2424
)
2525

26-
func HttpGet(url string, userAgent ...string) ([]byte, error) {
26+
func HttpGet(url string, headers ...map[string]string) ([]byte, error) {
2727
client := &http.Client{}
2828
req, err := http.NewRequest("GET", url, nil)
2929
if err != nil {
3030
return nil, err
3131
}
3232
req.Header.Set("User-Agent", "WiiLink News Channel File Generator")
3333

34-
if len(userAgent) > 0 && userAgent[0] != "" {
35-
req.Header.Set("User-Agent", userAgent[0])
34+
if len(headers) > 0 {
35+
for k, v := range headers[0] {
36+
req.Header.Set(k, v)
37+
}
3638
}
3739

3840
var resp *http.Response

0 commit comments

Comments
 (0)