-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerci_professeur.py
executable file
·383 lines (311 loc) · 12.8 KB
/
merci_professeur.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
import json
import os
import time
from urllib.request import urlretrieve
from urllib.parse import urlparse
import urllib
import re
import requests
from moviepy.editor import VideoFileClip, concatenate_videoclips
# Waypoint 1: Write a Python Class Episode
class Episode:
def __init__(self, title, page_url, image_url, broadcasting_date ):
"""
@params: title: the title of Episode class (read only).
page_url: URL of the web page dedicated to this Episode
(read only).
image_url: URL of the image (read only).
broadcasting_date: The date when this Episode
has been broadcast (read only).
@return: object Episode (print all parameters).
"""
self.__title = title
self.__page_url = "http://www.tv5monde.com" + page_url
self.__image_url = image_url
self.__broadcasting_date = broadcasting_date
@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
@staticmethod
def from_json(payload):
title = payload['title']
page_url = payload['url']
image_url = payload['image']
broadcasting_date = payload['date']
template_class = Episode(title, page_url, image_url, broadcasting_date)
template_class.duration = payload['duration']
return template_class
# Waypoint 2: Retrieve the Identification of an Episode
@staticmethod
def __parse_episode_id(url):
"""
@params: staticmethod __parse_episode_id representing
the Uniform Resource Locator of the image of an episode
property episode_id is __parse_episode_id (read only)
@return: the indentification of the Episode (a string)
the indentification of the Episode
"""
file_name = os.path.basename(url)
file_name_without_extension, file_extension = os.path.splitext(file_name)
return file_name_without_extension
@property
def episode_id(self):
self.__episode_id = self.__parse_episode_id(self.__image_url)
return self.__episode_id
# TEST WP1
# with open('./merci-professeur.json', 'r') as myfile:
# data = myfile.read()# url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
# episodes = fetch_episodes(url)
# episode = episodes[0]
# # download all videos and save to folder Music
# print(download_episode_video_segments(episode, path='~/Music'))
# payload = json.loads(data)
# payload_0 = payload['episodes'][0]
# episode = Episode.from_json(payload_0)
# print(episode.title)
# print (episode.page_url)
# print (episode.image_url)
# print (episode.broadcasting_date)
# print(episode.duration)
# episode.title = 'sth else'
# print(episode.title)
# TEST WP2
# print (episode.episode_id)
# Waypoint 3: Fetch the List of Episodes
def read_url(
url,
maximum_attempt_count=3,
sleep_duration_between_attempts=10):
"""
@params: except is the sleep time when the program Connection Error.
read_url function use to edit issues such as: network, machine,
application.
argument url (string) that performs the HTTP request to the
specified endpoint.
@return: a list of objects Episode
read_url function returns the data read contained in the HTTP
response.
"""
attempt_count = 0
while attempt_count < maximum_attempt_count:
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0)\
Gecko/20100101 Firefox/46.0'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
raise Exception('Resource not found/not allowed')
except requests.exceptions.ConnectionError:
print("Toi ngu mot chut...")
time.sleep(sleep_duration_between_attempts)
print("Toi thuc day")
attempt_count += 1
# Waypoint 5: when read html_page return json so the function raise
# ValueError of the html_content, converts return to text (string) for wp5
except ValueError:
return response.text
# url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
# print(read_url(url))
# Waypoint 4: Fetch the List of all the Episodes (update function from waypoint3)
def fetch_episodes(url):
"""
@params: fetch_episodes allows to get the list of episodes.
url: receive the url http://
@returns: returns a page of episodes.
"""
# get the number of pages
numPages = read_url(url)["numPages"]
# list to contain objects Episode
list_episodes = []
for i in range(1,numPages+1,1):
# format url for page
final_url_string = url + "?page={}".format(i)
# get json data from url
data = read_url(final_url_string)
# get each episode's json data
for episode in data['episodes']:
# append to list
list_episodes.append(Episode.from_json(episode))
return list_episodes
# TEST WP3, WP4
# url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
# episodes = fetch_episodes(url)
# for episode in episodes:
# print(episode.page_url)
# Waypoint 5: Parse Broadcast Data of an Episode
# Fetch the HTML source page of the episodeP1
def fetch_episode_html_page(episode):
"""
@param: episode calls the function read_url to read data (bytes)
from the specified URL, and converts these data
(encoded in UTF-8) to a string
@returns: the textual HTML content of the episode page
the scheme, the hostname, the path starts with i//hls,
the TS file name starts with segment, the query.
"""
html_content = read_url(episode.page_url)
return html_content
# Parse broadcast information about the episode's video
def parse_broadcast_data_attribute(html_page):
"""
@param: html_page takes an argument html_page, a string corresponding
to the source code of the HTML page of an episode.
@returns: a JSON expression corresponding to the string value of
the attribute data-broadcast.
"""
# String processing using regex
match_string = re.search(r"data-broadcast='([^']*)'", html_page)
broadcast_data = match_string.group(1)
return json.loads(broadcast_data)
# TEST WP5
# url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
# episodes = fetch_episodes(url)
# episode = episodes[0]
# episode_html_page = fetch_episode_html_page(episode)
# print(episode_html_page)
# print(len(episodes))
# print(parse_broadcast_data_attribute(episode_html_page))
# Waypoint 6: Build a URL Pattern of the Video Segments of an Episode
def build_segment_url_pattern(broadcast_data):
"""
@param: broadcast_data representing the broadcast data of an episode
@returns: a string representing a URL pattern that references
the video segments of this episode.
"""
broadcast_url = broadcast_data['files'][0]['url']
# WP10:
# if file format is mp4:
broadcast_format = broadcast_data['files'][0]['format']
if broadcast_format = 'mp4':
segment_url = urlparse(broadcast_url)
return segment_url.geturl()
# if file format is m3u8
head, sep, tail = broadcast_url.partition('csmil/')
segment_url = urlparse(head + sep + 'segment{}_3_av.ts?null=0')
return segment_url.geturl()
# TEST WP6
# url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
# episodes = fetch_episodes(url)
# episode = episodes[0]
# episode_html_page = fetch_episode_html_page(episode)
# broadcast_data = parse_broadcast_data_attribute(episode_html_page)
# print(broadcast_data)
# segment_url_pattern = build_segment_url_pattern(broadcast_data)
# print('\n' + segment_url_pattern + '\n')
# print(segment_url_pattern.format('1'))
# Waypoint 7: Download the Video Segments of an Episodeeeeeeeeeeeeeee
def download_episode_video_segments(episode, path=None):
"""
@param: episode that downloads all the TS video segments of this episode
@returns: the absolute path and file names of these video segments
in the order of the segment indices.
"""
try:
# read the html page of episode
episode_html_page = fetch_episode_html_page(episode)
# get the json data of attribute data-broadcast from html page
broadcast_data = parse_broadcast_data_attribute(episode_html_page)
# build the sample segment pattern to download
segment_pattern = build_segment_url_pattern(broadcast_data)
# check if path is input
if path is not None:
path = os.path.expanduser(path)
# check path exists if not create path
if not os.path.isdir(path):
os.mkdir(path)
# if path is not input set path = current working directory
else:
path = os.getcwd()
# segment index
index = 1
# list contain downloaded segments
downloaded_videos = []
# start the download loop
while True:
# format index of segment pattern to download
segment_url = segment_pattern.format(index)
# create segment file name if format is m3u8
file_name = 'segment_{}_{}.ts'.format(episode.episode_id, index)
# create segment file name if format is mp4
if 'mp4' in segment_pattern:
file_name = 'segment_{}_{}.mp4'.format(episode.episode_id, index)
# create final file name to download
des_path = path + '/' + file_name
# Waypoint 9: Implement a Cache Strategy
# if video segment already downloaded
if os.path.exists(des_path):
print(des_path + ' already downloaded')
# if video segment not downloaded
else:
# download the segment
urlretrieve(segment_url, des_path)
# append to list
downloaded_videos.append(des_path)
# wp10
if 'mp4' in segment_pattern:
return downloaded_videos
# update index value
index += 1
# when there's no more segments to download
except urllib.error.HTTPError:
return downloaded_videos
# TEST WP7
# url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
# episodes = fetch_episodes(url)
# episode = episodes[0]
# print(episode.title)
# print(eposode.episode_id)
# print(episode.page_url)
# segments = download_episode_video_segments(episode, path='~/Desktop')
# download in Home ~/ => Desktop, then after Desktop have /bla so create 1 folder
# print(segments)
# Waypoint 8: Build the Final Video of an Episode
def build_episode_video(episode, segment_file_path_names, path=None):
"""
@param: episode An object Episode.
segment_file_path_names a list of strings corresponding to
absolute path and file names of TS video segments in
the order of their index.
@returns: the absolute path and file name of the episode's video.
"""
# create file name
file_name = episode.episode_id + '.ts'
# wp10:
if 'mp4' in segment_file_path_names[0]:
file_name = episode.episode_id + '.mp4'
# check if path is not input, set path = the directory contain first segment
if path is None:
path = os.path.dirname(segment_file_path_names[0])
# get full input path
path = os.path.expanduser(path)
# if input path is not exists, create path
if not os.path.exists(path):
os.mkdir(path)
# create final file name combine with path
file_name = path + '/' + file_name
# if video already combine
if os.path.exists(file_name):
return file_name
# create the full video
final_clip = concatenate_videoclips([VideoFileClip(segment) for segment \
in segment_file_path_names])
# write content of full video to file
final_clip.write_videofile(file_name, codec = "libx264")
return file_name
# TEST WP8
url = 'http://www.tv5monde.com/emissions/episodes/merci-professeur.json'
episodes = fetch_episodes(url)
episode = episodes[0]
segment_file_path_names = download_episode_video_segments(episode, path='~/Small_videos')
file_name = build_episode_video(episode, segment_file_path_names, path='~/Big_video')
print(file_name)