Skip to content

Commit

Permalink
fix: don't rely on internetarchive's cli parsing
Browse files Browse the repository at this point in the history
Fixes compatibility with internetarchive>=5.0.3 by using a custom parser for the --metadata parameter instead of relying on the one (formerly) used by internetarchive

Closes #338
  • Loading branch information
DigitalDwagon committed Nov 13, 2024
1 parent a342a6d commit 5a1bd46
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ readme = "README.md"
requires-python = ">=3.9"
license = {file = "LICENSE"}
dependencies = [
"internetarchive==4.1.0",
"internetarchive",
"docopt==0.6.2",
"yt-dlp",
]

[project.scripts]
tubeup = "tubeup:__main__"
tubeup = "tubeup.__main__:main"

[tool.setuptools.dynamic]
version = {attr = "tubeup.__version__"}
6 changes: 2 additions & 4 deletions tubeup/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@
import logging
import traceback

import internetarchive
import internetarchive.cli

from tubeup.utils import key_value_to_dict
from tubeup.TubeUp import TubeUp
from tubeup import __version__

Expand Down Expand Up @@ -90,7 +88,7 @@ def main():
ch.setFormatter(formatter)
root.addHandler(ch)

metadata = internetarchive.cli.argparser.get_args_dict(args['--metadata'])
metadata = key_value_to_dict(args['--metadata'])

tu = TubeUp(verbose=not quiet_mode,
output_template=args['--output'])
Expand Down
19 changes: 19 additions & 0 deletions tubeup/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import os
import re
from collections import defaultdict


EMPTY_ANNOTATION_FILE = ('<?xml version="1.0" encoding="UTF-8" ?>'
'<document><annotations></annotations></document>')

def key_value_to_dict(lst):
"""
Convert many key:value pair strings into a python dictionary
"""
if not isinstance(lst, list):
lst = [lst]

result = defaultdict(list)
for item in lst:
key, value = item.split(":", 1)
assert value, f"Expected a value! for {key}"
if result[key] and value not in result[key]:
result[key].append(value)
else:
result[key] = [value]

# Convert single-item lists back to strings for non-list values
return {k: v if len(v) > 1 else v[0] for k, v in result.items()}

def sanitize_identifier(identifier, replacement='-'):
return re.sub(r'[^\w-]', replacement, identifier)
Expand Down

0 comments on commit 5a1bd46

Please sign in to comment.