-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpisode.py
87 lines (70 loc) · 2.28 KB
/
Episode.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
import json
import requests
# Waypoint 1: Write a Python Class Episode
class Episode: # _ co the gan duoc luc test print title = "a"
def __init__(self, title, page_url, image_url, broadcasting_date, duration):
self._title = title
self._page_url = "http://www.tv5monde.com" + page_url
self._image_url = image_url
self._broadcasting_date = broadcasting_date
self._duration = duration
self._episode_id = None
@property
def title(self):
return self._title
@property
def page_url(self):
return self._page_url
@property
def image_url(self):
return self._image_url
@property
def broadcasting_date(self):
return self._broadcasting_date
@property
def duration(self):
return self._duration
@staticmethod
def from_json(payload):
return Episode(payload['title'], payload["url"], payload["image"],\
payload["date"], payload["duration"])
# Waypoint 2: Retrieve the Identification of an Episode
@staticmethod
def __parse_episode_id(url):
return url.split('.')[2].split('/')[3]
@property
def episode_id(self):
self._episode_id = self.__parse_episode_id(self._image_url)
return self._episode_id
with open('./merci-professeur.json', 'r') as myfile:
data = myfile.read()
payload = json.loads(data)
payload_0 = payload['episodes'][0]
a = Episode.from_json(payload_0)
# TEST WP1
# print (a.title)
# print (a.page_url)
# print (a.image_url)
# print (a.broadcasting_date)
# print (a.duration)
# a.title = 'sth else'
# print (a.title)
# TEST WP2
# print (a.episode_id)
# Waypoint 3: Fetch the List of Episodes
def read_url(url, maximum_attempt_count=3,
sleep_duration_between_attempts=10):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0)\
Gecko/20100101 Firefox/46.0'}
r = requests.get(url, headers=headers)
return r.json()
def fetch_episodes(url):
api_data = read_url(url)
list_episodes = []
for episode in api_data['episodes']:
list_episodes.append(Episode.from_json(episode))
return list_episodes
url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
list_episodes = fetch_episodes(url)
for episode in list_episodes:
print(episode.episode_id)