-
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.
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__pycache__ | ||
*.epub | ||
cookie | ||
creds.json |
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,82 @@ | ||
import aiohttp | ||
import os | ||
import asyncio | ||
from getpass import getpass | ||
from bs4 import BeautifulSoup | ||
from urllib.parse import urljoin | ||
import json | ||
|
||
# TODO: use this | ||
# from .render import GLOWFIC_ROOT | ||
|
||
GLOWFIC_ROOT = "https://glowfic.com" | ||
# TODO: import this | ||
COOKIE_NAME = "_glowfic_constellation_production" | ||
|
||
|
||
async def main(): | ||
async with aiohttp.ClientSession() as session: | ||
await login(session) | ||
resp = await session.get("https://www.glowfic.com/api/v1/posts/6755") | ||
text = await resp.text() | ||
print(text) | ||
|
||
|
||
def get_creds(): | ||
if os.path.exists("creds.json"): | ||
with open("creds.json", "r") as fin: | ||
d = json.load(fin) | ||
return (d["username"], d["password"]) | ||
username = input("Username: ") | ||
password = getpass() | ||
save_str = input("Save credentials to file? [y/N]") | ||
save = save_str.lower() in ["y", "yes", "true"] | ||
if save: | ||
print("Saving to creds.json") | ||
d = {"username": username, "password": password} | ||
with open("creds.json", "w") as f: | ||
json.dump(d, f) | ||
return (username, password) | ||
|
||
|
||
async def login(session): | ||
# Set cookie for non-API endpoints | ||
authenticity_token = await get_authenticity_token(session) | ||
(username, password) = get_creds() | ||
payload = { | ||
"username": username, | ||
"password": password, | ||
"authenticity_token": authenticity_token, | ||
"commit": "Log+in", | ||
} | ||
url = urljoin(GLOWFIC_ROOT, "login") | ||
resp = await session.post(url, data=payload) | ||
found_cookie = False | ||
for cookie in session.cookie_jar: | ||
found_cookie |= cookie.key == COOKIE_NAME | ||
if not found_cookie: | ||
raise ValueError("Cookie not found after login") | ||
api_login_url = urljoin(GLOWFIC_ROOT, "/api/v1/login") | ||
payload = { | ||
"username": username, | ||
"password": password, | ||
} | ||
async with session.post(api_login_url, params=payload) as resp: | ||
token_json = await resp.json() | ||
try: | ||
token = token_json["token"] | ||
except KeyError: | ||
print(token_json) | ||
raise | ||
session.headers["Authorization"] = "Bearer %s" % token | ||
|
||
|
||
async def get_authenticity_token(session): | ||
async with session.get(GLOWFIC_ROOT) as resp: | ||
soup = BeautifulSoup(await resp.text(), "html.parser") | ||
form = soup.find("form", id="header-form") | ||
authenticity_token = form.find("input", attrs={"name": "authenticity_token"}) | ||
return authenticity_token.attrs["value"] | ||
|
||
|
||
asyncio.run(main()) |