36
36
import socket
37
37
38
38
try :
39
- # For Python 2
40
- from ConfigParser import Error
41
- from ConfigParser import MissingSectionHeaderError
42
- from urllib2 import urlopen
43
- from urllib2 import Request
44
- from urllib2 import HTTPError
45
- from urllib2 import URLError
46
- except ImportError :
47
39
# For Python 3.0 and later
48
40
from configparser import Error
49
41
from configparser import MissingSectionHeaderError
50
42
from urllib .request import urlopen
51
43
from urllib .request import Request
52
44
from urllib .error import HTTPError
53
45
from urllib .error import URLError
46
+ from urllib .parse import urlparse
47
+ except ImportError :
48
+ # Fall back to Python 2's naming
49
+ from ConfigParser import Error
50
+ from ConfigParser import MissingSectionHeaderError
51
+ from urllib2 import urlopen
52
+ from urllib2 import Request
53
+ from urllib2 import HTTPError
54
+ from urllib2 import URLError
55
+ from urlparse import urlparse
54
56
55
57
56
- def get_trailer_file_urls (page_url , res , types ):
58
+ def get_trailer_file_urls (page_url , res , types , download_all_urls ):
57
59
"""Get all trailer file URLs from the given movie page in the given
58
60
resolution and having the given trailer types.
59
61
"""
@@ -70,7 +72,9 @@ def get_trailer_file_urls(page_url, res, types):
70
72
file_info = clip ['versions' ]['enus' ]['sizes' ][apple_size ]
71
73
file_url = convert_src_url_to_file_url (file_info ['src' ], res )
72
74
73
- if should_download_file (types , video_type ):
75
+ if (get_url_path (page_url ) in download_all_urls or
76
+ should_download_file (types , video_type )):
77
+
74
78
url_info = {
75
79
'res' : res ,
76
80
'title' : title ,
@@ -210,14 +214,16 @@ def download_trailer_file(url, destdir, filename):
210
214
return
211
215
212
216
213
- def download_trailers_from_page (page_url , dl_list_path , res , destdir , types ):
217
+ def download_trailers_from_page (page_url , settings ):
214
218
"""Takes a page on the Apple Trailers website and downloads the trailer
215
219
for the movie on the page. Example URL:
216
220
http://trailers.apple.com/trailers/lions_gate/thehungergames/"""
217
221
218
222
logging .debug ('Checking for files at %s' , page_url )
219
- trailer_urls = get_trailer_file_urls (page_url , res , types )
220
- downloaded_files = get_downloaded_files (dl_list_path )
223
+ trailer_urls = get_trailer_file_urls (page_url , settings ['resolution' ],
224
+ settings ['video_types' ],
225
+ settings ['download_all_urls' ])
226
+ downloaded_files = get_downloaded_files (settings ['list_file' ])
221
227
222
228
for trailer_url in trailer_urls :
223
229
trailer_file_name = get_trailer_filename (trailer_url ['title' ],
@@ -226,9 +232,9 @@ def download_trailers_from_page(page_url, dl_list_path, res, destdir, types):
226
232
if trailer_file_name not in downloaded_files :
227
233
logging .info ('Downloading %s: %s' , trailer_url ['type' ],
228
234
trailer_file_name )
229
- download_trailer_file (trailer_url ['url' ], destdir ,
235
+ download_trailer_file (trailer_url ['url' ], settings [ 'download_dir' ] ,
230
236
trailer_file_name )
231
- record_downloaded_file (trailer_file_name , dl_list_path )
237
+ record_downloaded_file (trailer_file_name , settings [ 'list_file' ] )
232
238
else :
233
239
logging .debug ('*** File already downloaded, skipping: %s' ,
234
240
trailer_file_name )
@@ -249,6 +255,17 @@ def get_trailer_filename(film_title, video_type, res):
249
255
return trailer_file_name
250
256
251
257
258
+ def get_url_path (url ):
259
+ """Take a full URL and reduce it to just the path, with starting and ending
260
+ whitespace as well as the trailing slash removed, if they exist."""
261
+ url = url .strip ()
262
+ path = urlparse (url ).path
263
+ if path and path [- 1 ] == "/" :
264
+ path = path [:- 1 ]
265
+
266
+ return path
267
+
268
+
252
269
def validate_settings (settings ):
253
270
"""Validate the settings in the given dictionary. If any setting is
254
271
invalid, raises an Error with a user message"""
@@ -317,6 +334,12 @@ def get_config_values(config_path, defaults):
317
334
config_values = config .defaults ()
318
335
break
319
336
337
+ if config_values .get ('download_all_urls' , '' ):
338
+ config_values ['download_all_urls' ] = (
339
+ [get_url_path (s ) for s in config_values ['download_all_urls' ].split (',' )])
340
+ else :
341
+ config_values ['download_all_urls' ] = []
342
+
320
343
if not config_file_found :
321
344
logging .info ('Config file not found. Using default values.' )
322
345
@@ -334,9 +357,9 @@ def get_settings():
334
357
script_dir = os .path .abspath (os .path .dirname (__file__ ))
335
358
defaults = {
336
359
'download_dir' : script_dir ,
360
+ 'output_level' : 'debug' ,
337
361
'resolution' : '720' ,
338
362
'video_types' : 'single_trailer' ,
339
- 'output_level' : 'debug' ,
340
363
}
341
364
342
365
args = get_command_line_arguments ()
@@ -507,13 +530,7 @@ def main():
507
530
# Do the download
508
531
if 'page' in settings :
509
532
# The trailer page URL was passed in on the command line
510
- download_trailers_from_page (
511
- settings ['page' ],
512
- settings ['list_file' ],
513
- settings ['resolution' ],
514
- settings ['download_dir' ],
515
- settings ['video_types' ]
516
- )
533
+ download_trailers_from_page (settings ['page' ], settings )
517
534
518
535
else :
519
536
just_added_url = ('http://trailers.apple.com/trailers/'
@@ -522,13 +539,7 @@ def main():
522
539
523
540
for trailer in newest_trailers :
524
541
url = 'http://trailers.apple.com' + trailer ['location' ]
525
- download_trailers_from_page (
526
- url ,
527
- settings ['list_file' ],
528
- settings ['resolution' ],
529
- settings ['download_dir' ],
530
- settings ['video_types' ]
531
- )
542
+ download_trailers_from_page (url , settings )
532
543
533
544
534
545
if __name__ == '__main__' :
0 commit comments