Skip to content

Commit

Permalink
2.14.5
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCommCraft committed Aug 26, 2024
1 parent ab225a1 commit 9ee9e5d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 59 deletions.
4 changes: 2 additions & 2 deletions scratchcommunication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions scratchcommunication/commons.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 0 additions & 6 deletions scratchcommunication/headers.py

This file was deleted.

2 changes: 1 addition & 1 deletion scratchcommunication/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 34 additions & 49 deletions scratchcommunication/session.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,32 +11,26 @@
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):
if not _login:
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):
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 9ee9e5d

Please sign in to comment.