Advertisement
Guest User

qBitRuleScript 3.0

a guest
Nov 12th, 2018
2,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.72 KB | None | 0 0
  1. import urllib3
  2. import json
  3.  
  4. rule = {}
  5. def addRule(a,b,c,d,e):
  6.     rule.update({a: {'SeedTime': b, 'Ratio': c, 'DeleteFiles': d, 'Match': e}})
  7.  
  8.  
  9. ############################################################################################
  10. # Note: This script works locally on host machine in conjunction with qBittorrent WebUI to remove torrents from
  11. # specific categories that match the set rules. Make sure "Bypass authentication" in qBittorrent settings is enabled for script to work.
  12. #
  13. # **EXAMPLE RULE**
  14. # Category = 'Test_2' <----- # Category name in qBittorrent. If you are using subcategories, use 'CategoryName/SubCategoryName'
  15. # SeedTime = 168*60*60 <---- (168Hours)*60*60 - Torrent will be removed when this many seconds of active seed time is reached
  16. # Ratio = '999.99' <-------- Torrent will be removed when this number is reached
  17. # DeleteFiles = 'True' <---- (True | False) Set to remove or keep files on torrent removal
  18. # Match = '1'  <------------ '1': Match SeedTime & Ratio | '2': Match SeedTime | '3': Match Ratio | '4': Match either one
  19. # addRule(Category,SeedTime,Ratio,DeleteFiles,Match) <-----Keep this line the sam
  20. ############################################################################################
  21.  
  22.  
  23. ############################################################################################
  24. #EDIT BELOW THIS LINE - COPY EXAMPLE RULES AND CREATE YOUR OWN. EDIT TO YOUR LIKING
  25. ############################################################################################
  26.  
  27. url = "http://localhost:8080" #url to qBittorrent WebUI
  28.  
  29. #Make sure the rules are set to your liking before turning off testMode to prevent accidently deleting files.
  30.  
  31. testMode = 'on' # 'off' | 'on' :Takes action if 'off', shows what would happen if 'on'. Default: on
  32.  
  33. ############################################################################################
  34.  
  35.  
  36.  
  37. Category = 'Example 1' # If you are using subcategories, use 'CategoryName/SubCategoryName'
  38. SeedTime = 24*60*60 # (Measured in seconds) Change first number to change amount of hours (24*60*60 = 24Hours)
  39. Ratio = '2.00'
  40. DeleteFiles = 'False' # 'True' | 'False' :Delete files when rule is matched
  41. Match = '1' # '1': Match SeedTime & Ratio | '2': Match SeedTime | '3': Match Ratio | '4': Match either one
  42. addRule(Category,SeedTime,Ratio,DeleteFiles,Match) #Don't edit this line but be sure to include it when making new rules
  43.  
  44.  
  45. Category = 'Example 2'
  46. SeedTime = 24*60*60
  47. Ratio = '2.00'
  48. DeleteFiles = 'False'
  49. Match = '1'
  50. addRule(Category,SeedTime,Ratio,DeleteFiles,Match)
  51.  
  52.  
  53. Category = 'Example 3'
  54. SeedTime = 24*60*60
  55. Ratio = '2.00'
  56. DeleteFiles = 'False'
  57. Match = '1'
  58. addRule(Category,SeedTime,Ratio,DeleteFiles,Match)
  59.  
  60.  
  61. # Nothing further below needs to be edited
  62. ############################################################################################
  63. # Cheat Sheet (Days|Hours)
  64. # 1|24, 2|48, 3|72, 4|96, 5|120, 6|144, 7|168, ... 14|336, 21|504, 28|672
  65. ############################################################################################
  66. ############################################################################################
  67.  
  68.  
  69. def grabTorrents():
  70.     global torrents
  71.     http = urllib3.PoolManager()
  72.     r = http.request('GET', url+'/api/v2/torrents/info', fields={'filter': 'all'})
  73.     r.close()
  74.     torrents = json.loads(r.data.decode('utf-8'))
  75.  
  76.  
  77. def hashGrab(hash):
  78.     global hashinfo
  79.     http = urllib3.PoolManager()
  80.     r = http.request('GET', url + '/api/v2/torrents/properties', fields={'hash': hash})
  81.     r.close()
  82.     hashinfo = json.loads(r.data.decode('utf-8'))
  83.     return hashinfo
  84.  
  85.  
  86. def grabCategory(data):
  87.     output = []
  88.     for i in range(len(torrents)):
  89.         if (torrents[i].get('category'))==data:
  90.             output.append(torrents[i])
  91.     return (output)
  92.  
  93.  
  94. def removeTorrent(hash,deleteFiles):
  95.     http = urllib3.PoolManager()
  96.     r = http.request('GET', url + '/api/v2/torrents/delete', fields={'hashes': hash,'deleteFiles': deleteFiles})
  97.     r.close()
  98.  
  99.  
  100. def checkRules():
  101.  
  102.     for i in rule:
  103.  
  104.         for rulematch in grabCategory(i):
  105.  
  106.             hash = (rulematch.get('hash'))
  107.             ratio = str(rulematch.get('ratio'))
  108.             ratio = float((str(ratio)[:4]))
  109.             name = (rulematch.get('name'))
  110.             seedingtime = (hashGrab(hash).get('seeding_time'))
  111.  
  112.  
  113.             rRatio = (float(rule.get(i).get('Ratio')))
  114.             rSeedTime = (rule.get(i).get('SeedTime'))
  115.             rDeleteFiles = (rule.get(i).get('DeleteFiles'))
  116.             rMatch = (rule.get(i).get('Match'))
  117.  
  118.  
  119.             if ratio > rRatio:
  120.                 ratiomatch = True
  121.             else:
  122.                 ratiomatch = False
  123.  
  124.             if seedingtime > rSeedTime:
  125.                 seedmatch = True
  126.             else:
  127.                 seedmatch = False
  128.  
  129.  
  130.             if rMatch == '1': # '1': Match SeedTime & Ratio
  131.                 if seedmatch == True and ratiomatch == True:
  132.                     if testMode == 'off':
  133.                         removeTorrent(hash, rDeleteFiles)
  134.                     else:
  135.                         print ('Category: '+i+' | '+'Match = '+rMatch+' | Match SeedTime & Ratio\n'+'Removing torrent | Delete files = '+rDeleteFiles)
  136.                         print ('Torrent Name: '+name+'\nTime Seeded: '+str(seedingtime)+' / '+str(rSeedTime)+'\nRatio: '+str(ratio)+' / '+str(rRatio))
  137.                         print ('_____________________________________________________________________')
  138.  
  139.  
  140.             elif rMatch == '2': # '2': Match SeedTime
  141.                 if seedmatch == True:
  142.                     if testMode == 'off':
  143.                         removeTorrent(hash, rDeleteFiles)
  144.                     elif testMode == 'on':
  145.                         print ('Category: '+i+' | '+'Match = '+rMatch+' | Match SeedTime\n'+'Removing torrent | Delete files = '+rDeleteFiles)
  146.                         print ('Torrent Name: '+name+'\nTime Seeded: '+str(seedingtime)+' / '+str(rSeedTime)+'\nRatio: '+str(ratio)+' / '+str(rRatio))
  147.                         print ('_____________________________________________________________________')
  148.  
  149.  
  150.             elif rMatch == '3': # '3': Match Ratio
  151.                 if ratiomatch == True:
  152.                     if testMode == 'off':
  153.                         removeTorrent(hash, rDeleteFiles)
  154.                     else:
  155.                         print ('Category: '+i+' | '+'Match = '+rMatch+' | Match Ratio\n'+'Removing torrent | Delete files = '+rDeleteFiles)
  156.                         print ('Torrent Name: '+name+'\nTime Seeded: '+str(seedingtime)+' / '+str(rSeedTime)+'\nRatio: '+str(ratio)+' / '+str(rRatio))
  157.                         print ('_____________________________________________________________________')
  158.  
  159.  
  160.             elif rMatch == '4': # '4': Match either one
  161.                 if ratiomatch == True or seedmatch == True:
  162.                     if testMode == 'off':
  163.                         removeTorrent(hash, rDeleteFiles)
  164.                     else:
  165.                         print ('Category: '+i+' | '+'Match = '+rMatch+' | Match SeedTime or Ratio\n'+'Removing torrent | Delete files = '+rDeleteFiles)
  166.                         print ('Torrent Name: '+name+'\nTime Seeded: '+str(seedingtime)+' / '+str(rSeedTime)+'\nRatio: '+str(ratio)+' / '+str(rRatio))
  167.                         print ('_____________________________________________________________________')
  168.  
  169.  
  170. def TestModePause():
  171.     if testMode=='on':
  172.         import sys
  173.         v = (sys.version_info[0])
  174.         if (v==2):
  175.             print("\nTest Mode Enabled: PAUSING | No actions taken")
  176.             raw_input('...Press ENTER to continue')
  177.         elif v==3:
  178.             print("\nTest Mode Enabled: PAUSING | No actions taken")
  179.             input('...Press any key to continue')
  180.  
  181.  
  182. grabTorrents()
  183. checkRules()
  184. TestModePause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement