Skip to content

Commit

Permalink
Adapt to changed API (Use main website instead)
Browse files Browse the repository at this point in the history
  • Loading branch information
trollvottel committed Feb 17, 2023
1 parent fb29b72 commit 036314a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
32 changes: 16 additions & 16 deletions wetteronline/WetterOnline/__init__.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import logging
import urllib.parse
import re
import requests

from lxml import html

class WetterOnline:
def __init__(self, location):
self.logger = logging.getLogger(__name__)
self.api = 'http://api.wetteronline.de'
self.location = None
self.weather = None
self.url = self.get_url(location)

def get_url(self, location):
try:
url = '{}/search?name={}'.format(self.api, urllib.parse.quote(location))
url = 'http://api.wetteronline.de/search?name={}'.format(urllib.parse.quote(location.lower()))
rq = self._fetch_data(url, headers={'content-type': 'application/json'})
res = rq.json()
except Exception:
self.logger.exception('Failed searching location %s', location)

for x in res:
if 'match' in x and x['match'] == 'yes':
self.location = res[0]
self.location = res[0]['geoName']

if not self.location:
raise Exception("Location not found!")

return '{}/wetterwidget?gid={}&modeid={}&locationname={}'.format(self.api, self.location['geoID'], 'FC3', self.location['locationName'])
return 'http://www.wetteronline.de/{}/'.format(self.location)

def get(self):
try:
Expand All @@ -54,21 +54,21 @@ def _fetch_data(self, url, headers=None):
def _parse_tree(self, tree):
weather = []
try:
for day in tree.xpath('//div[@class="forecast_day"]'):
for day in range(1, 4):
w = {
'day': day.xpath('.//div[1]/text()')[0],
'date': day.xpath('.//div[2]/text()')[0],
'temp_max': float(day.xpath('.//div[4]/text()')[0].replace('°', '')),
'temp_min': float(day.xpath('.//div[5]/text()')[0].replace('°', '')),
'sunhours': float(day.xpath('.//div[8]/text()')[0].replace('h', '')),
'rain_probability': float(day.xpath('.//div[9]/text()')[0].replace('%', '')),
'src': day.xpath('.//div[@class="weathersymbol"]/img/@src')[0],
'day': tree.xpath(f'//table[@id="daterow"]/tbody/tr/th[{day}]/text()')[-1].strip(),
'date': re.search(r"(\d+\.\d+\.)", tree.xpath(f'//table[@id="daterow"]/tbody/tr/th[{day}]/span/text()')[-1].strip()).group(0),
'temp_max': float(tree.xpath(f'//table[@id="weather"]/tbody/tr[@class="Maximum Temperature"]/td[{day}]/div/span[2]/text()')[-1].replace('°', '')),
'temp_min': float(tree.xpath(f'//table[@id="weather"]/tbody/tr[@class="Minimum Temperature"]/td[{day}]/div/span[2]/text()')[-1].replace('°', '')),
'sunhours': float(re.search(r"(\d+)", tree.xpath(f'//tr[@id="sun_teaser"]/td[{day}]/span[1]/text()')[-1]).group(0)),
'rain_probability': float(re.search(r"(\d+)", tree.xpath(f'//tr[@id="precipitation_teaser"]/td[{day}]/span[1]/text()')[-1]).group(0)),
'src': tree.xpath(f'//tr[@id="wwdaysymbolrow"]/td[{day}]/img/@src')[-1],
'img': 'question',
'title': day.xpath('.//div[@class="weathersymbol"]/img/@title')[0],
'title': tree.xpath(f'//tr[@id="wwdaysymbolrow"]/td[{day}]/@data-tt-args')[-1].split(',')[2].replace('"', ''),
}

if w['day'] == 'heute':
w['temp_now'] = float(tree.xpath('//div[@id="temperature"]/text()')[0].replace('°', ''))
if day == 1:
w['temp_now'] = float(tree.xpath('//div[@id="nowcast-card-temperature"]/div[1]/text()')[-1])

weather.append(w)
except Exception:
Expand Down
5 changes: 3 additions & 2 deletions wetteronline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'wbs1': 'weather.partlysunny',
'mbs1': 'weather.partlysunny_n',
'bdr1': 'weather.rain1',
'bds1': 'weather.rain1',
'wbr1': 'weather.rain2_shower',
'wbr2': 'weather.rain2_shower',
'wbs2': 'weather.rain2_shower',
Expand Down Expand Up @@ -71,14 +72,14 @@ def pre_stage(self):
def update(self, value=None, trigger=None):
try:
wol_data = self.wol.get()
self.logger.info("Successfully fetched WetterOnline data")
self.logger.info("Successfully fetched WetterOnline data for %i days", len(wol_data['weather']))
except Exception:
self.logger.exception('Failed getting WetterOnline data')
return

# Map native icons
for wd in wol_data['weather']:
img = re.search(r"/([^/_]+)_*\.svg$", wd['src'])
img = re.search(r"/([^/_]+)_*\.(svg|png)$", wd['src'])
if img:
img = img.group(1)
if img in icons:
Expand Down

0 comments on commit 036314a

Please sign in to comment.