Weather.com: spoof user agent to allow web request (#745)

Weather.com's API is returning `403 Forbidden` for requests using
urllib's default user agent. This commit uses Firefox's user agent to
get around this limitation.
This commit is contained in:
Erik Johnson 2019-05-23 09:11:18 -05:00 committed by GitHub
parent 2cf37d86d7
commit 0d05a635ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,11 +2,13 @@ import json
import re import re
from datetime import datetime from datetime import datetime
from html.parser import HTMLParser from html.parser import HTMLParser
from urllib.request import urlopen from urllib.request import Request, urlopen
from i3pystatus.core.util import internet, require from i3pystatus.core.util import internet, require
from i3pystatus.weather import WeatherBackend from i3pystatus.weather import WeatherBackend
USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0'
class WeathercomHTMLParser(HTMLParser): class WeathercomHTMLParser(HTMLParser):
''' '''
@ -22,7 +24,8 @@ class WeathercomHTMLParser(HTMLParser):
def get_weather_data(self, url): def get_weather_data(self, url):
self.logger.debug('Making request to %s to retrieve weather data', url) self.logger.debug('Making request to %s to retrieve weather data', url)
self.weather_data = None self.weather_data = None
with urlopen(url) as content: req = Request(url, headers={'User-Agent': USER_AGENT})
with urlopen(req) as content:
try: try:
content_type = dict(content.getheaders())['Content-Type'] content_type = dict(content.getheaders())['Content-Type']
charset = re.search(r'charset=(.*)', content_type).group(1) charset = re.search(r'charset=(.*)', content_type).group(1)