-
Notifications
You must be signed in to change notification settings - Fork 5
/
landspitali.py
55 lines (48 loc) · 1.65 KB
/
landspitali.py
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
# -*- coding: utf-8 -*-
import requests
import lxml.html
import time
import datetime
import tweepy
import random
import string
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_SECRET = ''
chars = string.letters + string.digits
randomstring = ''.join([random.choice(chars) for i in xrange(4)]) # a random string for url appending to avoid cache
def landspitali_tweet():
url = 'http://landspitali.is?x=' + randomstring
html = requests.get(url).text
root = lxml.html.fromstring(html)
space = ' '
strings = root.xpath('//div[@class="activityNumbers activityNumbersNew"]')
messages = []
for s in strings:
record = {}
for d in s[1:]:
record['ward'] = d.attrib['class']
record['time'] = d[0].text
record['number'] = d[1].text
record['tail'] = d[2].text
record['date_time'] = time.strftime("%a %b %e %T %z %Y", time.gmtime())
msg = record['time'].replace('...', ':') + space + record['number'] + space + record['tail']
messages.append(msg)
if not messages:
print "Could not find any relevant information from %s" % url
return False
tweet = random.choice(messages)
try:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(tweet)
return True
except Exception, e:
print 'Failed to send tweet: %s' % tweet, e
return False
#landspitali_tweet()
#import cloud
#cloud.cron.register(landspitali_tweet, 'landspitali', '*/60 * * * *',_type = 's1')
#landspitali_tweet()