-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-recent-movies.py
executable file
·91 lines (75 loc) · 2.67 KB
/
copy-recent-movies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/env python
RESOURCES=''
import urllib2,cookielib,base64,traceback,json,os,optparse,sys,datetime, shutil, client
from urllib import urlencode
from operator import itemgetter, attrgetter
RESOURCES=os.path.dirname(os.path.abspath(__file__))
def update_fields(item,fields):
for field in fields:
if not field in item:
item[field]=''
return item
usage="""%prog [options]
"""
parser = optparse.OptionParser(usage=usage)
parser.add_option('-u','--username',dest='username',default=None,help='authentication user')
parser.add_option('-p','--password',dest='password',default=None,help='authentication password')
parser.add_option('-s','--source',dest='source',default='http://localhost:8080/jsonrpc',help='source URI of XBMC [default=%default]')
parser.add_option('-t','--test',dest='test',action='store_true',default=False,help='only tests connection and prints list of movies')
(options,args) = parser.parse_args()
if len(args) != 1:
parser.error("Script needs exactly 1 argument")
sys.exit(1)
if not os.path.exists(args[0]):
print 'Output dir %s does not exist! '%args[0]
sys.exit(1)
reader = client.RPCClient(options.source,options.username,options.password)
print 'Getting recently added movies'
data = reader.get_recently_added_movies()
line = '---------------------------------'
print 'Done'
try:
for movie in data['movies']:
data['size'] = 0
movie = update_fields(movie,['label','file'])
if movie['file'].startswith('stack://'):
movie['file'] = movie['file'].replace('stack://','')
movie['files'] = movie['file'].split(',')
else:
movie['files'] = [movie['file']]
for file in movie['files']:
file = file.strip()
if os.path.exists(file):
data['size'] += os.path.getsize(file)
if options.test:
print 'List of recently added movies'
for movie in data['movies']:
print line
print (movie['label'])
for file in movie['files']:
print ' - %s'% file
if not os.path.exists(file):
print ' WARN: File does not exist'
print line
print line
print 'Total: %0.2f GB' % (data['size']/(1024*1024*1024.0))
print line
else:
print 'Total size to be copied : %0.2f GB' % (data['size']/(1024*1024*1024.0))
for movie in data['movies']:
print 'Copying movie %s '% (movie['label'])
for file in movie['files']:
if not os.path.exists(file):
print ' WARN: File \'%s\' does not exist' % file
else:
dirname = os.path.dirname(file).split('/')[-1]
dest = args[0]+'/'+dirname
print ' - Copying - %s to %s'% (file,dest)
os.chdir(args[0])
os.mkdir(dirname)
shutil.copy(file,dest)
# shutil.copy(RESOURCES+'/resources/jquery.tools.js',options.output)
print 'Done'
except:
traceback.print_exc()
sys.exit(1)