-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a proper cookiejar + Nicer embeds
- Loading branch information
Showing
9 changed files
with
411 additions
and
230 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,3 @@ | ||
import importlib.util | ||
|
||
BS4_FEATURE = "lxml" if importlib.util.find_spec("lxml") else "html.parser" |
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,27 @@ | ||
from http.client import SERVICE_UNAVAILABLE | ||
import httpx | ||
from bs4 import BeautifulSoup | ||
|
||
from ._bs4 import BS4_FEATURE | ||
from .exceptions import MaintenanceException, ChuniNetError | ||
|
||
|
||
async def raise_on_chunithm_net_error(response: httpx.Response): | ||
if response.url.path != "/mobile/error/": | ||
return | ||
|
||
html = "" | ||
async for chunk in response.aiter_text(): | ||
html += chunk | ||
|
||
dom = BeautifulSoup(html, BS4_FEATURE) | ||
error_blocks = dom.select(".block.text_l .font_small") | ||
code = int(error_blocks[0].text.split(": ", 1)[1]) | ||
description = error_blocks[1].text if len(error_blocks) > 1 else "" | ||
|
||
raise ChuniNetError(code, description) | ||
|
||
|
||
async def raise_on_scheduled_maintenance(response: httpx.Response): | ||
if response.status_code == SERVICE_UNAVAILABLE: | ||
raise MaintenanceException |
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
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
70 changes: 70 additions & 0 deletions
70
database/alembic/versions/2024_03_17_1418-50bb24f19a3a_use_a_proper_cookie_jar.py
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,70 @@ | ||
"""Use a proper cookie jar | ||
Revision ID: 50bb24f19a3a | ||
Revises: 901af18ec932 | ||
Create Date: 2024-03-17 14:18:11.579871 | ||
""" | ||
from http.cookiejar import LWPCookieJar, Cookie | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "50bb24f19a3a" | ||
down_revision: Union[str, None] = "901af18ec932" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
cookies = sa.Table( | ||
"cookies", | ||
sa.MetaData(), | ||
sa.Column("discord_id", sa.BigInteger, primary_key=True), | ||
sa.Column("cookie", sa.String, nullable=False), | ||
sa.Column("kamaitachi_token", sa.String(40), nullable=True), | ||
) | ||
|
||
|
||
def upgrade() -> None: | ||
with op.batch_alter_table("cookies") as bop: | ||
bop.alter_column("cookie", type_=sa.String) | ||
|
||
conn = op.get_bind() | ||
rows = [x._asdict() for x in conn.execute(sa.select(cookies))] | ||
|
||
for row in rows: | ||
cookie = Cookie( | ||
version=0, | ||
name="clal", | ||
value=row["cookie"], | ||
port=None, | ||
port_specified=False, | ||
domain="lng-tgk-aime-gw.am-all.net", | ||
domain_specified=True, | ||
domain_initial_dot=False, | ||
path="/common_auth", | ||
path_specified=True, | ||
secure=False, | ||
expires=3856586927, # 2092-03-17 10:08:47Z | ||
discard=False, | ||
comment=None, | ||
comment_url=None, | ||
rest={}, | ||
) | ||
jar = LWPCookieJar() | ||
jar.set_cookie(cookie) | ||
|
||
conn.execute( | ||
sa.update(cookies) | ||
.where(cookies.c.discord_id == row["discord_id"]) | ||
.values( | ||
cookie=f"#LWP-Cookies-2.0\n{jar.as_lwp_str(ignore_discard=False, ignore_expires=False)}" | ||
) | ||
) | ||
|
||
conn.commit() | ||
|
||
|
||
def downgrade() -> None: | ||
pass |
Oops, something went wrong.