From 2099b337bcf7d5fc0d97a5b6b01b6d6578314576 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 29 Sep 2019 13:19:36 -0700 Subject: [PATCH] handle duplicates --- mediaweb/__init__.py | 47 ++++++++++++++++++++++------- requirements.txt | 1 - templates/sortform_done.html | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 templates/sortform_done.html diff --git a/mediaweb/__init__.py b/mediaweb/__init__.py index 927fa22..81361a8 100644 --- a/mediaweb/__init__.py +++ b/mediaweb/__init__.py @@ -10,6 +10,7 @@ from dataclasses import dataclass, field from deluge_client import DelugeRPCClient from jinja2 import Environment, FileSystemLoader, select_autoescape from mediaweb import shows +from enum import Enum APPROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../")) @@ -28,6 +29,12 @@ class Client: pathmap: (str, str) +class SortResult(Enum): + OK = 0 + EXISTED = 1 + FAIL = 2 + + class ClientCache(object): def __init__(self, options, libpath): self.options = options @@ -193,15 +200,16 @@ class MediaWeb(object): thematch = m break - self.execute_move(tkey, torrent, thematch) + result, reason = self.execute_move(tkey, torrent, thematch) self.cache.refresh() - # TODO summary display - return "OK" + return self.render("sortform_done.html", success_torrents=[[torrent, result, reason]], failed_torrents=[]) return self.render("sortform.html", torrent=torrent, matches=matches, tkey=tkey, score=score) def execute_move(self, tkey, torrent, match): + result = SortResult.OK + # resolve the pathmap thash, client = self.cache.client(tkey) @@ -216,9 +224,21 @@ class MediaWeb(object): local_torrent_path = os.path.join(pmap[1], client_full_path[len(pmap[0]):].lstrip("/")) # our perspective's path to the file local_library_path = os.path.join(self.options["library_path"], in_library_path) # where we will place the file in the library - # hard link into library - os.link(local_torrent_path, local_library_path) - print(f"os.link('{local_torrent_path}', '{local_library_path}')") + # check if the dest file already exists: + if os.path.exists(local_library_path): + # if the src and dest are already linked to the same file, this is a noop + if os.stat(local_torrent_path).st_ino == os.stat(local_library_path).st_ino: + result = SortResult.EXISTED + logging.info("dest exists, skipping linking %s -> %s", local_torrent_path, local_library_path) + else: + return SortResult.FAIL, "destination file already exists and has different contents" + else: + # hard link into library + showdir = os.path.dirname(local_library_path) + if not os.path.exists(showdir): + os.makedirs(showdir) + os.link(local_torrent_path, local_library_path) + logging.info("linking %s -> %s", local_torrent_path, local_library_path) client_stashdir = os.path.join(pmap[0], self.options["stashprefix"], @@ -230,20 +250,26 @@ class MediaWeb(object): # label torrent as sorted client.rpc.label.set_torrent(torrent["hash"], self.options["label_done"]) + return result, None + @cherrypy.expose def autosort(self, tkeys): if not isinstance(tkeys, list): tkeys = [tkeys] + results = [] + for tkey in tkeys: thash, client = self.cache.client(tkey) torrent = client.rpc.core.get_torrent_status(thash, []) # TODO reduce to needed fields - self.execute_move(tkey, torrent, self.cache.moves[tkey]) + res = self.execute_move(tkey, torrent, self.cache.moves[tkey]) + results.append([torrent, res[0], res[1]]) self.cache.refresh() - # TODO summary display of results - return f"autosorted: {repr(tkeys)}" + return self.render("sortform_done.html", + success_torrents=[r for r in results if r[1] != SortResult.FAIL], + failed_torrents=[r for r in results if r[1] == SortResult.FAIL]) @cherrypy.expose def modify(self, action, tkey): @@ -321,7 +347,8 @@ def main(): tpl = Environment(loader=FileSystemLoader(tpl_dir), autoescape=select_autoescape(['html', 'xml'])) tpl.filters.update(tsortbyname=tsortbyname, - len=len, ) + len=len, + jsond=json.dumps) def validate_password(realm, user, passw): return user == passw # lol diff --git a/requirements.txt b/requirements.txt index f864c3c..88b5eaa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,6 @@ fuzzywuzzy==0.17.0 jaraco.functools==2.0 Jinja2==2.10.1 MarkupSafe==1.1.1 -mediasortweb==0.0.0 more-itertools==7.2.0 portend==2.5 pytz==2019.2 diff --git a/templates/sortform_done.html b/templates/sortform_done.html new file mode 100644 index 0000000..8e4909d --- /dev/null +++ b/templates/sortform_done.html @@ -0,0 +1,58 @@ +{% extends "page.html" %} +{% block toolbar %} +Home +{% endblock %} + +{% block main %} +
+

Sorted

+
+
+

Success

+ + + + + + {% for torrent, result, reason in success_torrents %} + + + + + {% endfor %} +
torrentresult
+ {{ torrent.name }} +
    + {% for f in torrent.files %} +
  • {{ f.path }} - {{ f.size }} B
  • + {% endfor %} +
+
{{ result }}{% if reason %} - {{ reason }}{% endif %}
+ +

Failures

+ + + + + + {% for torrent, result, reason in failed_torrents %} + + + + + {% endfor %} +
torrentresult
+ {{ torrent.name }} +
    + {% for f in torrent.files %} +
  • {{ f.path }} - {{ f.size }} B
  • + {% endfor %} +
+
{{ result }}{% if reason %} - {{ reason }}{% endif %}
+
+ +{% if not failed_torrents %} + +{% endif %} + +{% endblock %}