-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracks.py
134 lines (122 loc) · 5.58 KB
/
tracks.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import requests
from parse_response import parse_response
BASE_URL = 'https://pythoncanarias.es'
def get_tracks_information() -> object:
url = 'https://pythoncanarias.es/api/v1/events/pydaygc19/tracks'
response = requests.get(url)
data = response.json()
return data
def create_card(speaker_info: object) -> object:
return {
"card": {
"title": "{} {}.\n{}.\n\nComienzo:{}\n\nFin:{}\n\n".format(
speaker_info['speakers'][0]['name'],
speaker_info['speakers'][0]['surname'],
speaker_info['name'],
speaker_info['start'],
speaker_info['end']),
"subtitle": speaker_info['description'],
"imageUri": BASE_URL + speaker_info['speakers'][0]['photo'],
"buttons": [
{
"text": f"{speaker_info['speakers'][0]['name']} {speaker_info['speakers'][0]['surname']}"
}
]
},
'platform': 'TELEGRAM',
}
def create_tracks_cards(tracks_information: list, selected_track: int = None, speaker_specific_hour: str = None) -> object:
if selected_track == 1:
track_one_information = tracks_information['result'][0]['schedule']
track_one = []
for track_info in track_one_information:
print(track_info)
if speaker_specific_hour:
if datetime.datetime.strptime(speaker_specific_hour,'%H:%M') >= datetime.datetime.strptime(track_info['start'],'%H:%M') and datetime.datetime.strptime(speaker_specific_hour, '%H:%M') <= datetime.datetime.strptime(track_info['end'], '%H:%M'):
track_one.append(create_card(track_info))
break
else:
track_one.append(create_card(track_info))
track_cards = track_one
elif selected_track == 2:
track_two_information = tracks_information['result'][1]['schedule']
track_two = []
for track_info in track_two_information:
if speaker_specific_hour:
if datetime.datetime.strptime(speaker_specific_hour,'%H:%M') >= datetime.datetime.strptime(track_info['start'],'%H:%M') and datetime.datetime.strptime(speaker_specific_hour, '%H:%M') <= datetime.datetime.strptime(track_info['end'], '%H:%M'):
track_one.append(create_card(track_info))
break
else:
track_two.append(create_card(track_info))
track_cards = track_two
else:
tracks = []
track_one_information = tracks_information['result'][0]['schedule']
track_two_information = tracks_information['result'][1]['schedule']
tracks.append(
{
"text": {
"text": [
"En el Track 1 {} se impartirán las siguientes ponencias".format(
tracks_information['result'][0]['name']
)
]
},
"platform": "TELEGRAM"
},
)
for track_info in track_one_information:
tracks.append(create_card(track_info))
tracks.append(
{
"text": {
"text": [
"En el Track 2 {} se impartirán las siguientes ponencias".format(
tracks_information['result'][1]['name']
)
]
},
"platform": "TELEGRAM"
},
)
for track_info in track_two_information:
tracks.append(create_card(track_info))
track_cards = tracks
return track_cards
def get_speaker_specific_hour():
return ''.join(str(datetime.datetime.now().hour) + ':' + str(datetime.datetime.now().minute))
def get_next_speaker():
return ''.join(str(datetime.datetime.now().hour + 1 ) + ':' + str(datetime.datetime.now().minute))
def tracks_action(req: object = None) -> object:
"""tracks action
"""
tracks_information = get_tracks_information()
if req['queryResult']['parameters']['number']:
selected_track = int(req['queryResult']['parameters']['number'])
if req['queryResult']['parameters']['now_entities']:
speaker_specific_hour = get_speaker_specific_hour()
tracks_cards = create_tracks_cards(tracks_information, selected_track, speaker_specific_hour)
else:
tracks_cards = create_tracks_cards(tracks_information, selected_track)
if req['queryResult']['parameters']['next_entities']:
speaker_specific_hour = get_next_speaker()
tracks_cards = create_tracks_cards(tracks_information, selected_track, speaker_specific_hour)
else:
if req['queryResult']['parameters']['now_entities']:
speaker_specific_hour = get_speaker_specific_hour()
if req['queryResult']['parameters']['next_entities']:
speaker_specific_hour = get_next_speaker()
tracks_cards = create_tracks_cards(tracks_information, speaker_specific_hour)
else:
tracks_cards = create_tracks_cards(tracks_information)
if req['queryResult']['parameters']['next_entities']:
speaker_specific_hour = get_next_speaker()
tracks_cards = create_tracks_cards(tracks_information, selected_track, speaker_specific_hour)
result = {"fulfillmentMessages": []}
for track_card in tracks_cards:
result['fulfillmentMessages'].append(track_card)
response = parse_response(result)
return response