106 lines
2.9 KiB
Python
Executable file
106 lines
2.9 KiB
Python
Executable file
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
import json
|
|
import ox
|
|
import os
|
|
|
|
|
|
api = ox.API('https://0xdb.org/api/')
|
|
config = json.load(open(os.path.expanduser('~/.ox/client.json')))
|
|
api.signin(username=config['username'], password=config['password'])
|
|
|
|
def get_files(item):
|
|
files = api.findMedia({
|
|
"keys": [
|
|
"selected",
|
|
#"path",
|
|
"part",
|
|
"language",
|
|
"type",
|
|
"duration",
|
|
"instances"
|
|
],
|
|
"query": {
|
|
"conditions":[{
|
|
"key": "id",
|
|
"value": item,
|
|
"operator": "=="
|
|
}]
|
|
},
|
|
"range": [0, 100],
|
|
"sort": [{"key": "path", "operator": "+"}]
|
|
})['data']['items']
|
|
files = filter(lambda f: f['type'] == 'video' and f['selected'], files)
|
|
for f in files:
|
|
f['path'] = f['instances'][0]['path']
|
|
del f['instances']
|
|
|
|
return files
|
|
|
|
def get_clips(terms):
|
|
clips = []
|
|
for term in terms:
|
|
r = api.findClips({
|
|
"keys":["position","annotations","id","in","out","videoRatio", "parts"],
|
|
"query":{"conditions":[{"operator":"=","key":"subtitles","value":term}],
|
|
"operator":"&"},
|
|
"range":[0,1000],
|
|
"sort":[{"operator":"+","key":"position"}],
|
|
#"sort":[{"operator":"+","key":"year"}],
|
|
"itemsQuery":{
|
|
"operator":"&",
|
|
"conditions":[{"operator":"=","key":"subtitles","value":term}]
|
|
}
|
|
})
|
|
if not 'data' in r or not 'items' in r['data']:
|
|
print r
|
|
clips += r['data']['items']
|
|
|
|
cache = {}
|
|
|
|
playlist = []
|
|
clips.sort(key=lambda c: c['in'])
|
|
|
|
for clip in clips:
|
|
id = clip['id'].split('/')[0]
|
|
part = 1
|
|
if not id in cache:
|
|
cache[id] = get_files(id)
|
|
data = cache[id]
|
|
position = 0
|
|
#FIXME: handle clips that span across parts
|
|
for part, f in enumerate(data):
|
|
if clip['in'] < position + f['duration']:
|
|
clip_in = clip['in'] - position
|
|
clip_out = clip['out'] - position
|
|
break
|
|
else:
|
|
position += f['duration']
|
|
|
|
path = f['path']
|
|
print path, clip_in, clip_out
|
|
subtitles = ''
|
|
for a in clip['annotations']:
|
|
subtitles += a['value']
|
|
subtitles = ox.strip_tags(subtitles)
|
|
playlist.append({
|
|
"id": id,
|
|
"part": part,
|
|
"path": path,
|
|
"in": clip_in,
|
|
"out": clip_out,
|
|
"subtitles": subtitles
|
|
})
|
|
return playlist
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
if len(sys.argv) != 3:
|
|
print 'usage: %s <term> <output.json>' % sys.argv[0]
|
|
sys.exit(1)
|
|
terms = [sys.argv[1]]
|
|
output = sys.argv[2]
|
|
playlist = get_clips(terms)
|
|
with open(output, 'w') as f:
|
|
json.dump(playlist, f, indent=2)
|