-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
243 lines (187 loc) · 6.75 KB
/
bot.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import datetime
import logging
import os
import pickle
import time
import pytz
from urllib.request import urlopen
from urllib.parse import urlencode
from bs4 import BeautifulSoup
from enum import Enum
import twitter
from twitter.error import TwitterError
class Line:
def __init__(self, name, emoji):
self.name = name
self.emoji = emoji
class MLStatus:
def __init__(self, message, ok):
self.message = message
self.ok = ok
class MLBot:
TIMEZONE = 'Europe/Lisbon'
STATUS_URL = 'http://app.metrolisboa.pt/status/estado_Linhas.php'
LINES = [
Line('Amarela', '\U0001F34B'),
Line('Vermelha', '\U000FE051'),
Line('Azul', '\U0001F433'),
Line('Verde', '\U0001F34F')
]
STRINGS = {
"LINE": "Linha",
"UP_EMOJI": "\U0001F535",
"DOWN_EMOJI": "\U0001F534"
}
def __init__(self, state_file, twitter_config, telegram_config, pretend=False):
"""
Initializes a bot instance
:param state_file: File where state will be stored
:param twitter_config: Twitter configuration
:param telegram_config: Telegram configuration
"""
self.state_file = state_file
self.pretend = pretend
if not self.pretend:
self.twitter = twitter.Api(**twitter_config)
self.twitter.VerifyCredentials()
self.telegram_config = telegram_config
self.tz = pytz.timezone(self.TIMEZONE)
self.log = logging.getLogger('mlbot')
self.log.debug('Loading state from file')
try:
with open(self.state_file, 'rb') as f:
self.status = pickle.load(f)
except FileNotFoundError:
self.log.warning('File not found, first run?')
self.status = {}
def check(self):
"""
Check the status for changes
"""
status = self.get_status()
# check for changes
for line, current in status.items():
last = self.status.get(line)
if last == None:
self.state_change(line, current)
elif ((current.ok != last.ok)
or (not current.ok and current.message != last.message)):
self.state_change(line, current)
# save state
self.log.debug('Saving state to file')
self.status = status
with open(self.state_file, 'wb') as f:
pickle.dump(self.status, f)
def get_status(self):
"""
Downloads and parses status from the Metro website
:return: MLStatus object
"""
self.log.debug('Downloading HTML')
html = urlopen(self.STATUS_URL)
self.log.debug('Parsing HTML')
soup = BeautifulSoup(html, 'html.parser')
status = {}
for line in self.LINES:
el = soup.select('td.linha_%s li' % line.name.lower())[0]
message = el.text
ok = 'semperturbacao' in el.parent.get('class', [])
status[line.name] = MLStatus(
message=message,
ok=ok)
return status
def state_change(self, line, status):
"""
A state change ocurred.
Builds a message and publishes it to Twitter.
:param line: Metro line
:param status: MLStatus object
"""
self.log.debug('State for line %s changed: %s', line, status.message)
emoji = '\u2705' if status.ok else '\u26A0\uFE0F'
message = status.message[0].upper() + status.message[1:]
# add full stop if there's none
if not message.endswith('.'):
message = message + '.'
self.publish("%s %s %s: %s" % (
emoji,
self.STRINGS["LINE"],
line,
message))
def publish(self, message):
self.log.info('Publishing: %s', message)
self.publish_twitter(message)
self.publish_telegram(message)
def publish_telegram(self, message):
"""
Publishes a message to telegram.
:param message: Message to publish
"""
key = self.telegram_config['api_key']
for dst in self.telegram_config['destination']:
data = urlencode(dict(
chat_id=dst,
parse_mode='HTML',
text=message
)).encode('utf-8')
res = urlopen(f'https://api.telegram.org/bot{key}/sendMessage', data=data)
def publish_twitter(self, message):
"""
Publishes a message to twitter.
:param message: Message to publish
"""
# add a timestamp to avoid duplicates
now = datetime.datetime.now()
now_tz = pytz.utc.localize(now).astimezone(self.tz)
timestamp = now_tz.strftime("[%H:%M]")
# split into tweets
parts = []
words = message.split(" ")
while len(words) > 0:
part = timestamp
while len(words) > 0:
joined = part + " " + words[0]
if len(joined) < 270:
part = joined
words.pop(0)
else:
break
parts.append(part)
for part in parts:
try:
if not self.pretend:
self.twitter.PostUpdate(part)
except TwitterError as e:
error = e.message
if (len(error) > 0 and
error[0].get('code') == 187):
# Duplicate? Add a dot.
self.publish_twitter(message + '.')
else:
raise e
if __name__ == '__main__':
# set up logger
logging.basicConfig(
format="%(asctime)-15s %(levelname)-9s %(message)s")
debug = os.environ.get('BOT_DEBUG', '0') == '1'
pretend = os.environ.get('BOT_PRETEND', '0') == '1'
log = logging.getLogger('mlbot')
log.setLevel(logging.DEBUG if debug else logging.INFO)
try:
state_file = os.environ['BOT_STATE_FILE']
bot = MLBot(
state_file,
twitter_config=dict(
consumer_key=os.environ['TWITTER_CONSUMER_KEY'],
consumer_secret=os.environ['TWITTER_CONSUMER_SECRET'],
access_token_key=os.environ['TWITTER_ACCESS_TOKEN_KEY'],
access_token_secret=os.environ['TWITTER_ACCESS_TOKEN_SECRET']
) if not pretend else {},
telegram_config=dict(
api_key=os.environ['TELEGRAM_KEY'],
destination=os.environ['TELEGRAM_DESTINATION'].split(',')
),
pretend=pretend)
except KeyError as e:
log.critical('Environment variable %s not found.' % e)
bot.check()