This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
executable file
·69 lines (55 loc) · 2.32 KB
/
service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python2.7
import base64
import json
import os
import time
import urllib2
from datetime import timedelta, datetime
from httplib import responses
import pytz
from hosted import config, Node
from jsonparser import JsonParser
from log import log
config.restart_on_update()
def local_now():
now = datetime.utcnow()
now = now.replace(tzinfo=pytz.utc)
now = now.astimezone(pytz.timezone('Europe/London'))
now = now.replace(tzinfo=None)
return now
def main():
while 1:
node = Node(os.environ['NODE'])
node.send('/updating:%s' % ".")
parser = JsonParser()
url = config['sold_out_feed_url']
log("Updating sold out list from: " + url)
try:
request = urllib2.Request(url, None, {'User-Agent': 'SoldOutScreen'})
if config['use_auth']:
log("Authenticating with user: " + config['feed_username'])
request.add_header("Authorization", "Basic %s" % base64.b64encode('%s:%s' %
(config['feed_username'],
config['feed_password'])
).replace('\n', ''))
response = urllib2.urlopen(request)
data = response.read()
node.write_json("feed.json", json.loads(data.decode("utf-8")))
sold_out_events = parser.parse("feed.json", config['today_only'])
if sold_out_events is not None:
node.write_file('soldoutevents.txt', "\n".join(sold_out_events))
log("--------")
for line in sold_out_events:
log(line)
log("--------")
node.send('/updating:%s' % " ")
except urllib2.HTTPError as e:
log("Server responded with: " + str(e.code) + " " + responses[e.code])
except urllib2.URLError as e:
log(e.reason)
update_seconds = int(config['update_interval']) * 60
updating_at = local_now() + timedelta(seconds=update_seconds)
log("Updating again in %s seconds at %s" % (update_seconds, updating_at.strftime("%H:%M:%S")))
time.sleep(update_seconds)
if __name__ == "__main__":
main()