Skip to content

Commit

Permalink
use manually exported cookies for HDT (L4GSP1KE#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirius-sama authored Feb 13, 2024
1 parent b8cba88 commit 46a3667
Showing 1 changed file with 22 additions and 34 deletions.
56 changes: 22 additions & 34 deletions src/trackers/HDT.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async def upload(self, meta):
data['season'] = 'false'

# Anonymous check
if meta['anon'] == 0 and bool(distutils.util.strtobool(str(self.config['TRACKERS']['HDT'].get('anon', "False")))) == False:
if meta['anon'] == 0 and bool(distutils.util.strtobool(str(self.config['TRACKERS'][self.tracker].get('anon', "False")))) == False:
data['anonymous'] = 'false'
else:
data['anonymous'] = 'true'
Expand All @@ -185,9 +185,9 @@ async def upload(self, meta):
console.print(data)
else:
with requests.Session() as session:
cookiefile = os.path.abspath(f"{meta['base_dir']}/data/cookies/HDT.pkl")
with open(cookiefile, 'rb') as cf:
session.cookies.update(pickle.load(cf))
cookiefile = os.path.abspath(f"{meta['base_dir']}/data/cookies/HDT.txt")

session.cookies.update(await common.parseCookieFile(cookiefile))
up = session.post(url=url, data=data, files=files)
torrentFile.close()

Expand All @@ -207,9 +207,9 @@ async def upload(self, meta):
async def search_existing(self, meta):
dupes = []
with requests.Session() as session:
cookiefile = os.path.abspath(f"{meta['base_dir']}/data/cookies/HDT.pkl")
with open(cookiefile, 'rb') as cf:
session.cookies.update(pickle.load(cf))
common = COMMON(config=self.config)
cookiefile = os.path.abspath(f"{meta['base_dir']}/data/cookies/HDT.txt")
session.cookies.update(await common.parseCookieFile(cookiefile))

search_url = f"https://hd-torrents.org/torrents.php"
csrfToken = await self.get_csrfToken(session, search_url)
Expand Down Expand Up @@ -241,30 +241,21 @@ async def search_existing(self, meta):


async def validate_credentials(self, meta):
cookiefile = os.path.abspath(f"{meta['base_dir']}/data/cookies/HDT.pkl")
if not os.path.exists(cookiefile):
await self.login(cookiefile)
cookiefile = os.path.abspath(f"{meta['base_dir']}/data/cookies/HDT.txt")
vcookie = await self.validate_cookies(meta, cookiefile)
if vcookie != True:
console.print('[red]Failed to validate cookies. Please confirm that the site is up and your passkey is valid.')
recreate = cli_ui.ask_yes_no("Log in again and create new session?")
if recreate == True:
if os.path.exists(cookiefile):
os.remove(cookiefile)
await self.login(cookiefile)
vcookie = await self.validate_cookies(meta, cookiefile)
return vcookie
else:
return False
console.print('[red]Failed to validate cookies. Please confirm that the site is up or export a fresh cookie file from the site')
return False
return True


async def validate_cookies(self, meta, cookiefile):
common = COMMON(config=self.config)
url = "https://hd-torrents.org/index.php"
cookiefile = f"{meta['base_dir']}/data/cookies/HDT.txt"
if os.path.exists(cookiefile):
with requests.Session() as session:
with open(cookiefile, 'rb') as cf:
session.cookies.update(pickle.load(cf))
session.cookies.update(await common.parseCookieFile(cookiefile))
res = session.get(url=url)
if meta['debug']:
console.print('[cyan]Cookies:')
Expand All @@ -276,7 +267,13 @@ async def validate_cookies(self, meta, cookiefile):
return False
else:
return False




"""
Old login method, disabled because of site's DDOS protection. Better to use exported cookies.
async def login(self, cookiefile):
with requests.Session() as session:
url = "https://hd-torrents.org/login.php"
Expand All @@ -300,17 +297,8 @@ async def login(self, cookiefile):
await asyncio.sleep(1)
console.print(response.url)
return
# No longer used as torrent is modified instead of downloaded.
# async def download_new_torrent(self, session, id, torrent_path):
# download_url = f"https://hd-torrents.org/download.php?id={id}"
# r = session.get(url=download_url)
# if r.status_code == 200:
# with open(torrent_path, "wb") as tor:
# tor.write(r.content)
# else:
# console.print("[red]There was an issue downloading the new .torrent from HDT")
# console.print(r.text)
# return
"""


async def get_csrfToken(self, session, url):
r = session.get(url)
Expand Down

0 comments on commit 46a3667

Please sign in to comment.