Skip to content

Commit

Permalink
Choose underground server by age of data
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 30, 2018
1 parent 2ac2f98 commit ff164bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.8.0'
_release = '1617'
_commit = 'e32d74f'
_release = '1618'
_commit = '2ac2f98'
15 changes: 13 additions & 2 deletions src/pywws/service/underground.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from __future__ import absolute_import, unicode_literals

from contextlib import contextmanager
from datetime import timedelta
from datetime import datetime, timedelta
import logging
import os
import sys
Expand All @@ -64,6 +64,7 @@
__docformat__ = "restructuredtext en"
service_name = os.path.splitext(os.path.basename(__file__))[0]
logger = logging.getLogger(__name__)
RTFREQ = timedelta(seconds=48)


class ToService(pywws.service.CatchupDataService):
Expand Down Expand Up @@ -109,8 +110,18 @@ def session(self):
with requests.Session() as session:
yield session

def prepare_data(self, data):
prepared_data = super(ToService, self).prepare_data(data)
# merge timestamp into prepared_data
prepared_data['idx'] = data['idx']
return prepared_data

def upload_data(self, session, prepared_data={}, live=False):
if live:
# extract timestamp from prepared_data
idx = prepared_data['idx']
del prepared_data['idx']
# use "rapid fire" server if data is current
if datetime.utcnow() - idx < RTFREQ:
prepared_data.update({'realtime': '1', 'rtfreq': '48'})
url = 'https://rtupdate.wunderground.com/'
else:
Expand Down

0 comments on commit ff164bd

Please sign in to comment.