Thursday, December 20, 2012

Automatically download subtitles and shows

For those of You who love to watch TV shows downloaded from internet .. but hate the procedure of finding the correct torrent file and subtitles I will present a way to automate the whole process.

What we will need:
  1. Utorrent client:  http://www.utorrent.com/
  2. Python 2.7.3 http://www.python.org/download/releases/2.7.3/
  3. BeautifulSoup 3.2.1 (Python library)
      http://www.crummy.com/software/BeautifulSoup/
      http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-3.2.1.tar.gz
  4. Periscope (Python module)
    http://code.google.com/p/periscope/

    python-periscope_0.2.4.orig.tar.gz
    (Or use my  python-periscope_0.2.4.orig.plirkee.repack)
 Target System: (in my case) Windows Vista
 Let's begin.

  •  Utorrent (briefly, cause you can find information on how to do this on many sites)
  1.  First you install Utorrent and open it.
  2.  Right click on Feeds and add  new Rss feed
    (it could be any rss feed of your favorite site mine is EzTv so I add  rss feed with feed url:  http://feeds.feedburner.com/eztv-rss-atom-feeds?format=xml )
  3. Go to Options => Rss downloader and Add new rule for your favorite show...
And thus You  are ready to  automatically download  your shows as soon as they are available! .... but, we will be back to utorrent settings to make it download subtitles as well, however first we need to set up python

  • Python
  1. install python to c:\python27
  2. download BeautifulSoup, unzip it into c:\python27\BeautifulSoup,
    run form command prompt: 
    cd c:\python27\BeautifulSoup\
    setup.py install
  3. download Periscope, unzip it into c:\python27\Periscope,
    run from command prompt:
    cd c:\python27\Periscope\
    setup.py install
  4. go to your user's folder (C:\Users\USERNAME) (this folder is defined ether by %HOME% or %USERPROFILE% or %HOMEDRIVE%%HOMEPATH% env variables) from command prompt and create directory with name ".config" e.g. 
    • cmd
    • cd C:\Users\USERNAME
    • mkdir .config
          (if you omit this step, you could get access denied error from periscope script)   
  • Vb Script
  1. create .vbs file that will handle subtitle downloading


  • c:\MyScripts\download.vbs
  • 'In v5 uTorrent kind (%K) support is added kind can be single  or multi
    Const RUN = "C:\Python27\python.exe C:\Python27\scripts\periscope"  
    Const LANGS = "-l el -l en"
    Const LOGFILE_POSTFIX = "_downloader.log"
    Const SUB_EXT = "srt"
    Const EXPECETED=3 ' utorrent pause code
    Const FORAPPENDING = 8
    Const LEAST_NUM_OF_ARG=3 ' (0) = Directory  (1) = File (2) = Kind + optional (3) = Status 
    Const KIND_MULTI = "multi"
    
    
    On Error Resume Next
    Dim directory,file,kind,extension,whatToRun 
    set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Function log(logfile,str)
     set objFile = objFSO.OpenTextFile(logfile, FORAPPENDING, True)
     objFile.WriteLine("Time Stamp: " & Now)
     objFile.WriteLine(" " & str)
     objFile.Close
     set objFile=Nothing
    End Function
    
    if ( WScript.Arguments.Count LEAST_NUM_OF_ARG) then 
     if (WScript.Arguments.Count=LEAST_NUM_OF_ARG+1 and WScript.Arguments(LEAST_NUM_OF_ARG)<>CStr(EXPECETED)) then  
      'Wscript.Echo "Arg3 unexpected value" &  WScript.Arguments(2) &" " & EXPECETED
      log logfile,WScript.Arguments(1) & "Arg3 unexpected value " &  WScript.Arguments(2) &" was expecting " & EXPECETED
      WScript.quit()
     end if
     end if
     
     extension = Right(file,3)
     if kind=KIND_MULTI then
        whatToRun = RUN & " " & LANGS &" " & chr(34) &directory&           chr(34)
     else 
        whatToRun = RUN & " " & LANGS &" " & chr(34) &directory&"\" &file& chr(34) 
     if (extension<>"mp4" and extension<>"avi" and extension<>"mkv") then 
      'Wscript.Echo "unsupported extension:" &  extension
      log logfile,"unsupported extension:" &  extension
      WScript.quit()
     end if 
     end if
     
      log logfile,"Will try " & file 
      Set WshShell = WScript.CreateObject("WScript.Shell") 
      'WshShell.Run "cmd.exe /c start /min  " 
      WshShell.Run "cmd.exe /c  " &_ 
                   " echo %date% %time% " &_
          directory & " " & file & whatToRun & " >> " & logfile & " &" &_
          whatToRun &  " >> "&logfile&"  2>&1"&_
          " & exit",0,True  
       newFile = left(file,len(file)-3)&SUB_EXT
      'Wscript.Echo(newFile)
       If objFSO.FileExists(chr(34) & directory&"\"&newFile &chr(34)) Then
       'Wscript.Echo("Downloaded subtitles for " & file)
        End If 
      
      set objFSO=nothing
      Set WshShell=nothing
    




  • Set  Utorrent - Preferences - Advanced - Run Program :
    Run This program when torrent finishes
    c:\MyScripts\download.vbs "%D" "%F" %K
    Run When torrent  changes state
    c:\MyScripts\download.vbs "%D" "%F" %K %S

  •  Of course you could create your own .bat file as I did  at first. In that case I would recommend you to call it with following command: "cmd.exe /c start /min  C:\MyPythonSubsDowloader.bat "%D" "%F" %K %S ^& exit", from utorrent. The downside of using bat file is the flickering of dos window that appears every time torrent state changes.
     Also, as you can see in vbs, the  script tries to download Greek and  English subs [Const LANGS = "-l el -l en"],  you can add your own languages by changing that line.

     Any way, we have finished our task. Now any time video has finished downloading ..or whenever you hit pause on downloaded video in utorrent - subtitle search is performed.
    I use utorrent pause (state code = 3) to cause downloading of subtitles at time of my choosing ...and use it in cases when no subs are found, the first time  the torrent finished.
     
    P.S. Also, it seems that currently there are some problems with a number of plugins of periscope (v 0.2.4) .. but you can download my fixes for Addic7ed and TVsubtitles. If you do, then don't forget to un-comment  these plugins in periscope\plugins\__init__.py before executing  setup.py install 

    P.S2 I upgraded the download vb script to v5 (24/07/2013) now it downloads subs even if you are getting muti-item torrent (e.g. complete season). The old version of the script is still available @ my  support site.
    From there you can download my repack of periscope which includes all  the above fixes.