This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
173 lines (156 loc) · 5.81 KB
/
manage.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys, os, shutil, requests
from flask.ext.script import Manager
from kodimediacopy import app, db
from kodimediacopy.models import User, Posters, FileCopy
from kodimediacopy.kodimodels import Movie, Tvshow
manager = Manager(app)
@manager.option('-n', '--name', dest='name', default='admin')
def adduser(name):
"""
creates new users
for each new user a new uuid is generated for login
"""
print 'adding user %s ' % name
if User.query.filter_by(username=name).first() is not None:
print 'user %s exists allready' % name
return
user = User(name)
db.session.add(user)
db.session.commit()
print 'user %s created' % name
@manager.option('-n', '--name', dest='name', default=None)
def deleteuser(name):
"""
delete given user
"""
if name is None:
print 'no user specified'
return
print 'deleting user %s ' % name
user = User.query.filter_by(username=name).first()
if user is None:
print 'user %s does not exist' % name
return
db.session.delete(user)
db.session.commit()
print 'user %s deleted' % name
@manager.option('-u', '--url', dest='url', default='http://127.0.0.1:5000/login/')
def listuser(url):
"""
lists all users in the User table
including their login url
param url is the base login url like http://host.tld:port/login/
"""
print 'listing users:'
users = User.query.all()
for user in users:
print 'name: %s | login_url: %s' % (user.username, url+user.uuid)
@manager.command
def initdb():
"""
recreate tables from models.py
bind=None only recreates the tables for the default sqlalchemy connection
bind based connections are not altered
( we dont want sqlalchemy to mess with kodi`s media database)
"""
db.drop_all(bind=None)
db.create_all(bind=None)
adduser('admin')
@manager.command
def updateart():
import urllib
from imdbpie import Imdb
imdb = Imdb()
print 'updating art for movies(imdb cover)'
for movie in Movie.query.all():
print 'processing %s' % movie.c00
imdbid = movie.c09
if Posters.query.filter_by(apiid=imdbid).first() is not None:
print 'skipping %s as it is allready in the database' % movie.c00
continue
try:
title = imdb.get_title_by_id(imdbid)
if title.cover_url is None:
continue
poster = Posters()
poster.apiid = imdbid
poster.type = 'movie'
response = urllib.urlopen(title.cover_url)
data = response.read()
data64 = data.encode('base64').replace('\n', '')
poster.imgdata = 'data:image/jpeg;base64,%s' % data64
# print poster.imgdata
db.session.add(poster)
db.session.commit()
except:
continue
print 'updating art for tv'
from tvdb_api import Tvdb
t = Tvdb(banners=True)
for show in Tvshow.query.all():
print 'processing %s' % show.c00
tvdbid = show.c12
if Posters.query.filter_by(apiid=tvdbid).first() is not None:
print 'skipping %s as it is allready in the database' % show.c00
continue
try:
tvdbshow = t[int(tvdbid)]
bannerkeys = tvdbshow['_banners']['season']['season'].keys()
banner_url = tvdbshow['_banners']['season']['season'][bannerkeys[0]]['_bannerpath']
poster = Posters()
poster.apiid = tvdbid
poster.type = 'tv'
response = urllib.urlopen(banner_url)
data = response.read()
data64 = data.encode('base64').replace('\n', '')
poster.imgdata = 'data:image/jpeg;base64,%s' % data64
# print poster.imgdata
db.session.add(poster)
db.session.commit()
except:
continue
@manager.option('-d', '--directory', dest='directory', default='/mnt/usb/mediacopy/')
def copyfiles(directory):
for user in User.query.all():
print 'processing files for user: %s' % user.username
basepath = os.path.join(directory, user.username)
if not os.path.exists(basepath):
os.mkdir(basepath)
for filecopy in FileCopy.query.filter_by(userid=user.id).all():
filepath = os.path.join(filecopy.filepath, filecopy.filename)
print 'copying file: %s' % filepath
try:
shutil.copy(filepath, basepath)
except:
continue
filecopy.copied = True
db.session.add(filecopy)
db.session.commit()
@manager.option('-d', '--directory', dest='directory', default='/mnt/usb/mediacopy/')
@manager.option('-u', '--url', dest='url', default='http://127.0.0.1:5000')
def copyfiles_remote(directory, url):
get_url = url+'/api/filecopy'
update_url = url+'/api/filecopy/setcopied/'
if 'ADMIN_TOKEN' not in app.config:
print 'ADMIN_TOKEN not set.. aborting'
return
print 'fetching stuff from %s' % get_url
response = requests.get(get_url, headers={'auth_token':app.config['ADMIN_TOKEN']})
if not response.status_code == 200:
print 'got http status %s .. something went wrong' %response.status_code
return
result = response.json()['results']
for filecopy in result:
basepath = os.path.join(directory, filecopy['username'])
if not os.path.exists(basepath):
os.mkdir(basepath)
filepath = os.path.join(filecopy['filepath'], filecopy['filename'])
print 'copying file: %s' % filepath
try:
shutil.copy(filepath, basepath)
except OSError as why:
print 'error: %s' % why.message
continue
requests.get(update_url+str(filecopy['id']), headers={'auth_token':app.config['ADMIN_TOKEN']})
if __name__ == "__main__":
manager.run()