add config/add_volume commands

This commit is contained in:
j 2012-02-21 15:15:27 +05:30
parent 4e8e4a0cf7
commit 0102bc68c4
2 changed files with 64 additions and 7 deletions

View file

@ -1,11 +1,12 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2008 # GPL 2012
import os import os
import sys import sys
from optparse import OptionParser from optparse import OptionParser
import json
root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..') root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
if os.path.exists(os.path.join(root, 'pandora_client')): if os.path.exists(os.path.join(root, 'pandora_client')):
@ -21,17 +22,30 @@ if __name__ == '__main__':
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
opts.config = os.path.expanduser(opts.config) opts.config = os.path.expanduser(opts.config)
if None in (opts.config, ) or not os.path.exists(opts.config): if None in (opts.config, ) or (args[0] != 'config' and not os.path.exists(opts.config)):
parser.print_help() parser.print_help()
sys.exit() sys.exit()
actions = ('scan', 'sync', 'upload', 'extract', 'clean', 'cmd', 'import_srt') actions = ('scan', 'sync', 'upload', 'extract', 'clean', 'cmd', 'import_srt')
if not args or args[0] not in actions: config = ('config', 'add_volume')
parser.error('you must specify a valid action. \n\t\tknown actions are: %s' % ', '.join(actions)) if not args or args[0] not in actions + config:
parser.error('you must specify a valid action. \n\t\tknown actions are: %s\n\t\tconfiguration: config, add_volume' % ', '.join(actions))
action = args[0] action = args[0]
offline = False offline = False
offline = action in config
if action == 'config':
if not os.path.exists(opts.config):
with open(opts.config, 'w') as f:
json.dump({
"url": "",
"username": "",
"password": "",
"cache": "~/.ox/client.sqlite",
"media": '~/.ox/media',
"volumes": {}
}, f, indent=2)
c = pandora_client.Client(opts.config, offline) c = pandora_client.Client(opts.config, offline)
getattr(c, action)(args[1:]) getattr(c, action)(args[1:])

View file

@ -10,6 +10,7 @@ import time
import shutil import shutil
import sys import sys
import socket import socket
import getpass
import ox import ox
@ -23,7 +24,7 @@ __version__ = '0.2'
socket.setdefaulttimeout(300) socket.setdefaulttimeout(300)
CHUNK_SIZE = 1024*1024 CHUNK_SIZE = 1024*1024
default_media_cache = os.environ.get('oxMEDIA', os.path.expanduser('~/.ox/media')) default_media_cache = os.environ.get('oxMEDIA', '~/.ox/media')
def encode(filename, prefix, profile, info=None, extract_frames=True): def encode(filename, prefix, profile, info=None, extract_frames=True):
if not info: if not info:
@ -62,8 +63,11 @@ def encode_cmd(filename, prefix, profile, info):
return extract.video_cmd(filename, video_f, profile, info) return extract.video_cmd(filename, video_f, profile, info)
class Client(object): class Client(object):
_configfile = None
def __init__(self, config, offline=False): def __init__(self, config, offline=False):
if isinstance(config, basestring): if isinstance(config, basestring):
self._configfile = config
with open(config) as f: with open(config) as f:
self._config = json.load(f) self._config = json.load(f)
else: else:
@ -222,6 +226,45 @@ class Client(object):
cmd = [' ' in c and '"%s"' % c or c for c in cmd] cmd = [' ' in c and '"%s"' % c or c for c in cmd]
print ' '.join(cmd) print ' '.join(cmd)
def save_config(self):
if not self._configfile:
raise Exception('Can not save temporary config')
with open(self._configfile, 'w') as f:
json.dump(self._config, f, indent=2)
def config(self, args):
print "Current Config:\n User %s\n URL:%s\n" %(self._config['username'], self._config['url'])
print "Leave empty to keep current value\n"
username = raw_input('Username: ')
if username:
self._config['username'] = username
password = getpass.getpass('Password: ')
if password:
self._config['password'] = password
url = raw_input('Pan.do/ra URL(i.e. http://pad.ma/api/): ')
if url:
self._config['url'] = url
self.save_config()
print "\nconfiguration updated."
def add_volume(self, args):
if len(args) != 2:
print "Usage: %s add_volume name path" % sys.argv[0]
sys.exit(1)
name = args[0]
path = args[1]
if not path.endswith('/'):
path = path+'/'
if os.path.isdir(path):
if name in self._config['volumes']:
print "updated %s to %s" % (name, path)
else:
print "added %s %s" % (name, path)
self._config['volumes'][name] = path
self.save_config()
self._config['volumes'][name] = path
def scan(self, args): def scan(self, args):
print "checking for new files ..." print "checking for new files ..."
for name in sorted(self._config['volumes']): for name in sorted(self._config['volumes']):
@ -469,11 +512,11 @@ class API(ox.API):
self.media_cache = media_cache self.media_cache = media_cache
if not self.media_cache: if not self.media_cache:
self.media_cache = default_media_cache self.media_cache = os.path.exanduser(default_media_cache)
self._resume_file = '/tmp/pandora_client.%s.json' % os.environ.get('USER') self._resume_file = '/tmp/pandora_client.%s.json' % os.environ.get('USER')
def uploadVideo(self, filename, data, profile, info=None): def uploadVideo(self, filename, data, profile, info=None):
i = encode(filename, self.media_cache, profile, info i = encode(filename, self.media_cache, profile, info,
self._config['media'].get('importFrames')) self._config['media'].get('importFrames'))
if not i: if not i:
print "failed" print "failed"