Skip to content

Commit

Permalink
check for existing downloaded media items but with bandcamp_item_id.t…
Browse files Browse the repository at this point in the history
…xt ID mismatch to prevent download loops, resolves #5
  • Loading branch information
meeb committed Sep 21, 2023
1 parent 439a4c2 commit c056f0e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 5 additions & 5 deletions bandcampsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def do_sync(cookies_path, cookies, dir_path, media_format):
bandcamp.verify_authentication()
bandcamp.load_purchases()
for item in bandcamp.purchases:
if local_media.is_locally_downloaded(item.item_id):
log.info(f'Already locally downloaded, skipping: "{item.band_name} / {item.item_title} "'
local_path = local_media.get_path_for_purchase(item)
if local_media.is_locally_downloaded(item, local_path):
log.info(f'Already locally downloaded, skipping: "{item.band_name} / {item.item_title}" '
f'(id:{item.item_id})')
continue
else:
log.info(f'New media item, will download: "{item.band_name} / {item.item_title} "'
log.info(f'New media item, will download: "{item.band_name} / {item.item_title}" '
f'(id:{item.item_id}) in "{media_format}"')
local_path = local_media.get_path_for_purchase(item)
local_path.mkdir(parents=True, exist_ok=True)
try:
initial_download_url = bandcamp.get_download_file_url(item, encoding=media_format)
except BandcampError as e:
log.error(f'Failed to locate download URL for media item "{item.band_name} / {item.item_title} " '
log.error(f'Failed to locate download URL for media item "{item.band_name} / {item.item_title}" '
f'(id:{item.item_id}), unable to download release ({e}), skipping')
continue
download_file_url = bandcamp.check_download_stat(item, initial_download_url)
Expand Down
14 changes: 12 additions & 2 deletions bandcampsync/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, media_dir):
self.media_dir = media_dir
self.media = {}
self.dirs = set()
self.item_names = set()
log.info(f'Local media directory: {self.media_dir}')
self.index()

Expand All @@ -46,6 +47,7 @@ def index(self):
if child3.name == self.ITEM_INDEX_FILENAME:
item_id = self.read_item_id(child3)
self.media[item_id] = child2
self.item_names.add((child2.parent.name, child2.name))
log.info(f'Detected locally downloaded media: {item_id} = {child2}')
return True

Expand All @@ -57,8 +59,16 @@ def read_item_id(self, filepath):
except Exception as e:
raise ValueError(f'Failed to cast item ID from {filepath} "{item_id}" as an int: {e}') from e

def is_locally_downloaded(self, item_id):
return item_id in self.media
def is_locally_downloaded(self, item, local_path):
if item.item_id in self.media:
return True
item_name = (local_path.parent.name, local_path.name)
if item_name in self.item_names:
log.info(f'Detected album at "{local_path}" but with an item ID mismatch '
f'({self.ITEM_INDEX_FILENAME} file does not contain {item.item_id}), '
f'you may want to check this item is correctly downloaded')
return True
return False

def is_dir(self, path):
return path in self.dirs
Expand Down

0 comments on commit c056f0e

Please sign in to comment.