Skip to content

Commit

Permalink
Abstracted item-name format into a function; fix(ing) issue when 'dis…
Browse files Browse the repository at this point in the history
…play_id' is null. (#304)

* Update TubeUp.py

* 2023-08-14T04:50Z

* 2023-08-14T04:52Z
  • Loading branch information
Windows81 authored Aug 17, 2023
1 parent 0eccb23 commit 34f8db0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
18 changes: 6 additions & 12 deletions tubeup/TubeUp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from internetarchive.config import parse_config_file
from datetime import datetime
from yt_dlp import YoutubeDL
from .utils import (sanitize_identifier, check_is_file_empty,
from .utils import (get_itemname, check_is_file_empty,
EMPTY_ANNOTATION_FILE)
from logging import getLogger
from urllib.parse import urlparse
Expand Down Expand Up @@ -108,23 +108,22 @@ def get_resource_basenames(self, urls,
downloaded_files_basename = set()

def check_if_ia_item_exists(infodict):
itemname = sanitize_identifier('%s-%s' % (infodict['extractor'],
infodict['display_id']))
itemname = get_itemname(infodict)
item = internetarchive.get_item(itemname)
if item.exists and self.verbose:
print("\n:: Item already exists. Not downloading.")
print('Title: %s' % infodict['title'])
print('Video URL: %s\n' % infodict['webpage_url'])
return 1
return 0
return True
return False

def ydl_progress_each(entry):
if not entry:
self.logger.warning('Video "%s" is not available. Skipping.' % url)
return
if ydl.in_download_archive(entry):
return
if check_if_ia_item_exists(entry) == 0:
if not check_if_ia_item_exists(entry):
ydl.extract_info(entry['webpage_url'])
downloaded_files_basename.update(self.create_basenames_from_ydl_info_dict(ydl, entry))
else:
Expand Down Expand Up @@ -325,18 +324,13 @@ def upload_ia(self, videobasename, custom_meta=None):
with open(json_metadata_filepath, 'r', encoding='utf-8') as f:
vid_meta = json.load(f)

itemname = ('%s-%s' % (vid_meta['extractor'],
vid_meta['display_id']))

# Exit if video download did not complete, don't upload .part files to IA
for ext in ['*.part', '*.f303.*', '*.f302.*', '*.ytdl', '*.f251.*', '*.248.*', '*.f247.*', '*.temp']:
if glob.glob(videobasename + ext):
msg = 'Video download incomplete, please re-run or delete video stubs in downloads folder, exiting...'
raise Exception(msg)

# Replace illegal characters within identifer
itemname = sanitize_identifier(itemname)

itemname = get_itemname(vid_meta)
metadata = self.create_archive_org_metadata_from_youtubedl_meta(
vid_meta)

Expand Down
8 changes: 8 additions & 0 deletions tubeup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def sanitize_identifier(identifier, replacement='-'):
return re.sub(r'[^\w-]', replacement, identifier)


def get_itemname(infodict):
# Remove illegal characters in identifier
return sanitize_identifier('%s-%s' % (
infodict.get('extractor'),
infodict.get('display_id', infodict.get('id')),
))


def check_is_file_empty(filepath):
"""
Check whether file is empty or not.
Expand Down

0 comments on commit 34f8db0

Please sign in to comment.