-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,3 +165,6 @@ poetry.toml | |
|
||
# ruff | ||
.ruff_cache/ | ||
|
||
captcha.png | ||
session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import requests, webbrowser, os | ||
from exceptions import InvalidCaptchaCode | ||
from typing import Optional | ||
|
||
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
|
||
class TSRSession: | ||
@classmethod | ||
def __init__(self, sessionId: Optional[str] = None) -> None: | ||
self.session = requests.Session() | ||
if sessionId is not None: | ||
if self.__isValidSessionId(sessionId): | ||
self.tsrdlsession = sessionId | ||
return | ||
|
||
self.__getTSRDLTicketCookie() | ||
self.__saveCaptchaImage() | ||
self.__openImageInBrowser() | ||
self.tsrdlsession = None | ||
|
||
print("Please enter captcha code:") | ||
captchaInput = input(">> ") | ||
if self.__tryCaptchaCode(captchaInput): | ||
self.tsrdlsession = self.session.cookies.get_dict().get("tsrdlsession") | ||
else: | ||
raise InvalidCaptchaCode | ||
|
||
@classmethod | ||
def __tryCaptchaCode(self, code: str) -> bool: | ||
r = self.session.post( | ||
"https://www.thesimsresource.com/downloads/session/itemId/1646133", | ||
data={"captchavalue": code}, | ||
headers={ | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Origin": "https://www.thesimsresource.com", | ||
}, | ||
allow_redirects=True, | ||
) | ||
return ( | ||
r.url == "https://www.thesimsresource.com/downloads/download/itemId/1646133" | ||
) | ||
|
||
@classmethod | ||
def __isValidSessionId(self, sessionId: str) -> bool: | ||
self.__getTSRDLTicketCookie() | ||
r = self.session.get( | ||
"https://www.thesimsresource.com/downloads/download/itemId/1646133", | ||
cookies={"tsrdlsession": sessionId}, | ||
) | ||
return ( | ||
r.url == "https://www.thesimsresource.com/downloads/download/itemId/1646133" | ||
) | ||
|
||
@classmethod | ||
def __getCaptchaImage(self) -> requests.Request: | ||
self.session.get( | ||
"https://www.thesimsresource.com/downloads/session/itemId/1646133" | ||
) | ||
return self.session.get( | ||
"https://www.thesimsresource.com/downloads/captcha-image" | ||
) | ||
|
||
@classmethod | ||
def __saveCaptchaImage(self): | ||
with open(f"{CURRENT_DIR}/captcha.png", "wb") as f: | ||
for chunk in self.__getCaptchaImage().iter_content(1024 * 1024): | ||
f.write(chunk) | ||
|
||
@classmethod | ||
def __openImageInBrowser(self) -> None: | ||
webbrowser.open_new_tab(f"{CURRENT_DIR}/captcha.png") | ||
|
||
@classmethod | ||
def __getTSRDLTicketCookie(self) -> str: | ||
response = self.session.get( | ||
f"https://www.thesimsresource.com/ajax.php?c=downloads&a=initDownload&itemid=1646133&format=zip" | ||
) | ||
return response.cookies.get("tsrdlticket") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters