This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meals.py
executable file
·71 lines (61 loc) · 2.16 KB
/
meals.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from lxml import etree, html
from datetime import datetime
import requests
def main():
now = datetime.now().hour
get_afuav_meal(now)
if now < 15 or now > 21:
meal = 'Almoço'
else:
meal = 'Jantar'
# shows canteens notifications
for m in get_canteen_meal(meal):
os.system(
'notify-send "' + m.attrib['canteen'] + '" "' + format_query_output(m) + '"')
def get_canteen_meal(meal):
f = requests.get(
'http://services.web.ua.pt/sas/ementas?date=day&format=xml', headers={'Connection':'close'})
doc = etree.fromstring(f.content)
# returns only opened canteens
return doc.xpath(
'//menu[@meal="' + meal + '" and @disabled="0"]')
def format_query_output(menu):
view = ''
for i in menu[0]:
if i.text != None:
view += i.attrib['name'] + ': ' + i.text + '\n'
return view
def get_afuav_meal(hour):
f = requests.get('https://www.facebook.com/AFUAv-1411897009022037/', headers={'Connection':'close'})
# parsing to etree from html string
doc = html.fromstring(f.content)
# filter by post
posts = doc.xpath('//div[contains(@class, "userContentWrapper")]')
ignore = ['Boa', 'Bom', 'Hoje', 'Ficamos', 'Ver', '...', '🍽️']
for p in posts:
# converts utc timestamp to datetime object
t = datetime.utcfromtimestamp(int(p.xpath('.//@data-utime')[0]))
view = None
if hour >= t.hour and ((t.hour in [11, 12] and hour < 14) or (t.hour in [18, 19] and hour < 22)):
show = True
else:
show = False
if show:
view = ''
# get all text in the post
meal = p.xpath(
'.//div[@class="text_exposed_root"]//*[self::p or self::span]/text()')
for m in meal:
# only show dishes
if not any(sub in m for sub in ignore) and m != ' ':
view += m.strip() + '\n'
# shows the notification
os.system(
'notify-send "' + 'AFUAv' + '" "' + view + '"')
break
else:
continue
main()