Advertisement
diyfuturism

torrentcleaner.py

Dec 1st, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.94 KB | None | 0 0
  1. #!/usr/bin/python
  2. #
  3. # Transmission RPC Reference
  4. # http://pythonhosted.org/transmissionrpc/reference/transmissionrpc.html
  5. # Command spec:
  6. # https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt
  7. #
  8. # transmission-fluid
  9. # http://pythonhosted.org/transmission-fluid/
  10.  
  11.  
  12. #
  13. # Script pulls list of torrents
  14. # Apollo/Waffles torrents in the downloads/complete/torrents folder
  15. # Get filed to seeds/apollo or seeds/waffles
  16. # Also copied to to_process (for headphones?)
  17. #
  18. # Ru torrents are seeded to 1:1 and then removed + delete files
  19. #
  20. # Public torrents are removed and left in downloads/complete/torrents
  21. #
  22.  
  23. from transmission import Transmission
  24. import shutil
  25. import os
  26.  
  27. #
  28. # Connect and retrieve torrents
  29. #
  30. client = Transmission(host='localhost',username='root',password='XXXXX')
  31. response = client('torrent-get',fields=['id','name','percentDone','seedRatioMode','uploadRatio','downloadDir','trackers'])
  32.  
  33. doneTorrents = 0;
  34. moveTorrents = 0;
  35. removeTorrents = 0;
  36. ruDeleted = 0;
  37. ruSeeding = 0;
  38. totalTorrents = len(response['torrents'])
  39.  
  40.  
  41. loop = 0;
  42. print "Connecting to Transmission to check torrents..."
  43. while (loop < totalTorrents):
  44.     currentTorrent = response['torrents'][loop];
  45.  
  46.     # DO THIS FOR ALL DONE TORRENTS
  47.     if (currentTorrent['percentDone'] == 1):
  48.         doneTorrents = doneTorrents + 1;
  49.  
  50.         #
  51.         # SORT TORRENTS BY TRACKER
  52.         #
  53.  
  54.         # APOLLO
  55.         # ACTION: put in seeds/apollo, copy to to_process
  56.         if "apollo" in currentTorrent['trackers'][0]['announce']:
  57.             # If it's in the download folder...
  58.             if "/mnt/usb1/downloads/complete/torrents" in currentTorrent['downloadDir']:
  59.                 torrentSource =  currentTorrent['downloadDir'] + "/" + currentTorrent['name']
  60.                 torrentprocessDestination = "/mnt/usb1/to_process/" + currentTorrent['name']
  61.                 torrentseedDestination = "/mnt/usb1/seeds/apollo/" + currentTorrent['name']
  62.  
  63.                 folderCorrect = False;
  64.                 moveTorrents = moveTorrents + 1;
  65.             # Already been moved, all good
  66.             else:
  67.               folderCorrect = True;
  68.  
  69.         # RUTRACKER
  70.         # ACTION: IF new, put in seeds/rutracker, copy to_process, set ratio limit
  71.         #         ELSE IF ratio 1:1 then remove it
  72.         elif "ru" in currentTorrent['trackers'][0]['announce']:
  73.             # If it's in the download folder...
  74.             if "/mnt/usb1/downloads/complete/torrents" in currentTorrent['downloadDir']:
  75.                 torrentSource =  currentTorrent['downloadDir'] + "/" + currentTorrent['name']
  76.                 torrentprocessDestination = "/mnt/usb1/to_process/" + currentTorrent['name']
  77.                 torrentseedDestination = "/mnt/usb1/seeds/rutracker/" + currentTorrent['name']
  78.  
  79.                 # Set Ratio Limit
  80.                 client('torrent-set',ids=currentTorrent['id'],seedRatioMode=1)
  81.  
  82.  
  83.                 folderCorrect = False;
  84.                 moveTorrents = moveTorrents + 1;
  85.                 ruSeeding = ruSeeding + 1;
  86.             # Already been moved, check if seeding complete
  87.             else:
  88.                 folderCorrect = True;
  89.  
  90.  
  91.                 #
  92.                 # CHECK IF SEEDING DONE, THEN REMOVE TORRENT / DEL FILES
  93.                 #
  94.                 if currentTorrent['uploadRatio'] > 0.99:
  95.                         removeTorrents = removeTorrents + 1;
  96.                         deletePath = currentTorrent['downloadDir'] + "/" + currentTorrent['name']
  97.                         print '* DELETED - %s' % (currentTorrent['name'])
  98.  
  99.                         # delete-local-data argument doesn't work here
  100.                         client('torrent-remove',ids=currentTorrent['id'])
  101.                         # use shutil instead
  102.                         if (os.path.isfile(deletePath) == True):
  103.                           os.remove(deletePath)
  104.                         else:
  105.                                 shutil.rmtree(deletePath)
  106.                         ruDeleted = ruDeleted + 1
  107.                 else:
  108.                         ruSeeding = ruSeeding + 1;
  109.  
  110.         # WAFFLES
  111.         elif "waffles" in currentTorrent['trackers'][0]['announce']:
  112.             # If it's in the download folder...
  113.             if "/mnt/usb1/downloads/complete/torrents" in currentTorrent['downloadDir']:
  114.                 torrentSource =  currentTorrent['downloadDir'] + "/" + currentTorrent['name']
  115.                 torrentprocessDestination = "/mnt/usb1/to_process/" + currentTorrent['name']
  116.                 torrentseedDestination = "/mnt/usb1/seeds/waffles/" + currentTorrent['name']
  117.  
  118.                 folderCorrect = False;
  119.                 moveTorrents = moveTorrents + 1;
  120.             # Already been moved, all good
  121.             else:
  122.               folderCorrect = True;
  123.  
  124.    
  125.  
  126.         # PUBLIC
  127.         else:
  128.             removeTorrents = removeTorrents + 1;
  129.             folderCorrect = True;
  130.             print '* REMOVED - %s' % (currentTorrent['name'])
  131.  
  132.             # Remove It
  133.             client('torrent-remove',ids=currentTorrent['id'])
  134.  
  135.  
  136.         #
  137.         # IF TORRENTS NEED TO BE MOVED
  138.         #
  139.         if (folderCorrect == False):
  140.             print '* MOVED - %s' % (currentTorrent['name'])
  141.  
  142.             # COPY TO PROCESS FOLDER
  143.             if (os.path.isfile(torrentSource) == True):
  144.                 shutil.copyfile(torrentSource, torrentprocessDestination)
  145.             else:
  146.                 shutil.copytree(torrentSource, torrentprocessDestination)
  147.             # MOVE TO SEEDING FOLDER
  148.             client('torrent-set-location',ids=currentTorrent['id'],location=torrentseedDestination,move='true')
  149.  
  150.     loop = loop + 1;
  151.  
  152. downTorrents = totalTorrents - doneTorrents;
  153.  
  154. print " "
  155. print "TORRENT CLEAN UP:"
  156. print "PUBLIC: %s removed." % (removeTorrents)
  157. print "SEEDS: %s moved to seeds." % (moveTorrents)
  158. print "RU: %s deleted, %s seeding." % (ruDeleted,ruSeeding)
  159. print " "
  160. print "DOWNLOADING: %s torrents." % (downTorrents)
  161. print "SEEDING: %s torrents" % (doneTorrents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement