inbox/services/core.py

59 lines
1.9 KiB
Python
Raw Normal View History

2023-10-04 20:42:39 +00:00
from httpx import AsyncClient
from settings import API_BASE
2023-10-11 19:12:55 +00:00
headers = {"Content-Type": "application/json"}
2023-11-14 18:25:55 +00:00
INTERNAL_AUTH_SERVER = "v2." in API_BASE
2023-10-04 20:42:39 +00:00
2023-11-14 18:20:45 +00:00
async def get_all_authors():
query_name = "authorsAll"
2023-11-06 16:25:28 +00:00
query_type = "query"
2023-11-14 18:20:45 +00:00
operation = "AuthorsAll"
query_fields = "id slug userpic name"
2023-11-14 18:25:55 +00:00
headers = {"Content-Type": "application/json"} # "Bearer " + removed
2023-11-06 16:25:28 +00:00
2023-10-04 20:42:39 +00:00
gql = {
2023-11-06 16:25:28 +00:00
"query": query_type + " " + operation + " { " + query_name + " { " + query_fields + " } " + " }",
"operationName": operation,
2023-11-14 18:20:45 +00:00
"variables": None,
2023-10-04 20:42:39 +00:00
}
2023-10-13 13:35:16 +00:00
async with AsyncClient() as client:
2023-10-16 20:46:33 +00:00
try:
response = await client.post(API_BASE, headers=headers, json=gql)
except Exception:
import traceback
2023-11-14 18:25:55 +00:00
2023-10-16 20:46:33 +00:00
traceback.print_exc()
2023-11-14 18:20:45 +00:00
print(f"[services.core] {query_name}: {response.status_code} {response.text}")
2023-10-14 14:55:51 +00:00
if response.status_code != 200:
2023-10-13 13:35:16 +00:00
return None
2023-10-14 14:55:51 +00:00
r = response.json()
2023-11-14 18:20:45 +00:00
return r.get("data", {}).get(query_name)
2023-10-04 20:42:39 +00:00
2023-10-11 19:12:55 +00:00
2023-11-14 18:20:45 +00:00
async def get_my_followings():
query_name = "loadMySubscriptions"
query_type = "query"
operation = "LoadMySubscriptions"
query_fields = "id slug userpic name"
2023-11-14 18:25:55 +00:00
headers = {"Content-Type": "application/json"} # "Bearer " + removed
2023-10-11 19:12:55 +00:00
gql = {
2023-11-14 18:20:45 +00:00
"query": query_type + " " + operation + " { " + query_name + " { authors {" + query_fields + "} } " + " }",
"operationName": operation,
"variables": None,
2023-10-11 19:12:55 +00:00
}
2023-11-14 18:20:45 +00:00
async with AsyncClient() as client:
try:
2023-10-14 14:55:51 +00:00
response = await client.post(API_BASE, headers=headers, json=gql)
2023-11-14 18:20:45 +00:00
except Exception:
import traceback
2023-11-14 18:25:55 +00:00
2023-11-14 18:20:45 +00:00
traceback.print_exc()
print(f"[services.core] {query_name}: {response.status_code} {response.text}")
if response.status_code != 200:
return None
r = response.json()
2023-11-14 18:35:35 +00:00
authors = r.get("data", {}).get(query_name, {}).get("authors", [])
2023-11-14 18:20:45 +00:00
return authors