pandor_incoming

This commit is contained in:
j 2013-10-28 21:42:10 +01:00
commit 0cbc44bd5e
4 changed files with 149 additions and 0 deletions

24
README Normal file
View file

@ -0,0 +1,24 @@
pandora_incoming
to use on Ubuntu 12.04 or later you need to
install python-ox and pandora-client, i.e.
via the pandora ppa:
sudo add-apt-repository ppa:j/pandora
sudo apt-get update
sudo apt-get install python-ox pandora-client
CONFIG
copy config.json.example to config.json and set the
options for your pan.do/ra instance
RUN
to run pandora_incoming
./pandora_incoming.py -c config.json
DAEMON
to run pandora_incoming permanently via upstart,
adjust path and copy pandora_incoming.conf to /etc/init/

12
config.json.example Normal file
View file

@ -0,0 +1,12 @@
{
"username": "username",
"password": "password",
"url": "http://pandora.local/api/",
"folder": "/tmp/incoming",
"defaults": {
"project": "Example Project",
"topic": ["check metadata"],
"groups": ["test", "test2"]
}
}

13
pandora_incoming.conf Normal file
View file

@ -0,0 +1,13 @@
# pandora_incoming
#
description "pandora incoming"
start on runlevel [2345]
stop on runlevel [!2345]
kill timeout 5
respawn
setuid pandora
chdir /opt/pandora_incoming
exec /opt/pandora_incoming/pandora_incoming -c /opt/pandora_incoming/config.json

100
pandora_incoming.py Normal file
View file

@ -0,0 +1,100 @@
#!/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)
# 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(self.config['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()