diff --git a/scratchcommunication/__init__.py b/scratchcommunication/__init__.py index 3f856b7..ebaeeaf 100644 --- a/scratchcommunication/__init__.py +++ b/scratchcommunication/__init__.py @@ -2,7 +2,7 @@ Module for communicating with scratch projects. """ -__version_number__ = '2.14.4' +__version_number__ = '2.14.5' from .session import * from .cloud import * @@ -12,7 +12,7 @@ from . import session from . import cloud from . import exceptions -from . import headers +from . import commons from . import security from . import cloud_socket from . import cloudrequests diff --git a/scratchcommunication/commons.py b/scratchcommunication/commons.py new file mode 100644 index 0000000..3ae2779 --- /dev/null +++ b/scratchcommunication/commons.py @@ -0,0 +1,32 @@ +from enum import Flag, auto + +_headers = { + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36", + "x-csrftoken": "a", + "x-requested-with": "XMLHttpRequest", + "referer": "https://scratch.mit.edu", +} + +_cookies = { + "scratchcsrftoken" : "a", + "scratchlanguage" : "en", + "scratchpolicyseen": "true", + "accept": "application/json", + "Content-Type": "application/json", +} + +def get_headers(): + return _headers.copy() + +def get_cookies(): + return _cookies.copy() + +class Browser(Flag): + FIREFOX = auto() + CHROME = auto() + EDGE = auto() + SAFARI = auto() + CHROMIUM = auto() + EDGE_DEV = auto() + VIVALDI = auto() + ANY = auto() diff --git a/scratchcommunication/headers.py b/scratchcommunication/headers.py deleted file mode 100644 index 74c74e5..0000000 --- a/scratchcommunication/headers.py +++ /dev/null @@ -1,6 +0,0 @@ -headers = { - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36", - "x-csrftoken": "a", - "x-requested-with": "XMLHttpRequest", - "referer": "https://scratch.mit.edu", -} \ No newline at end of file diff --git a/scratchcommunication/security.py b/scratchcommunication/security.py index 60e395c..5b5db30 100644 --- a/scratchcommunication/security.py +++ b/scratchcommunication/security.py @@ -234,7 +234,7 @@ def generate(cls, __type : Literal["RSA", "EC"] = "EC") -> Security: if __type == "RSA": return cls(data=RSAKeys.create_new_keys().keys, security_type="RSA") if __type != "EC": - assert_never() + assert_never(__type) scalar = bytearray(os.urandom(32)) scalar[0] &= 248 diff --git a/scratchcommunication/session.py b/scratchcommunication/session.py index e8128dd..a3c5b22 100644 --- a/scratchcommunication/session.py +++ b/scratchcommunication/session.py @@ -1,7 +1,7 @@ import warnings import requests, json, re from typing import Literal, Self, assert_never, Union -from .headers import headers +from .commons import get_headers, get_cookies, Browser from .exceptions import InvalidValueError, ErrorInCloudSocket, NotSupported from . import cloud, cloud_socket from . import security as sec @@ -11,16 +11,16 @@ except Exception as e: browsercookie = None browsercookie_err = e - -FIREFOX = 0 -CHROME = 1 -EDGE = 2 -SAFARI = 3 -CHROMIUM = 4 -EDGE_DEV = 5 -VIVALDI = 6 -ANY = 7 - + +FIREFOX = Browser.FIREFOX +CHROME = Browser.CHROME +EDGE = Browser.EDGE +SAFARI = Browser.SAFARI +CHROMIUM = Browser.CHROMIUM +EDGE_DEV = Browser.EDGE_DEV +VIVALDI = Browser.VIVALDI +ANY = Browser.ANY + class Session: __slots__ = ("session_id", "username", "headers", "cookies", "xtoken", "email", "id", "permissions", "flags", "banned", "session_data", "mute_status", "new_scratcher") def __init__(self, username : str = None, *, session_id : str = None, xtoken : str = None, _login : bool = True): @@ -28,15 +28,9 @@ def __init__(self, username : str = None, *, session_id : str = None, xtoken : s return self.session_id = session_id self.username = username - self.headers = headers - self.cookies = { - "scratchcsrftoken" : "a", - "scratchlanguage" : "en", - "scratchpolicyseen": "true", - "scratchsessionsid" : self.session_id, - "accept": "application/json", - "Content-Type": "application/json", - } + self.headers = get_headers() + self.cookies = get_cookies() + self.cookies["scratchsessionsid"] = session_id self._login(xtoken=xtoken) def __enter__(self): @@ -83,43 +77,34 @@ def supply_xtoken(self, xtoken): self.headers["X-Token"] = xtoken @classmethod - def from_browser(cls, browser : Literal[0,1,2,3,4,5,6,7]) -> Self: + def from_browser(cls, browser : Browser) -> Self: """ Import cookies from browser to login """ if not browsercookie: raise NotSupported("You cannot use browsercookie") from browsercookie_err - match browser: - case 0: - cookies = browsercookie.firefox() - case 1: - cookies = browsercookie.chrome() - case 2: - cookies = browsercookie.edge() - case 3: - cookies = browsercookie.safari() - case 4: - cookies = browsercookie.chromium() - case 5: - cookies = browsercookie.edge_dev() - case 6: - cookies = browsercookie.vivaldi() - case 7: - cookies = browsercookie.load() - case _: - assert_never(browser) + if ANY in Browser: + cookies = browsercookie.load() + if FIREFOX in Browser: + cookies = browsercookie.firefox() + if CHROME in Browser: + cookies = browsercookie.chrome() + if EDGE in Browser: + cookies = browsercookie.edge() + if SAFARI in Browser: + cookies = browsercookie.safari() + if CHROMIUM in Browser: + cookies = browsercookie.chromium() + if EDGE_DEV in Browser: + cookies = browsercookie.edge_dev() + if FIREFOX in Browser: + cookies = browsercookie.vivaldi() with requests.Session() as session: session.cookies.update(cookies) - session.headers.update(headers) + session.headers.update(get_headers()) obj = cls(_login=False) - obj.cookies = { - "scratchcsrftoken" : "a", - "scratchlanguage" : "en", - "scratchpolicyseen": "true", - "accept": "application/json", - "Content-Type": "application/json", - } + obj.cookies = get_cookies() obj.cookies.update(session.cookies.get_dict(".scratch.mit.edu")) obj.session_id = session.cookies.get_dict(".scratch.mit.edu").get("scratchsessionsid") obj.headers = session.headers @@ -139,7 +124,7 @@ def login(cls, username : str, password : str) -> Self: "username": username, "password": password }), - headers=headers, + headers=get_headers(), cookies={ "scratchcsrftoken": "a", "scratchlanguage": "en" diff --git a/setup.py b/setup.py index 7a651a7..bdd0140 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ with open("README.md", encoding="utf-8") as f: long_description = f.read() -VERSION = '2.14.4' +VERSION = '2.14.5' setup( name='scratchcommunication',