📜 ⬆️ ⬇️

Shutdown at the end of all downloads Transmission

image
I love Transmission for its simplicity and ease of use. But what I would add is the ability to automatically turn off the computer at the completion of all downloads . I made this feature for myself under Ubuntu, but for those who will find it interesting, I think, they can freely remake it for another OS.

The task was solved by means of the RPC-JSON service , kindly provided by Transmission. To enable this feature you need in the Transmission before the tab "Web Interface" in the Settings check the box "Enable Web Interface". If you want to restrict access to the web panel from outsiders, you can set a password and username.

The first step is to install the module for python transmissionrpc . We get it:
hg clone www.bitbucket.org/blueluna/transmissionrpc
Note: The hg program is in the mercurial package. If it is not installed, you need to install it:
sudo apt-get install mercurial
After that, when the source of the module lies on the local computer, you need to install it by going to the transmissionrpc directory:
cd transmissionrpc
sudo python setup.py install

The module is installed, you can write the script itself. But, since our ultimate goal is to turn off the computer, which by default cannot be done without administrative authority, we will have to do a few more gestures.

So, our command to turn off the computer without asking for a password is ready for use.
Perhaps someone will find this solution a little crooked, then please make your suggestions.

And here is the script itself. I think in the comments to the script everything is clear.
Note. Since I used my own error handler, I had to file
/usr/local/lib/python2.6/dist-packages/transmissionrpc-0.5-py2.6.egg/transmissionrpc/transmission.py
make changes: lines 219-221 are commented out, although this is not critical.
')
  1. #! / usr / bin / python
  2. # - * - coding: utf-8 - * -
  3. # Pay attention to the second line-comment in the script. It allows you to avoid misunderstandings with a unicod encoding in the text of the script.
  4. # Import the necessary functions from the corresponding modules
  5. from time import localtime strftime sleep
  6. from sys import argv
  7. from os import geteuid, system
  8. from transmissionrpc import client
  9. from os.path import isfile
  10. # It is not necessary for the user to be an administrator. For then running Transmission as root is undesirable. And why is there admin power?
  11. if geteuid () == 0:
  12. print "\ 033 [1; 31mYou should not run this script as administrator! \ 033 [1; m"
  13. exit ()
  14. # Check if libnotify-bin is installed
  15. use_libnotify = isfile ( "/ usr / bin / notify-send" )
  16. # If not, then we will display a recommendation to do this for user-friendly notifications.
  17. if not use_libnotify:
  18. # Ask the user
  19. res = raw_input ( "\ 033 [1; 33mThe libnotify-bin package is not installed. You can install it with \ nsudo apt-get install libnotify-bin \ nDo it now? Type Y if you agree and press Enter: \ 033 [1; m " )
  20. if res in [ 'Y' , 'y' ]:
  21. # If he agrees, install the package
  22. res = system ( "sudo apt-get install -y libnotify-bin" )
  23. if res == 0:
  24. # I wonder if the package is installed?
  25. print "\ 033 [1; 32mPack libnotify-bin successfully installed! \ 033 [1; m"
  26. else :
  27. # It seems that the password is incorrectly entered or the problem with the package manager. Although, not the last reason.
  28. print "\ 033 [1; 31m Error installing libnotify-bin package. Try to do it yourself later. \ 033 [1; m"
  29. else :
  30. # Or do not install
  31. print "\ 033 [1; 33mIn this case, you can install it later or not install it at all. \ 033 [1; m"
  32. try :
  33. # Well, let's try to connect to the Transmission RPC service, using the appropriate parameters specified in its settings
  34. tc = Client ( 'localhost' , port = 9091, user = None, password = None)
  35. except:
  36. # Uh-uh .. Transmission, does not seem to be running?
  37. print "\ 033 [1; 31mError of connection to Transmission. Maybe it is not running or the web interface is not enabled in the program settings. \ 033 [1; m"
  38. # Inquire if you need to start the program now
  39. res = raw_input ( "\ 033 [1; 33mStart Transmission now? Enter Y if you agree and press Enter: \ 033 [1; m" )
  40. # So what did he put in there?
  41. if res in [ 'Y' , 'y' ]:
  42. # Well, run, if he agrees. Be sure to in the background!
  43. system ( "transmission &" )
  44. # and wait 5 seconds for it to load
  45. sleep (5)
  46. print "\ 033 [1; 32mTransmission successfully started. \ 033 [1; m"
  47. # Let’s try again to connect to the Transmission RPC service
  48. tc = Client ( 'localhost' , port = 9091, user = None, password = None)
  49. else :
  50. # We are leaving. Do not want what you want. Why then was it to run this script at all?
  51. print "\ 033 [1; 33mCompletion of the script. \ 033 [1; m"
  52. exit ()
  53. else :
  54. # If, after all, we have established a connection with Transmission, then we will rejoice about this user
  55. print "\ 033 [1; 32mConnection to Transmission established. \ 033 [1; m"
  56. # We will use the variable-flag of downloading to indicate whether at least one torrent is being downloaded. Initial value = True to start our validation cycle.
  57. downloading = true
  58. # While downloads are going (checking the downloading flag)
  59. while downloading:
  60. # And immediately reset it to False
  61. downloading = False
  62. # How many torrents?
  63. trc = len (tc.list ())
  64. # Clear the console for beautiful output
  65. system ( "clear" )
  66. # Colorize: \ 033 [brightness; color
  67. print "\ 033 [1; 36m ==== Download Status ==== \ 033 [1; m"
  68. # let's start going through all the torrents
  69. for i in range (1, trc):
  70. # Get information about the torrent with the index i
  71. torrent = tc.info (i) [i]
  72. # Default color
  73. col = "\ 033 [2; 37m"
  74. # Every man to his own taste!
  75. if torrent.status == "seeding" :
  76. col = "\ 033 [1; 36m"
  77. elif torrent.status == "downloading" :
  78. col = "\ 033 [1; 32m"
  79. elif torrent.status == "checking" :
  80. col = "\ 033 [1; 34m"
  81. print col + torrent.status + "(" + str ( "% .3g" % torrent.progress) + "%)" + torrent.name + "\ 033 [1; m"
  82. # Is it downloaded / checked? If not, this cycle will be interrupted, since the downloading flag will be set to False
  83. downloading = downloading or (torrent.status == "downloading" ) or (torrent.status == "checking" )
  84. print "\ 033 [1; 36m ==== Total torrents:" + str (trc) + "==== \ 033 [1; m"
  85. # Breathe before the next check.
  86. sleep (10)
  87. # Since we have reached here, it means downloading = False. Well, let's start the computer shutdown procedure.
  88. try :
  89. # If the libnotify-bin package is installed, let's say this to the user beautifully.
  90. if use_libnotify:
  91. system ( 'notify-send "All downloads are complete" "The system is shutting down. You can interrupt the script by pressing Ctrl + C in the script window." -i /usr/share/icons/gnome/scalable/actions/exit.svg' )
  92. # Or not quite beautiful ...
  93. print "\ 033 [1; 31mAll downloads are complete. The system is shutting down. You can interrupt the script by pressing Ctrl + C in the script window. \ 033 [1; m"
  94. # Give the user the last chance - 30 seconds to interrupt the script
  95. sleep (30)
  96. # if, after all, he interrupted him by pressing the key combination Ctrl + C
  97. except KeyboardInterrupt:
  98. # If the libnotify-bin package is installed, let's say this to the user beautifully.
  99. if use_libnotify:
  100. system ( 'notify-send "Shut down stopped" "Interrupted by user." -i /usr/share/icons/gnome/scalable/actions/redo.svg' )
  101. # Or not quite beautiful ...
  102. print "\ 033 [1; 31mCompletion stopped" "Interrupted by user. Exit script \ 033 [1; m"
  103. # We leave away from the script
  104. exit ()
  105. # And here it is, in fact, the function that initiates the shutdown of the system. Shutdown!
  106. system ( "/ usr / bin / shutdown -h now" )
  107. # If suddenly it will be interesting to the falling asleep user, when the script has started shutting down, open the file for this in the record mode
  108. f = open ( '/tmp/shutdownctl.time' , 'a' )
  109. # And write down our event there, specifying the time.
  110. f.write ( 'Shutdown was started at' + strftime ( "% a,% d% b% Y% H:% M:% S" , localtime ()) + '\ n' )
  111. # And finally, close the log file.
  112. f.close
  113. # That's all!
This code was highlighted with SkyManPHP .


If desired, you can finish it by using the documentation on the transmissionrpc module . Although, I believe that there are already such solutions on the Internet, even though I did not find them in such a way as I would like.
Therefore, I do not pretend to originality. And I don’t write in Python at all - I’ve spied on some commands in Google. But if anyone needs it, I will be happy.

Source: https://habr.com/ru/post/93004/


All Articles