add ssl, check content-type

This commit is contained in:
iou1name 2019-07-26 20:03:20 -04:00
parent 23eb0543c8
commit 27fd41da49

View File

@ -10,7 +10,7 @@ import configparser
import http.cookiejar
import requests
from twisted.internet import protocol, reactor
from twisted.internet import ssl, protocol, reactor
from twisted.words.protocols import irc
HEADERS = {"User-Agent": "spaghetti is a faggot"}
@ -39,6 +39,9 @@ class OppaiBot(irc.IRCClient):
"""
res = requests.get(url, cookies=self.cj, headers=HEADERS, verify=True)
res.raise_for_status()
if not res.headers['Content-Type'].startswith('application/x-bittorrent'):
print(f"ERROR: Could not download torrent file from URL '{url}'.")
return
fname =re.search(r'filename="(.+)"',res.headers['content-disposition'])
fname = fname.group(1)
fname = fname.encode("latin-1").decode("utf-8")
@ -119,5 +122,9 @@ if __name__ == "__main__":
server = tracker_config["server"]
port = tracker_config.getint("port")
print("Connecting to:", tracker_config.name)
reactor.connectTCP(server, port, OppaiBotFactory(tracker_config))
reactor.connectSSL(
server,
port,
OppaiBotFactory(tracker_config),
ssl.ClientContextFactory())
reactor.run()