mediasort/mediaweb/__init__.py

220 lines
7.4 KiB
Python
Raw Normal View History

2019-08-17 10:25:11 -07:00
import os
import logging
2019-08-17 12:50:46 -07:00
import cherrypy
2019-08-17 10:50:31 -07:00
from time import sleep
from queue import Queue
2019-08-17 12:50:46 -07:00
from pprint import pprint
from threading import Thread
from urllib.parse import urlparse
2019-08-17 10:50:31 -07:00
from dataclasses import dataclass, field
2019-08-17 12:50:46 -07:00
from deluge_client import DelugeRPCClient
from jinja2 import Environment, FileSystemLoader, select_autoescape
from mediaweb import shows
2019-08-17 10:25:11 -07:00
APPROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
2019-08-17 10:50:31 -07:00
@dataclass
class Cache:
torrents: dict = field(default_factory=dict)
2019-08-17 12:50:46 -07:00
shows: dict = field(default_factory=dict)
2019-08-17 10:50:31 -07:00
class ClientCache(object):
2019-08-17 12:50:46 -07:00
def __init__(self, client, libpath):
2019-08-17 10:50:31 -07:00
self.client = client
self.data = Cache()
self.q = Queue()
2019-08-17 12:50:46 -07:00
self.inflight = False
self.libpath = libpath
2019-08-17 10:50:31 -07:00
self.background_t = Thread(target=self.background, daemon=True)
self.background_t.start()
self.timer_t = Thread(target=self.timer, daemon=True)
self.timer_t.start()
def refresh(self):
2019-08-17 12:50:46 -07:00
if not self.inflight and self.q.qsize() == 0: # best effort duplicate work reduction
self.q.put(None)
2019-08-17 10:50:31 -07:00
def background(self):
while True:
self.q.get() # block until we need to do something
2019-08-17 12:50:46 -07:00
self.inflight = True
2019-08-17 10:50:31 -07:00
logging.info("performing background tasks...")
2019-08-17 12:50:46 -07:00
self.build_showindex()
self.build_torrentindex()
2019-08-17 10:50:31 -07:00
self.q.task_done()
2019-08-17 12:50:46 -07:00
self.inflight = False
2019-08-17 10:50:31 -07:00
logging.info("background tasks complete")
def timer(self):
while True:
self.refresh()
logging.info("sleeping...")
sleep(300) # TODO configurable task interval
2019-08-17 12:50:46 -07:00
def build_torrentindex(self):
logging.info("refreshing torrents")
self.data.torrents = self.client.core.get_torrents_status({"label": "sickrage"},
['name', 'label', 'save_path', 'is_seed',
'is_finished', 'progress'])
def build_showindex(self):
logging.info("updating show index")
data = shows.create_index([self.libpath])
self.data.shows = sorted(data, key=lambda x: x.name)
2019-08-17 10:50:31 -07:00
2019-08-17 10:25:11 -07:00
class MediaWeb(object):
def __init__(self, rpc, templater, uioptions):
self.tpl = templater
self.rpc = rpc
self.uioptions = uioptions
def render(self, template, **kwargs):
"""
Render a template
"""
2019-08-17 12:50:46 -07:00
return self.tpl.get_template(template).render(**kwargs,
options=self.uioptions,
torrents=self.rpc.data.torrents,
shows=self.rpc.data.shows,
**self.get_default_vars())
2019-08-17 10:25:11 -07:00
def get_default_vars(self):
return {}
@cherrypy.expose
2019-08-17 10:50:31 -07:00
def index(self, action=None):
if action:
if action == "update":
self.rpc.refresh()
raise cherrypy.HTTPRedirect("/")
2019-08-17 12:50:46 -07:00
return self.render("index.html")
2019-08-17 10:25:11 -07:00
@cherrypy.expose
def move(self, thash, dest=None, otherdest=None):
2019-08-17 10:50:31 -07:00
torrent = self.rpc.client.core.get_torrent_status(thash, []) # TODO reduce to needed fields
2019-08-17 10:25:11 -07:00
if cherrypy.request.method == "POST" and (dest or otherdest):
target = otherdest or dest
2019-08-17 10:50:31 -07:00
self.rpc.client.core.move_storage([thash], target)
self.rpc.refresh()
2019-08-17 10:25:11 -07:00
raise cherrypy.HTTPRedirect("/")
return self.render("moveform.html", torrent=torrent)
2019-08-17 12:50:46 -07:00
@cherrypy.expose
def sort(self, thash, dest=None):
torrent = self.rpc.client.core.get_torrent_status(thash, []) # TODO reduce to needed fields
# find the actual file among the torrent's files
# really we just pick the biggest one
finfo = None
fsize = 0
for tfile in torrent["files"]:
if tfile["size"] > fsize:
finfo = tfile
fname = finfo["path"]
matches = shows.match_episode(fname, self.rpc.data.shows)
if cherrypy.request.method == "POST" and dest:
thematch = None
for m in matches:
if m.dest.dir == dest:
thematch = m
break
return f"sort {fname} into {thematch}"
return self.render("sortform.html", torrent=torrent, matches=matches)
2019-08-17 10:25:11 -07:00
def main():
import argparse
import signal
parser = argparse.ArgumentParser(description="mediaweb server")
parser.add_argument('-p', '--port', help="tcp port to listen on",
default=int(os.environ.get("MEDIAWEB_PORT", 8080)), type=int)
parser.add_argument('-o', '--library', default=os.environ.get("STORAGE_URL"), help="media library path")
parser.add_argument('--debug', action="store_true", help="enable development options")
parser.add_argument('-s', '--server', help="deluge uris", action="append", required=True)
parser.add_argument('--ui-movedests', help="move destination options in the UI", nargs="+", required=True)
args = parser.parse_args()
uioptions = {
"movedests": args.ui_movedests,
}
# TODO smarter argparser that understands env vars
if not args.library:
2019-08-17 12:50:46 -07:00
parser.error("--library or MEDIAWEB_DLDIR is required")
2019-08-17 10:25:11 -07:00
logging.basicConfig(level=logging.INFO if args.debug else logging.WARNING,
format="%(asctime)-15s %(levelname)-8s %(filename)s:%(lineno)d %(message)s")
2019-08-17 12:50:46 -07:00
tpl_dir = os.path.join(APPROOT, "templates")
2019-08-17 10:25:11 -07:00
tpl = Environment(loader=FileSystemLoader(tpl_dir),
autoescape=select_autoescape(['html', 'xml']))
# self.tpl.filters.update(basename=os.path.basename,
# ceil=math.ceil
def validate_password(realm, user, passw):
return user == passw # lol
# assume 1 deluge server for now
uri = urlparse(args.server[0])
assert uri.scheme == "deluge"
rpc = DelugeRPCClient(uri.hostname, uri.port if uri.port else 58846, uri.username, uri.password, decode_utf8=True)
2019-08-17 12:50:46 -07:00
rpc_cache = ClientCache(rpc, args.library)
2019-08-17 10:50:31 -07:00
web = MediaWeb(rpc_cache, tpl, uioptions)
2019-08-17 10:25:11 -07:00
cherrypy.tree.mount(web, '/', {'/': {'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'mediaweb',
'tools.auth_basic.checkpassword': validate_password, }})
# General config options
cherrypy.config.update({
'tools.sessions.on': False,
'request.show_tracebacks': True,
'server.show_tracebacks': True,
'server.socket_port': args.port,
'server.thread_pool': 1 if args.debug else 5,
'server.socket_host': '0.0.0.0',
'log.screen': False,
'engine.autoreload.on': args.debug
})
# Setup signal handling and run it.
def signal_handler(signum, stack):
logging.critical('Got sig {}, exiting...'.format(signum))
cherrypy.engine.exit()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
try:
cherrypy.engine.start()
cherrypy.engine.block()
finally:
logging.info("API has shut down")
cherrypy.engine.exit()
if __name__ == '__main__':
main()
# https://github.com/deluge-torrent/deluge/blob/1.3-stable/deluge/ui/console/commands/info.py#L46
# https://deluge.readthedocs.io/en/latest/reference/api.html?highlight=rpc