Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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.
- # 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.
- from datetime import datetime
- import re
- import subprocess
- import sys
- import time
- import requests
- import irc.client
- # CONSTANTS
- MIN_SPACE = 10 * 1024 * 1024 * 1024 # 10 GB
- SPACE_CMD = ["df", "/srv/deluge/Downloads"]
- GGN = "https://gazellegames.net"
- FL_ANN_SOURCE = "Vertigo!Vertigo@vertigo.bot.gazellegames.net"
- FL_ANN_REGEX = re.compile(r"A freeleech pot was filled.*https://gazellegames\.net/torrents\.php\?id=(\d+)")
- # AUTH
- API_KEY = ""
- AUTH_KEY = ""
- TORRENT_PASS = ""
- IRC_SERVER = "127.0.0.1"
- IRC_PORT = 8667
- IRC_NICK = ""
- IRC_USER = "/ggn"
- IRC_PASS = ""
- # END AUTH
- global last_req # hack around python's terrible variable scoping
- last_req = 0
- def req_group(group_id):
- # ratelimit
- global last_req
- if time.time() - last_req <= 2:
- time.sleep(3)
- last_req = time.time()
- # request
- r = requests.get(f"{GGN}/api.php?request=torrentgroup&id={group_id}&key={API_KEY}")
- return r.json()["response"]
- def free_space():
- process = subprocess.run(SPACE_CMD, check=True, capture_output=True)
- out = process.stdout.decode()
- line = out.split("\n")[1]
- return int(line.split()[3]) * 1024 # df returns 1K blocks
- def try_download(torrent, other_size):
- # keep a minimum of space on the drive
- space = free_space()
- min_space = MIN_SPACE + torrent["size"] + other_size
- if space < min_space: return False
- # download the torrent
- tid = torrent["id"]
- r = requests.get(f"{GGN}/torrents.php?action=download&id={tid}&authkey={AUTH_KEY}&torrent_pass={TORRENT_PASS}")
- with open(f"./{tid}.torrent", "wb") as f:
- f.write(r.content)
- return True
- def on_msg(_connection, event):
- if event.source != FL_ANN_SOURCE: return
- print(event)
- match = FL_ANN_REGEX.search(event.arguments[0])
- if match is None: return
- group_id = match[1]
- print(group_id)
- group = req_group(group_id)
- print(group["group"]["name"])
- most_seeded = {}
- most_recent = None
- most_recent_time = datetime.fromisoformat("2000-01-01 01:01:01")
- for t in group["torrents"]:
- if not t["freeTorrent"] or t["neutralTorrent"]: continue
- if t["type"] != "Torrent": continue
- if t["reported"]: continue
- if t["releaseType"] == "GameDOX": continue
- if t["language"] != "English" and t["language"] != "Multi-Language": continue
- seed = t["seeders"]
- if not most_seeded or seed > most_seeded["seeders"]:
- most_seeded = t
- time = datetime.fromisoformat(t["time"])
- if time > most_recent_time:
- most_recent = t
- most_recent_time = time
- if most_seeded == most_recent: most_recent = None
- if most_seeded:
- downloaded = try_download(most_seeded, 0)
- if most_recent:
- try_download(most_recent, most_seeded["size"] if downloaded else 0)
- def on_disconnect(_connection, _event):
- raise SystemExit()
- def main():
- reactor = irc.client.Reactor()
- try:
- server = reactor.server()
- server.connect(IRC_SERVER, IRC_PORT, IRC_NICK, username=IRC_USER, password=IRC_PASS)
- except irc.client.ServerConnectionError:
- print(sys.exc_info()[1])
- raise SystemExit(1)
- server.add_global_handler("privmsg", on_msg)
- server.add_global_handler("pubmsg", on_msg)
- server.add_global_handler("notice", on_msg)
- server.add_global_handler("disconnect", on_disconnect)
- reactor.process_forever()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment