Skip to content

Instantly share code, notes, and snippets.

@iwconfig
Last active February 25, 2019 10:46
Show Gist options
  • Save iwconfig/2f7312db1de333c53661dccf9b30796e to your computer and use it in GitHub Desktop.
Save iwconfig/2f7312db1de333c53661dccf9b30796e to your computer and use it in GitHub Desktop.
Using transmission-remote-gtk, visit the file or folder in your organized library. If item is not (yet?) organized, visit the origin instead (i.e. the 'Downloads' folder).
#!/usr/bin/env python3
# This script is supposed to be used with a remote torrent client,
# such as transmission-remote-gtk. Add this script as a local command
# and it will check if the file/dir has been sorted by filebot and if
# so, open the sorted file path. If not, open the path to the unsorted
# path (i.e. file/dir in 'Downloads' folder). It translates the path
# to correspond to the local mount point (samba share).To parse the
# xml file from filebot I use the untangle package because I'm lazy
# and because it's so easy to use. Install it with pip.
# pip install -U untangle
# I have also set up a passwordless authentication between my devices
# which suits this task well since I need to rsync the xml file from
# my remote server to my local computer.
# In transmission-remote-gtk I have set up the a local command:
# /path/to/this/script.py %{leftUntilDone} "%{full-path}" "%{name}"
# The script is very simple so it expects those arguments in that order.
import untangle
import os, sys, subprocess, tempfile
def translate_path(_):
if not _: return _
local_paths = {
"/volumeUSB1/usbshare/" : "/mnt/arkivet/usbshare1/",
"/volumeUSB2/usbshare/" : "/mnt/arkivet/usbshare2",
"/volume1/homes/snelhingst/" : "/mnt/arkivet/home/"
}
for a, b in local_paths.items():
_ = _.replace(a, b)
return _
args = sys.argv[1:]
if len(args) == 0:
msg = 'No arguments given'
print(msg)
sys.exit(1)
isComplete = int(args[0]) == 0
remoteFullPath = args[1]
localFullPath = translate_path(args[1])
baseName = args[2]
if isComplete is False:
if os.path.isdir(localFullPath):
subprocess.Popen(['xdg-open', '{}'.format(localFullPath)])
if os.path.isfile(localFullPath):
subprocess.Popen(['xdg-open', '{}'.format(os.path.dirname(localFullPath))])
sys.exit(0)
with tempfile.NamedTemporaryFile('w', encoding='utf8') as tmpf:
cmd = [
'/usr/bin/rsync',
'--rsync-path=/Apps/opt/bin/rsync',
'arkivet:/usr/local/filebot/data/sc-transmission/history.xml',
'{dest}'.format(dest=tmpf.name)
]
subprocess.run(cmd)
xml = untangle.parse(tmpf.name)
if os.path.isdir(localFullPath):
for sequence in xml.history.sequence:
for elem in sequence.rename:
if elem.get_attribute('dir') == remoteFullPath:
organizedLocalFullPath = translate_path(elem.get_attribute('to'))
subprocess.Popen(['xdg-open', '{}'.format(os.path.dirname(organizedLocalFullPath))])
sys.exit(0)
subprocess.Popen(['xdg-open', '{}'.format(os.path.dirname(localFullPath))])
sys.exit(0)
if os.path.isfile(localFullPath):
for sequence in xml.history.sequence:
for elem in sequence.rename:
if elem.get_attribute('from') == baseName:
organizedLocalFullPath = translate_path(elem.get_attribute('to'))
subprocess.Popen(['xdg-open', '{}'.format(os.path.dirname(organizedLocalFullPath))])
sys.exit(0)
subprocess.Popen(['xdg-open', '{}'.format(os.path.dirname(localFullPath))])
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment