74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
# mam_seedbox.py
|
|
|
|
import json
|
|
import urllib.error
|
|
import urllib.request
|
|
from http.cookies import SimpleCookie
|
|
from pathlib import Path
|
|
|
|
__location__ = Path(__file__).parent.resolve()
|
|
cookie_file = __location__.joinpath("mam_id")
|
|
api_url = "https://t.myanonamouse.net/json/dynamicSeedbox.php"
|
|
|
|
|
|
def save_cookie(headers):
|
|
set_cookie_header = headers.get("Set-Cookie")
|
|
if set_cookie_header:
|
|
cookie = SimpleCookie(set_cookie_header)
|
|
if "mam_id" in cookie:
|
|
cookie_file.write_text(cookie["mam_id"].value)
|
|
|
|
|
|
def load_cookie() -> str:
|
|
if not cookie_file.exists():
|
|
print("Warning: mam_id file not found. Proceeding without it...", flush=True)
|
|
return ""
|
|
|
|
mam_id = cookie_file.read_text().strip()
|
|
return f"mam_id={mam_id}"
|
|
|
|
|
|
def main():
|
|
print("Updating MaM Dynamic Seedbox IP...", flush=True)
|
|
|
|
req = urllib.request.Request(api_url)
|
|
cookie_val = load_cookie()
|
|
if cookie_val:
|
|
req.add_header("Cookie", cookie_val)
|
|
|
|
status_code = None
|
|
data = {}
|
|
headers = {}
|
|
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
status_code = response.status
|
|
data = json.loads(response.read().decode("utf-8"))
|
|
headers = response.headers
|
|
except urllib.error.HTTPError as e:
|
|
status_code = e.code
|
|
try:
|
|
data = json.loads(e.read().decode("utf-8"))
|
|
except json.JSONDecodeError:
|
|
data = {"msg": "Unknown server error"}
|
|
except urllib.error.URLError as e:
|
|
print(f"Failed to connect to MaM: {e.reason}", flush=True)
|
|
return
|
|
|
|
msg = data.get("msg", "No message provided")
|
|
|
|
match status_code:
|
|
case 200:
|
|
print(f"Successfully updated MaM seedbox: {msg}", flush=True)
|
|
save_cookie(headers)
|
|
case 429:
|
|
print(f"Failed to update MaM seedbox (Rate Limited): {msg}", flush=True)
|
|
case 403:
|
|
print(f"Failed to update MaM seedbox (Forbidden): {msg}", flush=True)
|
|
case _:
|
|
print(f"Something went VERY wrong... HTTP {status_code}: {msg}", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|