-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.py
64 lines (57 loc) · 2.53 KB
/
schedule.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from parse_response import parse_response
BASE_URL = 'https://pythoncanarias.es'
def get_schedule_information() -> object:
url = 'https://pythoncanarias.es/api/v1/events/pydaygc19/talks'
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_speaker_cards(schedule_information: list, speaker_schedule: list = []) -> list:
speakers = []
if len(speaker_schedule) == 0:
for speaker in schedule_information['result']:
speakers.append(create_card(speaker))
else:
for speaker in schedule_information['result']:
splited_name = speaker['speakers'][0]['name'].split()
splited_surname = speaker['speakers'][0]['surname'].split()
if speaker_schedule[0] in splited_name or \
speaker_schedule[0] in splited_surname or \
speaker['speakers'][0]['name'] + speaker['speakers'][0]['surname'] == speaker_schedule[0]:
speakers.append(create_card(speaker))
return speakers
def schedule_action(req: object = None) -> object:
"""schedule action
"""
schedule_information = get_schedule_information()
if req['queryResult']['parameters']['speaker_entities']:
speaker_schedule = req['queryResult']['parameters']['speaker_entities']
speaker_cards = create_speaker_cards(schedule_information, speaker_schedule)
else:
speaker_cards = create_speaker_cards(schedule_information)
result = {"fulfillmentMessages": []}
for speaker_card in speaker_cards:
result['fulfillmentMessages'].append(speaker_card)
response = parse_response(result)
return response