-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (149 loc) · 5.73 KB
/
main.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
# -*- coding: utf-8 -*-
from flask import Flask
from telegram import Bot
import requests
import re
from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(60)
from google.appengine.ext import ndb
class NbdConfiguration(ndb.Model):
temperature_apikey = ndb.StringProperty()
telegram_apikey = ndb.StringProperty()
ifttt_key = ndb.StringProperty()
@classmethod
def get(cls):
single = cls.query().get()
if single:
return single
else:
None
class NbdGoulashBotStore(ndb.Model):
last_update_id = ndb.IntegerProperty()
users = ndb.JsonProperty()
def read_last_update_id(self):
return self.last_update_id
def save_last_update_id(self, update_id):
self.last_update_id = update_id
self.put()
def read_users(self):
return self.users
def add_user(self, user_id, chat_id):
self.users[user_id] = chat_id
self.put()
def remove_user(self, user_id, chat_id):
del self.users[user_id]
self.put()
@classmethod
def get(cls):
single = cls.query().get()
if single:
return single
else:
return NbdGoulashBotStore(last_update_id=0, users={})
class NbdGoulashFound(ndb.Model):
found = ndb.BooleanProperty()
def read_found(self):
return self.found
def save_found(self, found):
self.found = found
self.put()
@classmethod
def get(cls):
single = cls.query().get()
if single:
return single
else:
return NbdGoulashFound(found=False)
class GoulashBot:
def __init__(self):
self.configuration = NbdConfiguration.get()
self.store = NbdGoulashBotStore.get()
self.last_update_id = 0
self.users = {}
self.bot = Bot(self.configuration.telegram_apikey)
self.goulash_found = NbdGoulashFound.get()
for update in self.bot.getUpdates():
self.process_message(update)
def temperature(self):
response = requests.get("http://api.openweathermap.org/data/2.5/weather?id=3435910&units=metric&APPID=%s" % self.configuration.temperature_apikey).json()
temps = response['main']
return temps['temp']
def load_data(self):
self.last_update_id = self.store.read_last_update_id()
self.users = self.store.read_users()
def add_user(self, user, chat_id):
if user not in self.users.keys():
self.store.add_user(user, chat_id)
self.users[user] = chat_id
self.bot.sendMessage(chat_id=chat_id, text=("Registrado"))
else:
self.bot.sendMessage(chat_id=chat_id, text=("Ya estabas registrado"))
def remove_user(self, user, chat_id):
if user not in self.users.keys():
self.bot.sendMessage(chat_id=chat_id, text=("No estabas registrado"))
else:
self.store.remove_user(user, chat_id)
del self.users[user]
self.bot.sendMessage(chat_id=chat_id, text=("Ya no recibirás notificaciones"))
def unknown_command(self, user, chat_id):
self.bot.sendMessage(chat_id=chat_id, text='unknown command')
def process_message(self, update):
chat_id = update.message.chat_id
message = update.message.text
username = str(update.message.from_user.id)
if message:
if message.startswith('/subscribe'):
self.add_user(username, chat_id)
elif message.startswith('/unsubscribe'):
self.remove_user(username, chat_id)
else:
self.unknown_command(username, chat_id)
self.last_update_id = update.update_id + 1
self.store.save_last_update_id(self.last_update_id)
# Correr periodicamente
def check_for_messages(self):
for update in self.bot.getUpdates(offset=self.last_update_id):
self.process_message(update)
def goulash(self):
response = requests.get('http://latropilla.platosdeldia.com/modules.php?name=PDD&func=nick&nick=latropilla')
body = response.text
m = re.search(r'([^<>]*(ulash|spaetzle|speciale)[^<>]*)[^$]*(\$[.0-9]*)', body)
return (m.group(1), m.group(3), self.temperature()) if m else None
# Correr una vez al dia
def reset_goulash_flag(self):
self.goulash_found.save_found(False)
def goulash_alert(self, found):
for user in self.users.keys():
self.bot.sendMessage(chat_id=self.users[user], text=self.build_message(found))
def build_message(self, found):
return "HAY %s (%s) [temp: %sC]!!!!" % found
def ifttt(self, found):
requests.post("http://maker.ifttt.com/trigger/goulash/with/key/%s" % self.configuration.ifttt_key, data={'value1': self.build_message(found)})
# Correr periodicamente
def check_for_goulash(self):
if not NbdGoulashFound.get().read_found():
found = self.goulash()
self.goulash_found.save_found(found is not None)
if found:
self.ifttt(found)
self.goulash_alert(found)
app = Flask(__name__)
app.config['DEBUG'] = True
goulash_bot = GoulashBot()
goulash_bot.load_data()
@app.route('/cron/check_for_messages')
def check_for_messages():
goulash_bot.check_for_messages()
return str(goulash_bot.users)
@app.route('/cron/check_for_goulash')
def check_for_goulash():
goulash_bot.check_for_goulash()
return "Goulash found" if goulash_bot.goulash_found else "Goulash not found"
@app.route('/cron/reset_goulash_flag')
def reset_goulash_flag():
goulash_bot.reset_goulash_flag()
return 'Reseted.'
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, nothing at this URL.', 404