-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
112 lines (97 loc) · 3.44 KB
/
app.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
import os
import datetime
import sass
import re
import yaml
from flask import Flask, render_template, jsonify
from flask import send_from_directory
from cal_setup import get_calendar_service
from html.parser import HTMLParser
from flask_fontawesome import FontAwesome
app = Flask(__name__)
fa = FontAwesome(app)
filesource = "./config.yaml"
sass_map = {"static/scss/style.scss": "static/style.css"}
labelList = ["webinar","url","tags","city"]
events = list()
file = list()
class HTMLFilter(HTMLParser):
text = ""
def handle_data(self, data):
self.text += data
# gen css
def compile_sass_to_css(sass_map):
for source, dest in sass_map.items():
with open(dest, "w") as outfile:
outfile.write(sass.compile(filename=source))
# read yaml file
def read_yaml():
with open(filesource, 'r') as stream:
try:
file = yaml.load(stream, Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
print(exc)
return file
# resplit label after html to text
def split_label(event,label):
event['desc'] = event['desc'].replace(label+":", " "+label+":")
return event
# search label to description
def search_label(event,label,vTags):
parse = (searh for searh in event['desc'].split() if re.match(r"^"+label+":.*", searh))
for i in parse:
if label == "tags":
fullTags = i.replace(label+":", "").split(",")
event[label] = list( d for d in fullTags if d in vTags )
else:
event[label] = i.replace(label+":", "").split(",")
event['desc'] = event['desc'].replace(i , "")
return event
# get event to Google Calendar
def get_gcalendar(idCal):
service = get_calendar_service()
now = datetime.datetime.utcnow().isoformat() + 'Z'
events_result = service.events().list(calendarId= idCal, timeMin=now,maxResults=100, singleEvents=True,orderBy='startTime').execute()
events = events_result.get('items', [])
return events
# format champs for timeline
def parse_data(events,vTags):
for event in events:
if 'start' in event:
start = event['start'].get('dateTime', event['start'].get('date'))
event['datestart'] = datetime.datetime.fromisoformat(start).strftime("%d/%m/%y %H:%M")
if 'end' in event:
end = event['end'].get('dateTime', event['end'].get('date'))
event['dateend'] = datetime.datetime.fromisoformat(end).strftime("%d/%m/%y %H:%M")
if 'description' in event:
# description : to txt
desc = HTMLFilter()
desc.feed(event['description'])
event['desc'] = desc.text
# split, search and clean label
for i in labelList:
event = split_label(event,i)
for i in labelList:
event = search_label(event,i,vTags)
return events
# route
@app.route('/')
def accueil():
compile_sass_to_css(sass_map)
file = read_yaml()
events = get_gcalendar(file['idCalendar'])
events = parse_data(events,file['valideTags'])
return render_template('index.html', events=events, file=file)
@app.errorhandler(404)
def page_not_found(e):
compile_sass_to_css(sass_map)
file = read_yaml()
return render_template('404.html',file=file), 404
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),'favicon.png', mimetype='image/png')
@app.route('/ping')
def ping():
return "pong"
if __name__ == '__main__':
app.run(debug=True)