Skip to content

Commit

Permalink
Prevent repeat download in single_trailer mode
Browse files Browse the repository at this point in the history
Previously there was a situation where the script would download
multiple trailers, even in single_trailer mode. If a movie only had a
"Trailer 2" and no "Trailer 1", the script would download "Trailer 2" in
single_trailer mode. Then, if a "Trailer 1" appeared for that movie, the
script would have downloaded that trailer as well.

This commit changes the behavior so that if the script has downloaded
any trailer for a particular movie, it will never download another
trailer in single_trailer mode.
  • Loading branch information
aag committed Jan 18, 2020
1 parent e0780dd commit 38982e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
25 changes: 23 additions & 2 deletions download_trailers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ def record_downloaded_file(filename, dl_list_path):
write_downloaded_files(file_list, dl_list_path)


def file_already_downloaded(file_list, movie_title, video_type, res,
requested_types):
"""Returns true if the file_list contains a file that matches the file
properties."""

if requested_types.lower() == 'single_trailer':
trailer_prefix = '{}.trailer'.format(movie_title.lower())
movie_trailers = [f for f in file_list
if f.lower().startswith(trailer_prefix)]
return bool(movie_trailers)

trailer_file_name = get_trailer_filename(movie_title, video_type, res)
return trailer_file_name in file_list


def download_trailer_file(url, destdir, filename):
"""Accepts a URL to a trailer video file and downloads it
You have to spoof the user agent or the site will deny the request
Expand All @@ -196,7 +211,7 @@ def download_trailer_file(url, destdir, filename):
existing_file_size = os.path.getsize(file_path)

data = None
headers = {'User-Agent': 'Quick_time/7.6.2'}
headers = {}

resume_download = False
if file_exists and (existing_file_size > 0):
Expand Down Expand Up @@ -256,7 +271,13 @@ def download_trailers_from_page(page_url, settings):
trailer_file_name = get_trailer_filename(trailer_url['title'],
trailer_url['type'],
trailer_url['res'])
if trailer_file_name not in downloaded_files:
already_downloaded = (
file_already_downloaded(downloaded_files, trailer_url['title'],
trailer_url['type'], trailer_url['res'],
settings['video_types'])
)

if not already_downloaded:
logging.info('Downloading %s: %s', trailer_url['type'],
trailer_file_name)
download_trailer_file(trailer_url['url'], settings['download_dir'],
Expand Down
15 changes: 15 additions & 0 deletions test/test_download_trailers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ def test_get_download_types_trailers():
assert download_types == expected_types


def test_file_already_downloaded_downloaded():
download_list = [u'Film.Trailer 2.1080p.mov', u'☃.Clip.480p.mov']
assert trailers.file_already_downloaded(download_list, 'Film', 'Trailer 2', '1080', 'all')


def test_file_already_downloaded_single_trailer():
download_list = [u'Film.Trailer 2.1080p.mov', u'☃.Clip.480p.mov']
assert trailers.file_already_downloaded(download_list, 'Film', 'Trailer 1', '1080', 'single_trailer')


def test_file_already_downloaded_single_trailer():
download_list = [u'Film.Clip 2.1080p.mov', u'☃.Clip.480p.mov']
assert not trailers.file_already_downloaded(download_list, 'Film', 'Trailer 1', '1080', 'single_trailer')


def test_get_downloaded_files_missing_file():
assert trailers.get_downloaded_files('/not/a/real/path/to/file.txt') == []

Expand Down

0 comments on commit 38982e3

Please sign in to comment.