pandora-upload
This commit is contained in:
commit
e3adbbbbd0
6 changed files with 179 additions and 0 deletions
70
pandora_upload/__init__.py
Executable file
70
pandora_upload/__init__.py
Executable file
|
|
@ -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)
|
||||
|
||||
2
pandora_upload/__main__.py
Normal file
2
pandora_upload/__main__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
from . import main
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue