1 from urllib2 import urlopen
2 from hashlib import md5
3 from datetime import datetime
6 from config import config
8 CONFIGDIR = path.expanduser('~/.gotovienna')
9 STATIONFILENAME = path.join(CONFIGDIR, 'stations.db')
10 URLPREFIX = 'http://www.logic.at/people/kelvan/python/gotovienna/downloads/%s'
11 REMOTEFILE = 'stations.db'
12 REMOTEHASH = 'stations.md5'
14 def compare_hash(md5sum, fn):
16 md5sum = md5sum.split()[0]
18 if not path.exists(fn):
22 with open(fn,'rb') as f:
23 for chunk in iter(lambda: f.read(8192), ''):
24 localmd5.update(chunk)
25 return localmd5.hexdigest() == md5sum
27 def check_stations_update():
28 """ Check for new version of stations.db
29 return True if new version available
31 raise exception if unable to check for new version
33 remote_hash = urlopen(URLPREFIX % REMOTEHASH).read()
34 return not compare_hash(remote_hash, STATIONFILENAME)
36 def update_stations():
37 if not path.exists(CONFIGDIR):
39 remote = urlopen(URLPREFIX % REMOTEFILE)
40 with open(STATIONFILENAME, 'wb') as f:
41 f.write(remote.read())
42 config.setLastStationsUpdate(datetime.now().strftime('%c'))