Page 1 of 1

Debugging utorrent-postprocess.pyw

Posted: 08 Jul 2017, 11:38
by methos237
I am trying to get this working, and it is just not happening. The script executes from utorrent, but the files aren't renamed and nothing is written to amc.log. I'm on Windows 10 x64 and my .pyw file is as follows:

Code: Select all

# Run Program: 
# "C:\path\to\utorrent-postprocess.pyw" "%L" "%S" "%N" "%K" "%F" "%D"
#
# Test with Console:
# python "C:\path\to\utorrent-postprocess.pyw" "Movie" "5" "Avatar" "multi" "" "X:\Files\Avatar"


import sys
import subprocess


# configuration
output = 'D:\!Sorted_Downloads'


# custom formats
movieFormat  = '''D:\!Sorted_Downloads\Movies\{ny}\{ny}{' CD'+pi}{subt}'''
seriesFormat = '''D:\!Sorted_Downloads\TV Shows\{n.replaceTrailingBrackets()}\{'Season '+s.pad(2)}\{n.replaceTrailingBrackets()} - {s00e00} - {t.replaceAll(/[!?.]+$/).replaceAll(/[`´‘’?]/, "'") .lowerTrail().replacePart(', Part $1')}'''
animeFormat  = '''{plex}'''
musicFormat  = '''{plex}'''


# required args
label, state, title, kind, file, directory = sys.argv[1:7]


command = [
	'filebot', '-script', 'fn:amc',
	'--output', output,
	'--action', 'move',
	'--conflict', 'auto',
	'-non-strict',
	'--log-file', output + '/amc.log',
	'--def',
		'ut_state_allow=11',
		'unsorted=y',
		'music=y',
		'artwork=n',
		'movieFormat='  + movieFormat,
		'seriesFormat=' + seriesFormat,
		'animeFormat='  + animeFormat,
		'musicFormat='  + musicFormat,
		'ut_label=' + label,
		'ut_state=' + state,
		'ut_title=' + title,
		'ut_kind='  + kind,
		'ut_file='  + file,
		'ut_dir='   + directory
]


# execute command (and hide cmd window)
subprocess.run(command, creationflags=0x08000000)
Here is the output from utorrent:

Code: Select all

[2017-07-08 07:20:34]  Executing: "D:\!Sorted_Downloads\utorrent-postprocess.pyw" "" "11" "Silence 2016 1080p WEB-DL x264 AC3-JYK" "multi" "Subs\English.srt" "D:\Downloads\Silence 2016 1080p WEB-DL x264 AC3-JYK"

Re: [Windows] Suppress CMD window

Posted: 08 Jul 2017, 12:25
by rednoah
1.
Please open CMD and then run your script:

Code: Select all

python "D:\!Sorted_Downloads\utorrent-postprocess.pyw" "" "11" "Silence 2016 1080p WEB-DL x264 AC3-JYK" "multi" "Subs\English.srt" "D:\Downloads\Silence 2016 1080p WEB-DL x264 AC3-JYK"
(we run the python script with python.exe instead of pythonw.exe so we can see the console output)


2.
What does the output say?

Re: [Windows] Suppress CMD window

Posted: 08 Jul 2017, 15:33
by methos237
OK, so the first error I got was:

Code: Select all

C:\Users\James>python "D:\!Sorted_Downloads\utorrent-postprocess.pyw" "" "11" "Silence 2016 1080p WEB-DL x264 AC3-JYK" "multi" "Subs\English.srt" "D:\Downloads\Silence 2016 1080p WEB-DL x264 AC3-JYK"
  File "D:\!Sorted_Downloads\utorrent-postprocess.pyw", line 18
SyntaxError: Non-ASCII character '\xb4' in file D:\!Sorted_Downloads\utorrent-postprocess.pyw on line 18, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
So I changed

Code: Select all

seriesFormat = '''D:\!Sorted_Downloads\TV Shows\{n.replaceTrailingBrackets()}\{'Season '+s.pad(2)}\{n.replaceTrailingBrackets()} - {s00e00} - {t.replaceAll(/[!?.]+$/).replaceAll(/[`´‘’?]/, "'") .lowerTrail().replacePart(', Part $1')}''''
back to

Code: Select all

seriesFormat = '''{plex}'''

Then I tried again and got:

Code: Select all

C:\Users\James>python "D:\!Sorted_Downloads\utorrent-postprocess.pyw" "" "11" "Silence 2016 1080p WEB-DL x264 AC3-JYK" "multi" "Subs\English.srt" "D:\Downloads\Silence 2016 1080p WEB-DL x264 AC3-JYK"
Traceback (most recent call last):
  File "D:\!Sorted_Downloads\utorrent-postprocess.pyw", line 53, in <module>
    subprocess.run(command, creationflags=0x08000000)
AttributeError: 'module' object has no attribute 'run'
After doing some Google-fu, I found that the run () subprocess module wasn't added until Python 3.5 and I was using 2.7.13. So I updated to 3.6.2rc2, and got the file not found gibberish as I had already renamed the file manually. So I tried a new download and everything worked like a charm!

Now if I could figure out why it doesn't work in utorrent when I put it in the "Run this program when a torrent finishes" box, I'll be set. Right now I have it in the "Run this program when a torrent changes state" box (hence the need for the "ut_state_allow=11" -def) and it tries running the script 5 times for each torrent. It may be an issue with DuckieTV stopping torrents on completion, so I'll have to investigate further.

Thank you very much for pointing me in the right troubleshooting direction!!

Re: Debugging utorrent-postprocess.pyw

Posted: 08 Jul 2017, 15:49
by rednoah
If you decide whether or not to call the amc script based on the torrent state, then I would highly recommend making that check in the python script instead of always calling filebot/amc just to have it abort right away. That would save your CPU a lot of unnecessary work.

Re: Debugging utorrent-postprocess.pyw

Posted: 08 Jul 2017, 16:36
by methos237
rednoah wrote: 08 Jul 2017, 15:49 If you decide whether or not to call the amc script based on the torrent state, then I would highly recommend making that check in the python script instead of always calling filebot/amc just to have it abort right away. That would save your CPU a lot of unnecessary work.
What do you mean? I'm trying with an if/else statement, so let me know if this looks right to you:

Code: Select all

#...
# required args
label, state, title, kind, file, directory = sys.argv[1:7]


command = [
#...
]
if state == '11':
# execute command (and hide cmd window)
	subprocess.run(command, creationflags=0x08000000)
else:
	sys.exit
	
I'm running it and I think it's working right...

Re: Debugging utorrent-postprocess.pyw

Posted: 09 Jul 2017, 02:49
by rednoah
Pretty much. You can always test it in the shell. If the script returns within milliseconds then it's working and filebot isn't called. Because java / filebot / groovy / amc is a mighty beast that takes quite a bit longer to initialize and run. ;)

Re: Debugging utorrent-postprocess.pyw

Posted: 09 Jul 2017, 08:01
by methos237
Well it seems to be working great: 4 torrents downloaded and only 4 entries in amc.log, with all showing "done". Thank you for all your help Rednoah!!