From e3adbbbbd0b44ff2806b34dd9aa20e9aee73d47b Mon Sep 17 00:00:00 2001 From: j Date: Sun, 14 Nov 2021 22:48:52 +0000 Subject: [PATCH] pandora-upload --- .editorconfig | 23 +++++++++++++ .gitignore | 6 ++++ README.md | 20 +++++++++++ pandora_upload/__init__.py | 70 ++++++++++++++++++++++++++++++++++++++ pandora_upload/__main__.py | 2 ++ setup.py | 58 +++++++++++++++++++++++++++++++ 6 files changed, 179 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 README.md create mode 100755 pandora_upload/__init__.py create mode 100644 pandora_upload/__main__.py create mode 100644 setup.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e5938ae --- /dev/null +++ b/.editorconfig @@ -0,0 +1,23 @@ +[*] +end_of_line = lf +insert_final_newline = true + +[*.py] +indent_style = space +indent_size = 4 + +[*.sass] +indent_style = space +indent_size = 2 + +[*.scss] +indent_style = space +indent_size = 2 + +[*.html] +indent_style = space +indent_size = 4 + +[*.js] +indent_style = space +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac793c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +dist/ +pandora_upload.egg-info/ +*.pyc +*.swp +*.swo +__pycache__ diff --git a/README.md b/README.md new file mode 100644 index 0000000..98de011 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# pandora-upload + +pandora_upload is a commandline client for pan.do/ra. +You can use it to upload one or more files to your pandora instance. +No conversion is done on the client side. +To upload/sync large repositories use pandora_client + +You can also use pandora-upload as a python script: + +``` python +import pandora_client +item_id = pandora_client.upload( + 'http://pandora/api/', + ['/home/example/Videos/video.mp4', + { + 'title': 'This is an example', + 'date': '2021-11-15' + } +) +``` diff --git a/pandora_upload/__init__.py b/pandora_upload/__init__.py new file mode 100755 index 0000000..ce8226b --- /dev/null +++ b/pandora_upload/__init__.py @@ -0,0 +1,70 @@ +#!/usr/bin/python3 + +from argparse import ArgumentParser +import json +import os +import sys + +import ox +import ox.api + + +def upload(api_url, files, metadata): + ''' + create new item with metadata and upload all files to that item + returns the item id of the new item + + upload('https://pandora/api/', ['/home/pandora/Videos/example.mp4'], {'title': 'This is an example'}) + ''' + api = ox.api.signin(api_url) + item_id = None + if isinstance(files, str): + files = [files] + for path in files: + oshash = ox.oshash(path) + filename = os.path.basename(path) + data = { + 'id': oshash, + 'filename': filename + } + if item_id: + data['item'] = item_id + r = api.addMedia(data) + if not item_id: + item_id = r['data']['item'] + + api.upload_chunks(api.url + 'upload/direct/', path, {'id': oshash}) + if item_id: + data = metadata.copy() + data['id'] = item_id + api.edit(data) + return item_id + +def main(): + usage = "usage: %(prog)s [options] path" + parser = ArgumentParser(usage=usage, prog='pandora-upload') + parser.add_argument('-v', '--version', action="version", version="%(prog)s 1.0") + parser.add_argument('-p', '--pandora', dest='api_url', + help='pandora api url', default='http://127.0.0.1:2620/api/') + parser.add_argument('-d', '--debug', dest='debug', + help='output debug information', action="store_true") + parser.add_argument('-m', '--meta', dest='meta', help='metadata key=value', action='append') + parser.add_argument('files', metavar='path', type=str, nargs='*', help='file or files to upload') + opts = parser.parse_args() + meta = {} + if opts.meta: + for m in opts.meta: + if '=' not in m: + parser.print_help() + print('\ninvalid metadata argument, format is -m "key=value"') + sys.exit(1) + k, v = m.split('=', 1) + meta[k] = v + files = opts.files + if not files: + parser.print_help() + sys.exit(1) + + id = upload(opts.api_url, files, meta) + print(id) + diff --git a/pandora_upload/__main__.py b/pandora_upload/__main__.py new file mode 100644 index 0000000..031df43 --- /dev/null +++ b/pandora_upload/__main__.py @@ -0,0 +1,2 @@ +from . import main +main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7f2a82f --- /dev/null +++ b/setup.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +from setuptools import setup + +with open("README.md", "r", encoding="utf-8") as fh: + long_description = fh.read() + + +def get_version(): + import os + import re + import subprocess + dot_git = os.path.join(os.path.dirname(__file__), '.git') + changelog = os.path.join(os.path.dirname(__file__), 'debian/changelog') + if os.path.exists(dot_git): + cmd = ['git', 'rev-list', 'HEAD', '--count'] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + rev = int(stdout) + return u'%s' % rev + elif os.path.exists(changelog): + f = open(changelog) + head = f.read().strip().split('\n')[0] + f.close() + rev = re.compile('\d+\.\d+\.(\d+)').findall(head) + if rev: + return "%s" % rev[0] + return 'unknown' + + +setup( + name="pandora-upload", + version="1.0.%s" % get_version(), + description="pandora-upload is a commandline uploader for pan.do/ra", + long_description=long_description, + long_description_content_type="text/markdown", + author="j", + author_email="j@mailb.org", + url="https://code.0x2620.org/0x2620/pandora-upload", + license="GPLv3", + packages=['pandora_upload'], + entry_points={ + 'console_scripts': [ + 'pandora-upload=pandora_upload:main' + ] + }, + install_requires=[ + 'ox >= 2.3.804,<3', + 'lxml', + ], + keywords=[], + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Environment :: Console', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 3', + 'License :: OSI Approved :: GNU General Public License (GPL)', + ], +)