Guest User

ggnautofl

a guest
Feb 18th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | Source Code | 0 0
  1. # NOTE: Assumes a Linux environment (see free_space/SPACE_CMD). Assumes a znc server. Edit the code if this is not the case for you.
  2. # only dependency is irc. install with "pip install irc". may also be available on your distro's package manager, e.g. "sudo pacman -S python-irc" on arch.
  3.  
  4. from datetime import datetime
  5. import re
  6. import subprocess
  7. import sys
  8. import time
  9. import requests
  10. import irc.client
  11.  
  12. # CONSTANTS
  13. MIN_SPACE = 10 * 1024 * 1024 * 1024 # 10 GB
  14. SPACE_CMD = ["df", "/srv/deluge/Downloads"]
  15.  
  16. GGN = "https://gazellegames.net"
  17. FL_ANN_SOURCE = "Vertigo!Vertigo@vertigo.bot.gazellegames.net"
  18. FL_ANN_REGEX = re.compile(r"A freeleech pot was filled.*https://gazellegames\.net/torrents\.php\?id=(\d+)")
  19.  
  20. # AUTH
  21. API_KEY = ""
  22. AUTH_KEY = ""
  23. TORRENT_PASS = ""
  24.  
  25. IRC_SERVER = "127.0.0.1"
  26. IRC_PORT = 8667
  27. IRC_NICK = ""
  28. IRC_USER = "/ggn"
  29. IRC_PASS = ""
  30. # END AUTH
  31.  
  32.  
  33. global last_req # hack around python's terrible variable scoping
  34. last_req = 0
  35. def req_group(group_id):
  36.     # ratelimit
  37.     global last_req
  38.     if time.time() - last_req <= 2:
  39.         time.sleep(3)
  40.     last_req = time.time()
  41.  
  42.     # request
  43.     r = requests.get(f"{GGN}/api.php?request=torrentgroup&id={group_id}&key={API_KEY}")
  44.     return r.json()["response"]
  45.  
  46. def free_space():
  47.     process = subprocess.run(SPACE_CMD, check=True, capture_output=True)
  48.     out = process.stdout.decode()
  49.     line = out.split("\n")[1]
  50.     return int(line.split()[3]) * 1024 # df returns 1K blocks
  51.  
  52. def try_download(torrent, other_size):
  53.     # keep a minimum of space on the drive
  54.     space = free_space()
  55.     min_space = MIN_SPACE + torrent["size"] + other_size
  56.     if space < min_space: return False
  57.  
  58.     # download the torrent
  59.     tid = torrent["id"]
  60.  
  61.     r = requests.get(f"{GGN}/torrents.php?action=download&id={tid}&authkey={AUTH_KEY}&torrent_pass={TORRENT_PASS}")
  62.     with open(f"./{tid}.torrent", "wb") as f:
  63.         f.write(r.content)
  64.     return True
  65.  
  66. def on_msg(_connection, event):
  67.     if event.source != FL_ANN_SOURCE: return
  68.     print(event)
  69.     match = FL_ANN_REGEX.search(event.arguments[0])
  70.     if match is None: return
  71.  
  72.     group_id = match[1]
  73.     print(group_id)
  74.     group = req_group(group_id)
  75.     print(group["group"]["name"])
  76.  
  77.     most_seeded = {}
  78.     most_recent = None
  79.     most_recent_time = datetime.fromisoformat("2000-01-01 01:01:01")
  80.  
  81.     for t in group["torrents"]:
  82.         if not t["freeTorrent"] or t["neutralTorrent"]: continue
  83.         if t["type"] != "Torrent": continue
  84.         if t["reported"]: continue
  85.         if t["releaseType"] == "GameDOX": continue
  86.         if t["language"] != "English" and t["language"] != "Multi-Language": continue
  87.  
  88.         seed = t["seeders"]
  89.         if not most_seeded or seed > most_seeded["seeders"]:
  90.             most_seeded = t
  91.  
  92.         time = datetime.fromisoformat(t["time"])
  93.         if time > most_recent_time:
  94.             most_recent = t
  95.             most_recent_time = time
  96.  
  97.     if most_seeded == most_recent: most_recent = None
  98.  
  99.     if most_seeded:
  100.         downloaded = try_download(most_seeded, 0)
  101.  
  102.     if most_recent:
  103.         try_download(most_recent, most_seeded["size"] if downloaded else 0)
  104.  
  105. def on_disconnect(_connection, _event):
  106.     raise SystemExit()
  107.  
  108. def main():
  109.     reactor = irc.client.Reactor()
  110.  
  111.     try:
  112.         server = reactor.server()
  113.         server.connect(IRC_SERVER, IRC_PORT, IRC_NICK, username=IRC_USER, password=IRC_PASS)
  114.     except irc.client.ServerConnectionError:
  115.         print(sys.exc_info()[1])
  116.         raise SystemExit(1)
  117.  
  118.     server.add_global_handler("privmsg", on_msg)
  119.     server.add_global_handler("pubmsg", on_msg)
  120.     server.add_global_handler("notice", on_msg)
  121.     server.add_global_handler("disconnect", on_disconnect)
  122.  
  123.     reactor.process_forever()
  124.  
  125. if __name__ == '__main__':
  126.     main()
  127.  
Add Comment
Please, Sign In to add comment