#!/usr/bin/python # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 # GPL 2013 import os import sys import time from optparse import OptionParser import json import ox import pandora_client __version__ = '0.1' class Incoming(object): wait = 60 sleep = 5 def __init__(self, config): with open(config) as fd: self.config = json.load(fd) if not self.config['url'].endswith('/'): self.config['url'] += '/' self.api = pandora_client.API(self.config['url']); r = self.api.signin(username=self.config['username'], password=self.config['password']) if r['status']['code'] != 200: print 'failed to signin' print r sys.exit(1) def monitor(self): while True: for root, folders, files in os.walk(self.config['folder']): for f in files: f = os.path.join(root, f) st = os.stat(f) if st.st_mtime < time.mktime(time.localtime()) - self.wait: self.import_media(f) time.sleep(self.sleep) def import_media(self, f): filename = os.path.basename(f) defaults = self.config['defaults'] # try to load default values from tree folder = os.path.dirname(f) while len(folder) > len(self.config['folder']): defaults_json = os.path.join(folder, 'defaults.json') if os.path.exists(defaults_json): try: with open(defaults_json) as fd: defaults = json.load(fd) break except: pass folder = os.path.dirname(folder) # register file with pan.do/ra info = ox.avinfo(f) oshash = info['oshash'] if 'path' in info: del info['path'] r = self.api.addMedia({ 'id': oshash, 'filename': filename, 'info': info }) ''' # dont upload again if file is known if r['status']['text'] == 'file exists': os.unlink(f) return ''' # update item with default metadata item = r['data']['item'] item_info = {'id': item} item_info.update(defaults) self.api.edit(item_info) # upload media file url = '%supload/direct/' % self.config['url'] r = self.api.upload_chunks(url, f, { 'id': oshash }) # remove file after successfull upload if r: os.unlink(f) else: print f, 'failed' if __name__ == '__main__': usage = "usage: %prog -c config.json" parser = OptionParser(usage=usage) parser.add_option('-v', '--version', dest='version', action="store_true") parser.add_option('-c', '--config', dest='config', help='config.json with configuration information', default='', type='string') (opts, args) = parser.parse_args() if opts.version: print "%s %s" % (os.path.basename(sys.argv[0]), __version__) sys.exit(0) if not opts.config: parser.print_help() sys.exit(1) i = Incoming(opts.config) i.monitor()