-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmealbot.py
47 lines (32 loc) · 1.48 KB
/
mealbot.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
import urllib2, ssl, os
import xml.etree.ElementTree as ET
from datetime import datetime
from threading import Timer
meal = ['*☀ 조식 ☀*️', '*🕐 중식 🕐*', '*🌙 석식 🌙*']
bot_token = os.environ['TELEGRAM_BOT_TOKEN']
context = ssl._create_unverified_context()
def create_timer():
today = datetime.today()
tomorrow = today.replace(day=today.day+1, hour=6, minute=0, second=0, microsecond=0)
time = tomorrow - today
t = Timer(time.seconds+1, bot)
t.start()
def bot():
parser = ET.XMLParser(encoding="utf-8")
page = ''
for line in urllib2.urlopen('https://ksa.hs.kr/Home/CafeteriaMenu/72', context=context):
if not '<link' in line and not '<meta' in line and not '<input' in line and not 'DOCTYPE' in line and not '<img' in line:
if not '&' in line:
page += line.replace('&', ' ')
else:
page += line
root = ET.fromstring(page, parser=parser)
menu = ''
for i, item in enumerate(root.findall(".//table[@class='table table-bordered meal']//li")):
menu += meal[i].decode('utf-8') + item.text+ '\n'
menu = menu.strip().encode('utf-8')
if menu is not '':
url = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=@ksameals&parse_mode=Markdown&text=' + urllib2.quote(menu)
print urllib2.urlopen(url).readlines()
create_timer()
create_timer()