2010-11-16 20:22:30 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2011-12-30 13:17:28 +00:00
|
|
|
# GPL 2012
|
2010-11-16 20:22:30 +00:00
|
|
|
from __future__ import division, with_statement
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import sqlite3
|
|
|
|
import time
|
|
|
|
import shutil
|
2011-12-30 13:17:28 +00:00
|
|
|
import sys
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
import ox
|
|
|
|
|
|
|
|
import extract
|
|
|
|
import utils
|
|
|
|
|
|
|
|
|
2011-12-28 15:00:17 +00:00
|
|
|
DEBUG = False
|
2011-12-30 13:17:28 +00:00
|
|
|
|
|
|
|
__version__ = '0.2'
|
|
|
|
CHUNK_SIZE = 1024*1024
|
2010-11-27 11:35:56 +00:00
|
|
|
default_media_cache = os.environ.get('oxMEDIA', os.path.expanduser('~/.ox/media'))
|
2010-11-16 20:22:30 +00:00
|
|
|
|
2011-12-30 13:17:28 +00:00
|
|
|
|
2011-04-06 14:42:40 +00:00
|
|
|
def encode(filename, prefix, profile, info=None):
|
|
|
|
if not info:
|
|
|
|
info = utils.avinfo(filename)
|
2011-01-18 09:52:13 +00:00
|
|
|
if not 'oshash' in info:
|
|
|
|
return None
|
2010-11-16 20:22:30 +00:00
|
|
|
oshash = info['oshash']
|
|
|
|
frames = []
|
2011-02-23 11:55:40 +00:00
|
|
|
cache = os.path.join(prefix, os.path.join(*utils.hash_prefix(oshash)))
|
|
|
|
if info['video']:
|
|
|
|
for pos in utils.video_frame_positions(info['duration']):
|
|
|
|
frame_name = '%s.png' % pos
|
|
|
|
frame_f = os.path.join(cache, frame_name)
|
|
|
|
if not os.path.exists(frame_f):
|
|
|
|
print frame_f
|
|
|
|
extract.frame(filename, frame_f, pos)
|
|
|
|
frames.append(frame_f)
|
2011-02-06 12:43:50 +00:00
|
|
|
video_f = os.path.join(cache, profile)
|
2010-11-16 20:22:30 +00:00
|
|
|
if not os.path.exists(video_f):
|
|
|
|
extract.video(filename, video_f, profile, info)
|
|
|
|
return {
|
|
|
|
'info': info,
|
|
|
|
'oshash': oshash,
|
|
|
|
'frames': frames,
|
|
|
|
'video': video_f
|
|
|
|
}
|
|
|
|
|
2012-01-02 12:32:15 +00:00
|
|
|
def encode_cmd(filename, prefix, profile, info):
|
|
|
|
if not info:
|
|
|
|
info = utils.avinfo(filename)
|
|
|
|
if not 'oshash' in info:
|
|
|
|
return None
|
|
|
|
oshash = info['oshash']
|
|
|
|
cache = os.path.join(prefix, os.path.join(*utils.hash_prefix(oshash)))
|
|
|
|
video_f = os.path.join(cache, profile)
|
|
|
|
return extract.video_cmd(filename, video_f, profile, info)
|
|
|
|
|
2010-11-16 20:22:30 +00:00
|
|
|
class Client(object):
|
2011-12-28 19:56:35 +00:00
|
|
|
def __init__(self, config, offline=False):
|
2010-11-18 14:34:34 +00:00
|
|
|
if isinstance(config, basestring):
|
|
|
|
with open(config) as f:
|
2010-11-27 11:35:56 +00:00
|
|
|
self._config = json.load(f)
|
2010-11-16 20:22:30 +00:00
|
|
|
else:
|
2011-01-14 10:48:51 +00:00
|
|
|
self._config = config
|
2011-12-28 19:56:35 +00:00
|
|
|
|
|
|
|
self.profile = self._config.get('profile', '480p.webm')
|
|
|
|
|
|
|
|
if not offline:
|
|
|
|
self.online()
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
conn, c = self._conn()
|
|
|
|
|
|
|
|
c.execute('''CREATE TABLE IF NOT EXISTS setting (key varchar(1024) unique, value text)''')
|
|
|
|
|
|
|
|
if int(self.get('version', 0)) < 1:
|
|
|
|
self.set('version', 1)
|
|
|
|
db = [
|
|
|
|
'''CREATE TABLE IF NOT EXISTS file (
|
|
|
|
path varchar(1024) unique,
|
|
|
|
oshash varchar(16),
|
|
|
|
atime FLOAT,
|
|
|
|
ctime FLOAT,
|
|
|
|
mtime FLOAT,
|
|
|
|
size INT,
|
|
|
|
info TEXT,
|
|
|
|
created INT,
|
|
|
|
modified INT,
|
|
|
|
deleted INT)''',
|
|
|
|
'''CREATE INDEX IF NOT EXISTS path_idx ON file (path)''',
|
|
|
|
'''CREATE INDEX IF NOT EXISTS oshash_idx ON file (oshash)''',
|
|
|
|
]
|
|
|
|
for i in db:
|
|
|
|
c.execute(i)
|
|
|
|
conn.commit()
|
2012-01-04 08:15:10 +00:00
|
|
|
if int(self.get('version', 0)) < 2:
|
|
|
|
self.set('version', 1)
|
|
|
|
db = [
|
|
|
|
'''CREATE TABLE IF NOT EXISTS encode (
|
|
|
|
oshash varchar(16),
|
|
|
|
site varchar(255))''',
|
|
|
|
'''CREATE INDEX IF NOT EXISTS upload_site_idx ON encode (site)''',
|
|
|
|
]
|
|
|
|
for i in db:
|
|
|
|
c.execute(i)
|
|
|
|
conn.commit()
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
def _conn(self):
|
2011-02-24 10:52:59 +00:00
|
|
|
db_conn = os.path.expanduser(self._config['cache'])
|
|
|
|
if not os.path.exists(os.path.dirname(db_conn)):
|
|
|
|
os.makedirs(os.path.dirname(db_conn))
|
2010-11-16 20:22:30 +00:00
|
|
|
conn = sqlite3.connect(db_conn, timeout=10)
|
|
|
|
conn.text_factory = sqlite3.OptimizedUnicode
|
|
|
|
return conn, conn.cursor()
|
|
|
|
|
2010-11-27 11:35:56 +00:00
|
|
|
def media_cache(self):
|
|
|
|
return os.path.expanduser(self._config.get('media-cache', default_media_cache))
|
|
|
|
|
2010-11-16 20:22:30 +00:00
|
|
|
def get(self, key, default=None):
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute('SELECT value FROM setting WHERE key = ?', (key, ))
|
|
|
|
for row in c:
|
|
|
|
return row[0]
|
|
|
|
return default
|
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute(u'INSERT OR REPLACE INTO setting values (?, ?)', (key, str(value)))
|
|
|
|
conn.commit()
|
|
|
|
|
2011-04-06 14:42:40 +00:00
|
|
|
def info(self, oshash):
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute('SELECT info FROM file WHERE oshash = ?', (oshash, ))
|
|
|
|
for row in c:
|
2011-12-28 15:00:17 +00:00
|
|
|
return json.loads(row[0])
|
2011-04-06 14:42:40 +00:00
|
|
|
return None
|
|
|
|
|
2011-12-28 15:00:17 +00:00
|
|
|
def path(self, oshash):
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute('SELECT path FROM file WHERE oshash = ?', (oshash, ))
|
|
|
|
paths = []
|
|
|
|
for row in c:
|
|
|
|
paths.append(row[0])
|
|
|
|
return paths
|
|
|
|
|
2011-12-28 19:56:35 +00:00
|
|
|
def online(self):
|
|
|
|
self.api = API(self._config['url'], media_cache=self.media_cache())
|
|
|
|
self.api.DEBUG = DEBUG
|
|
|
|
self.signin()
|
|
|
|
self.profile = "%sp.webm" % max(self.api._config['video']['resolutions'])
|
|
|
|
|
2011-04-08 10:03:33 +00:00
|
|
|
def signin(self):
|
|
|
|
if 'username' in self._config:
|
|
|
|
r = self.api.signin(username=self._config['username'], password=self._config['password'])
|
|
|
|
if r['status']['code'] == 200 and not 'errors' in r['data']:
|
|
|
|
self.user = r['data']['user']
|
|
|
|
else:
|
|
|
|
self.user = False
|
|
|
|
print 'login failed'
|
|
|
|
return False
|
2011-12-20 10:29:43 +00:00
|
|
|
r = self.api.init()
|
|
|
|
if r['status']['code'] == 200:
|
|
|
|
self.api._config = r['data']['site']
|
2011-04-08 10:03:33 +00:00
|
|
|
return True
|
|
|
|
|
2012-01-04 08:15:10 +00:00
|
|
|
def set_encodes(self, site, files):
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute('DELETE FROM encode WHERE site = ?' , (site, ))
|
|
|
|
for oshash in files:
|
|
|
|
c.execute(u'INSERT INTO encode VALUES (?, ?)', (oshash, site))
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
def get_encodes(self, site):
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute('SELECT oshash FROM encodes WHERE site = ?', (site, ))
|
|
|
|
files = []
|
|
|
|
for row in c:
|
|
|
|
files.append(row[0])
|
|
|
|
return files
|
|
|
|
|
2010-11-16 20:22:30 +00:00
|
|
|
def scan_file(self, path):
|
|
|
|
conn, c = self._conn()
|
|
|
|
|
|
|
|
update = True
|
|
|
|
modified = time.mktime(time.localtime())
|
|
|
|
created = modified
|
|
|
|
|
|
|
|
sql = 'SELECT atime, ctime, mtime, size, created FROM file WHERE deleted < 0 AND path=?'
|
|
|
|
c.execute(sql, [path])
|
|
|
|
stat = os.stat(path)
|
|
|
|
for row in c:
|
|
|
|
if stat.st_atime == row[0] and stat.st_ctime == row[1] and stat.st_mtime == row[2] and stat.st_size == row[3]:
|
|
|
|
created = row[4]
|
|
|
|
update = False
|
|
|
|
break
|
|
|
|
if update:
|
|
|
|
info = utils.avinfo(path)
|
2010-12-28 15:20:32 +00:00
|
|
|
if info['size'] > 0:
|
|
|
|
oshash = info['oshash']
|
|
|
|
deleted = -1
|
|
|
|
t = (path, oshash, stat.st_atime, stat.st_ctime, stat.st_mtime,
|
|
|
|
stat.st_size, json.dumps(info), created, modified, deleted)
|
|
|
|
c.execute(u'INSERT OR REPLACE INTO file values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', t)
|
|
|
|
conn.commit()
|
2010-11-16 20:22:30 +00:00
|
|
|
|
2012-01-02 12:32:15 +00:00
|
|
|
def cmd(self, args):
|
|
|
|
filename = args[0]
|
|
|
|
if len(filename) == 16:
|
|
|
|
path = self.paths(filename)
|
|
|
|
else:
|
|
|
|
path = [filename]
|
|
|
|
for p in path:
|
|
|
|
if os.path.exists(p):
|
|
|
|
cmd = encode_cmd(p, self.media_cache(), self.profile, None)
|
|
|
|
cmd = [' ' in c and '"%s"' % c or c for c in cmd]
|
|
|
|
print ' '.join(cmd)
|
|
|
|
|
|
|
|
def scan(self, args):
|
2011-12-30 13:17:28 +00:00
|
|
|
print "checking for new files ..."
|
2010-11-16 20:22:30 +00:00
|
|
|
for name in self._config['volumes']:
|
|
|
|
path = self._config['volumes'][name]
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
files = []
|
2010-12-07 18:32:15 +00:00
|
|
|
for dirpath, dirnames, filenames in os.walk(path, followlinks=True):
|
2010-11-16 20:22:30 +00:00
|
|
|
if isinstance(dirpath, str):
|
|
|
|
dirpath = dirpath.decode('utf-8')
|
|
|
|
if filenames:
|
|
|
|
for filename in sorted(filenames):
|
|
|
|
if isinstance(filename, str):
|
|
|
|
filename = filename.decode('utf-8')
|
|
|
|
if not filename.startswith('._') and not filename in ('.DS_Store', ):
|
|
|
|
file_path = os.path.join(dirpath, filename)
|
2011-01-14 10:48:51 +00:00
|
|
|
if os.path.exists(file_path):
|
|
|
|
files.append(file_path)
|
|
|
|
self.scan_file(file_path)
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
conn, c = self._conn()
|
|
|
|
c.execute('SELECT path FROM file WHERE path LIKE ? AND deleted < 0', ["%s%%"%path])
|
|
|
|
known_files = [r[0] for r in c.fetchall()]
|
|
|
|
deleted_files = filter(lambda f: f not in files, known_files)
|
2011-12-28 15:00:17 +00:00
|
|
|
|
2010-11-16 20:22:30 +00:00
|
|
|
if deleted_files:
|
|
|
|
deleted = time.mktime(time.localtime())
|
|
|
|
for f in deleted_files:
|
|
|
|
c.execute('UPDATE file SET deleted=? WHERE path=?', (deleted, f))
|
|
|
|
conn.commit()
|
|
|
|
|
2011-12-30 13:17:28 +00:00
|
|
|
print "scanned volume %s: %s files, %s new, %s deleted" % (
|
2011-12-28 15:00:17 +00:00
|
|
|
name, len(files), len(files) - len(known_files), len(deleted_files))
|
|
|
|
|
2012-01-04 08:15:10 +00:00
|
|
|
def extract(self, args):
|
2012-01-04 07:01:43 +00:00
|
|
|
conn, c = self._conn()
|
2012-01-04 08:15:10 +00:00
|
|
|
if args:
|
|
|
|
if args[0] == 'offline':
|
|
|
|
files = self.get_encodes(self._config['url'])
|
|
|
|
elif args[0] == 'all':
|
|
|
|
files = []
|
|
|
|
for name in self._config['volumes']:
|
|
|
|
path = self._config['volumes'][name]
|
|
|
|
path = os.path.normpath(path)
|
2012-01-04 07:01:43 +00:00
|
|
|
|
|
|
|
if os.path.exists(path):
|
2012-01-04 08:15:10 +00:00
|
|
|
files += self.files(path).keys['info']
|
|
|
|
def no_extras(oshash):
|
|
|
|
for path in self.path(oshash):
|
|
|
|
if '/extras' in path.lower() or \
|
|
|
|
'/versions' in path.lower():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
files = filter(no_extras, files)
|
|
|
|
else:
|
|
|
|
if not self.user:
|
|
|
|
print "you need to login or run pandora_client extract offline"
|
|
|
|
return
|
|
|
|
#send empty list to get updated list of requested info/files/data
|
|
|
|
post = {'info': {}}
|
|
|
|
r = self.api.update(post)
|
|
|
|
files = r['data']['data']
|
|
|
|
self.set_encodes(self._config['url'], files)
|
|
|
|
|
|
|
|
for oshash in files:
|
|
|
|
for path in self.path(oshash):
|
|
|
|
if os.path.exists(path):
|
|
|
|
info = self.info(oshash)
|
|
|
|
print path.encode('utf-8')
|
|
|
|
i = encode(path, self.media_cache(), self.profile, info)
|
|
|
|
break
|
2010-12-28 14:14:48 +00:00
|
|
|
|
2012-01-02 12:32:15 +00:00
|
|
|
def sync(self, args):
|
2011-04-06 14:42:40 +00:00
|
|
|
if not self.user:
|
|
|
|
print "you need to login"
|
|
|
|
return
|
2010-11-16 20:22:30 +00:00
|
|
|
conn, c = self._conn()
|
|
|
|
|
|
|
|
volumes = {}
|
|
|
|
for name in self._config['volumes']:
|
|
|
|
path = self._config['volumes'][name]
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
|
|
|
|
volumes[name] = {}
|
|
|
|
volumes[name]['path'] = path
|
|
|
|
if os.path.exists(path):
|
|
|
|
volumes[name]['available'] = True
|
|
|
|
else:
|
|
|
|
volumes[name]['available'] = False
|
|
|
|
|
|
|
|
for name in volumes:
|
|
|
|
if volumes[name]['available']:
|
|
|
|
prefix = volumes[name]['path']
|
|
|
|
files = self.files(prefix)
|
2011-04-06 14:42:40 +00:00
|
|
|
post = {}
|
|
|
|
post['files'] = files['files']
|
|
|
|
post['volume'] = name
|
2011-12-30 13:17:28 +00:00
|
|
|
print 'sending list of files in %s (%s total)' % (name, len(post['files']))
|
2011-04-06 14:42:40 +00:00
|
|
|
r = self.api.update(post)
|
2010-11-16 20:22:30 +00:00
|
|
|
if r['status']['code'] == 200:
|
2010-12-25 13:54:36 +00:00
|
|
|
#backend works on update request asyncronously, wait for it to finish
|
|
|
|
if 'taskId' in r['data']:
|
|
|
|
t = self.api.taskStatus(task_id=r['data']['taskId'])
|
2011-12-30 13:17:28 +00:00
|
|
|
print 'waiting for server ...'
|
2010-12-25 13:54:36 +00:00
|
|
|
while t['data']['status'] == 'PENDING':
|
|
|
|
time.sleep(5)
|
|
|
|
t = self.api.taskStatus(task_id=r['data']['taskId'])
|
2011-04-06 14:42:40 +00:00
|
|
|
#send empty list to get updated list of requested info/files/data
|
2010-12-25 13:54:36 +00:00
|
|
|
post = {'info': {}}
|
|
|
|
r = self.api.update(post)
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
if r['data']['info']:
|
2011-04-06 14:42:40 +00:00
|
|
|
info = r['data']['info']
|
|
|
|
max_info = 100
|
2011-04-06 14:53:45 +00:00
|
|
|
total = len(info)
|
2011-12-30 13:17:28 +00:00
|
|
|
print 'sending info for %s files' % total
|
2011-04-06 14:53:45 +00:00
|
|
|
for offset in range(0, total, max_info):
|
|
|
|
post = {'info': {}, 'upload': True}
|
2011-04-06 14:42:40 +00:00
|
|
|
for oshash in info[offset:offset+max_info]:
|
|
|
|
if oshash in files['info']:
|
|
|
|
post['info'][oshash] = files['info'][oshash]
|
2011-08-10 12:24:01 +00:00
|
|
|
if len(post['info']):
|
|
|
|
r = self.api.update(post)
|
2011-12-28 15:00:17 +00:00
|
|
|
if r['data']['data']:
|
|
|
|
files = []
|
|
|
|
for f in r['data']['data']:
|
|
|
|
for path in self.path(f):
|
|
|
|
if os.path.exists(path):
|
|
|
|
files.append(path)
|
|
|
|
break
|
|
|
|
if files:
|
2011-12-30 13:17:28 +00:00
|
|
|
print '\ncould encoded and upload %s videos:\n' % len(files)
|
2011-12-28 15:00:17 +00:00
|
|
|
print '\n'.join(files)
|
|
|
|
if r['data']['file']:
|
|
|
|
files = []
|
|
|
|
for f in r['data']['file']:
|
|
|
|
for path in self.path(f):
|
|
|
|
if os.path.exists(path):
|
|
|
|
files.append(path)
|
|
|
|
break
|
|
|
|
if files:
|
2011-12-30 13:17:28 +00:00
|
|
|
print '\ncould upload %s subtitles:\n' % len(files)
|
2011-12-28 15:00:17 +00:00
|
|
|
print '\n'.join(files)
|
2011-08-23 19:20:16 +00:00
|
|
|
|
2012-01-02 12:32:15 +00:00
|
|
|
def upload(self, args):
|
2011-08-23 19:20:16 +00:00
|
|
|
if not self.user:
|
|
|
|
print "you need to login"
|
|
|
|
return
|
|
|
|
conn, c = self._conn()
|
|
|
|
|
|
|
|
#send empty list to get updated list of requested info/files/data
|
|
|
|
post = {'info': {}}
|
|
|
|
r = self.api.update(post)
|
|
|
|
|
|
|
|
if r['data']['file']:
|
2011-12-30 13:17:28 +00:00
|
|
|
print 'uploading %s files' % len(r['data']['file'])
|
2011-08-23 19:20:16 +00:00
|
|
|
for oshash in r['data']['file']:
|
2011-12-31 11:14:04 +00:00
|
|
|
for path in self.path(oshash):
|
|
|
|
if os.path.exists(path):
|
|
|
|
self.api.uploadData(path, oshash)
|
2012-01-04 07:01:43 +00:00
|
|
|
break
|
2011-08-23 19:20:16 +00:00
|
|
|
|
|
|
|
if r['data']['data']:
|
2011-12-30 13:17:28 +00:00
|
|
|
print 'encoding and uploading %s videos' % len(r['data']['data'])
|
2011-08-23 19:20:16 +00:00
|
|
|
for oshash in r['data']['data']:
|
|
|
|
data = {}
|
2011-12-31 11:14:04 +00:00
|
|
|
for path in self.path(oshash):
|
|
|
|
if os.path.exists(path):
|
|
|
|
info = self.info(oshash)
|
2012-01-04 07:01:43 +00:00
|
|
|
if not self.api.uploadVideo(path,
|
|
|
|
data, self.profile, info):
|
2011-12-31 11:14:04 +00:00
|
|
|
if not self.signin():
|
|
|
|
print "failed to login again"
|
|
|
|
return
|
2012-01-04 07:01:43 +00:00
|
|
|
break
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
def files(self, prefix):
|
|
|
|
conn, c = self._conn()
|
|
|
|
files = {}
|
|
|
|
files['info'] = {}
|
|
|
|
files['files'] = []
|
|
|
|
sql = 'SELECT path, oshash, info, atime, ctime, mtime FROM file WHERE deleted < 0 AND path LIKE ? ORDER BY path'
|
|
|
|
t = [u"%s%%"%prefix]
|
|
|
|
c.execute(sql, t)
|
|
|
|
for row in c:
|
|
|
|
path = row[0]
|
|
|
|
oshash = row[1]
|
|
|
|
info = json.loads(row[2])
|
|
|
|
for key in ('atime', 'ctime', 'mtime', 'path'):
|
|
|
|
if key in info:
|
|
|
|
del info[key]
|
|
|
|
files['info'][oshash] = info
|
|
|
|
files['files'].append({
|
|
|
|
'oshash': oshash,
|
|
|
|
'path': path[len(prefix)+1:],
|
|
|
|
'atime': row[3],
|
|
|
|
'ctime': row[4],
|
|
|
|
'mtime': row[5],
|
|
|
|
})
|
|
|
|
return files
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
print "remove temp videos and stills"
|
2010-11-27 11:35:56 +00:00
|
|
|
if os.path.exists(self.prefix()):
|
|
|
|
shutil.rmtree(self.prefix())
|
2010-11-16 20:22:30 +00:00
|
|
|
|
2011-11-06 12:40:59 +00:00
|
|
|
class API(ox.API):
|
|
|
|
__name__ = 'pandora_client'
|
|
|
|
__version__ = __version__
|
2011-08-10 12:24:01 +00:00
|
|
|
|
2011-11-06 12:40:59 +00:00
|
|
|
def __init__(self, url, cj=None, media_cache=None):
|
|
|
|
super(API, self).__init__(url, cj)
|
2010-11-16 20:22:30 +00:00
|
|
|
|
2010-11-27 11:35:56 +00:00
|
|
|
self.media_cache = media_cache
|
|
|
|
if not self.media_cache:
|
|
|
|
self.media_cache = default_media_cache
|
|
|
|
|
2011-04-06 14:42:40 +00:00
|
|
|
def uploadVideo(self, filename, data, profile, info=None):
|
|
|
|
i = encode(filename, self.media_cache, profile, info)
|
2011-01-18 09:52:13 +00:00
|
|
|
if not i:
|
|
|
|
print "failed"
|
|
|
|
return
|
2011-12-20 10:29:43 +00:00
|
|
|
|
2010-11-16 20:22:30 +00:00
|
|
|
#upload frames
|
2011-12-20 10:29:43 +00:00
|
|
|
if self._config['media']['importPosterFrames']:
|
|
|
|
form = ox.MultiPartForm()
|
|
|
|
form.add_field('action', 'upload')
|
|
|
|
form.add_field('id', i['oshash'])
|
|
|
|
for key in data:
|
|
|
|
form.add_field(key, data[key])
|
|
|
|
for frame in i['frames']:
|
|
|
|
fname = os.path.basename(frame)
|
|
|
|
if os.path.exists(frame):
|
|
|
|
form.add_file('frame', fname, open(frame, 'rb'))
|
2011-12-27 13:05:43 +00:00
|
|
|
r = self._json_request(self.url, form)
|
2011-12-20 10:29:43 +00:00
|
|
|
|
|
|
|
#upload video
|
2011-08-08 14:14:47 +00:00
|
|
|
if os.path.exists(i['video']):
|
2011-12-30 09:05:12 +00:00
|
|
|
size = ox.formatBytes(os.path.getsize(i['video']))
|
2011-12-30 13:17:28 +00:00
|
|
|
print "uploading %s of %s (%s)" % (profile, os.path.basename(filename), size)
|
2011-08-08 14:14:47 +00:00
|
|
|
url = self.url + 'upload/' + '?profile=' + str(profile) + '&id=' + i['oshash']
|
2011-12-30 13:17:28 +00:00
|
|
|
if not self.upload_chunks(url, i['video'], data):
|
2011-08-08 14:14:47 +00:00
|
|
|
if DEBUG:
|
|
|
|
print "failed"
|
|
|
|
return False
|
|
|
|
else:
|
2011-12-28 15:00:17 +00:00
|
|
|
print "Failed"
|
2011-04-08 10:03:33 +00:00
|
|
|
return False
|
|
|
|
return True
|
2010-11-16 20:22:30 +00:00
|
|
|
|
|
|
|
def uploadData(self, filename, oshash):
|
2011-08-24 16:58:49 +00:00
|
|
|
if DEBUG:
|
|
|
|
print 'upload', filename
|
2010-11-16 20:22:30 +00:00
|
|
|
form = ox.MultiPartForm()
|
|
|
|
form.add_field('action', 'upload')
|
2011-08-08 14:14:47 +00:00
|
|
|
form.add_field('id', str(oshash))
|
2010-11-16 20:22:30 +00:00
|
|
|
fname = os.path.basename(filename)
|
|
|
|
if isinstance(fname, unicode): fname = fname.encode('utf-8')
|
|
|
|
form.add_file('file', fname, open(filename, 'rb'))
|
|
|
|
r = self._json_request(self.url, form)
|
|
|
|
return r
|
|
|
|
|
2011-12-30 13:17:28 +00:00
|
|
|
def upload_chunks(self, url, filename, data=None):
|
|
|
|
form = ox.MultiPartForm()
|
|
|
|
if not data:
|
|
|
|
for key in data:
|
|
|
|
form.add_field(key, data[key])
|
|
|
|
data = self._json_request(url, form)
|
2011-12-31 10:44:47 +00:00
|
|
|
if 'url' in data:
|
|
|
|
print data['url']
|
2011-12-30 13:17:28 +00:00
|
|
|
if 'uploadUrl' in data:
|
|
|
|
uploadUrl = data['uploadUrl']
|
|
|
|
f = open(filename)
|
|
|
|
fsize = os.stat(filename).st_size
|
|
|
|
done = 0
|
2011-12-31 14:40:11 +00:00
|
|
|
start = time.mktime(time.localtime())
|
2011-12-30 13:17:28 +00:00
|
|
|
chunk = f.read(CHUNK_SIZE)
|
|
|
|
fname = os.path.basename(filename)
|
|
|
|
if isinstance(fname, unicode):
|
|
|
|
fname = fname.encode('utf-8')
|
|
|
|
while chunk:
|
2011-12-31 14:40:11 +00:00
|
|
|
elapsed = time.mktime(time.localtime()) - start
|
|
|
|
remaining = elapsed / (done/fsize) - elapsed
|
|
|
|
print '%0.2f%% %s of %s done, %s remaining\r' % (
|
|
|
|
100 * done/fsize, ox.formatBytes(done), ox.formatBytes(fsize),
|
|
|
|
ox.formatDuration(remaining, verbosity=2)),
|
2011-12-30 13:17:28 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
form = ox.MultiPartForm()
|
|
|
|
form.add_file('chunk', fname, chunk)
|
|
|
|
if len(chunk) < CHUNK_SIZE or f.tell() == fsize:
|
|
|
|
form.add_field('done', '1')
|
|
|
|
try:
|
|
|
|
data = self._json_request(uploadUrl, form)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print "\ninterrupted by user."
|
|
|
|
sys.exit(1)
|
|
|
|
except:
|
2012-01-02 12:32:15 +00:00
|
|
|
print "uploading chunk failed, will try again in 5 seconds\r",
|
|
|
|
sys.stdout.flush()
|
2011-12-30 13:17:28 +00:00
|
|
|
if DEBUG:
|
2012-01-02 12:32:15 +00:00
|
|
|
print '\n', uploadUrl
|
2011-12-30 13:17:28 +00:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
data = {'result': -1}
|
|
|
|
time.sleep(5)
|
|
|
|
if data and 'status' in data:
|
|
|
|
if data['status']['code'] == 403:
|
|
|
|
print "login required"
|
|
|
|
return False
|
|
|
|
if data['status']['code'] != 200:
|
|
|
|
print "request returned error, will try again in 5 seconds"
|
|
|
|
if DEBUG:
|
|
|
|
print data
|
|
|
|
time.sleep(5)
|
2011-12-31 14:40:11 +00:00
|
|
|
if data and data.get('result') == 1:
|
2011-12-30 13:17:28 +00:00
|
|
|
done += len(chunk)
|
|
|
|
chunk = f.read(CHUNK_SIZE)
|
|
|
|
print ' '
|
2011-12-31 14:40:11 +00:00
|
|
|
return data and 'result' in data and data.get('result') == 1
|
2011-12-30 13:17:28 +00:00
|
|
|
else:
|
|
|
|
if DEBUG:
|
|
|
|
if 'status' in data and data['status']['code'] == 401:
|
|
|
|
print "login required"
|
|
|
|
else:
|
|
|
|
print "failed to upload file to", url
|
|
|
|
print data
|
|
|
|
return False
|
|
|
|
|