This commit is contained in:
@@ -1,26 +1,20 @@
|
||||
from datetime import datetime
|
||||
|
||||
from httpx import AsyncClient
|
||||
|
||||
from settings import API_BASE
|
||||
from validators.member import ChatMember
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
INTERNAL_AUTH_SERVER = 'v2.' in API_BASE
|
||||
|
||||
async def get_author(author_id):
|
||||
query_name = "getAuthorById" if INTERNAL_AUTH_SERVER else "GetAuthorById"
|
||||
async def get_all_authors():
|
||||
query_name = "authorsAll"
|
||||
query_type = "query"
|
||||
operation = "GetAuthorById"
|
||||
query_fields = "id slug userpic name lastSeen" if INTERNAL_AUTH_SERVER else "id slug userpic name last_seen"
|
||||
operation = "AuthorsAll"
|
||||
query_fields = "id slug userpic name"
|
||||
headers = {"Content-Type": "application/json"} # "Bearer " + removed
|
||||
|
||||
gql = {
|
||||
"query": query_type + " " + operation + " { " + query_name + " { " + query_fields + " } " + " }",
|
||||
"operationName": operation,
|
||||
"variables": {
|
||||
"author_id": author_id
|
||||
},
|
||||
"variables": None,
|
||||
}
|
||||
async with AsyncClient() as client:
|
||||
try:
|
||||
@@ -28,66 +22,39 @@ async def get_author(author_id):
|
||||
except Exception:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(f"[services.core] get_author: {response.status_code} {response.text}")
|
||||
print(f"[services.core] {query_name}: {response.status_code} {response.text}")
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
r = response.json()
|
||||
a = r.get("data", {}).get("getAuthorById")
|
||||
if a:
|
||||
ts_value = a.get("lastSeen", a.get("last_seen"))
|
||||
dt = datetime.strptime(ts_value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||
timestamp = int(dt.timestamp())
|
||||
a["last_seen"] = timestamp
|
||||
author: ChatMember = a
|
||||
return author
|
||||
return r.get("data", {}).get(query_name)
|
||||
|
||||
|
||||
async def get_network(author_id: int, limit: int = 50, offset: int = 0) -> list:
|
||||
async def get_my_followings():
|
||||
query_name = "loadMySubscriptions"
|
||||
query_type = "query"
|
||||
operation = "LoadMySubscriptions"
|
||||
query_fields = "id slug userpic name"
|
||||
headers = {"Content-Type": "application/json"} # "Bearer " + removed
|
||||
|
||||
gql = {
|
||||
"query": """query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) {
|
||||
authorFollowings(author_id: $author_id, limit: $limit, offset: $offset) {
|
||||
id slug userpic name
|
||||
}
|
||||
}""",
|
||||
"operation": "LoadAuthors",
|
||||
"variables": {"author_id": author_id, "limit": limit, "offset": offset},
|
||||
"query": query_type + " " + operation + " { " + query_name + " { authors {" + query_fields + "} } " + " }",
|
||||
"operationName": operation,
|
||||
"variables": None,
|
||||
}
|
||||
|
||||
followings = []
|
||||
try:
|
||||
async with AsyncClient() as client:
|
||||
async with AsyncClient() as client:
|
||||
try:
|
||||
response = await client.post(API_BASE, headers=headers, json=gql)
|
||||
if response.status_code != 200:
|
||||
return []
|
||||
r = response.json()
|
||||
followings = r.get("data", {}).get("authorFollowings", [])
|
||||
more_amount = limit - len(followings)
|
||||
if more_amount > 0:
|
||||
followers = await get_followers(author_id, more_amount)
|
||||
followings.extend(followers)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
except Exception:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(f"[services.core] {query_name}: {response.status_code} {response.text}")
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
r = response.json()
|
||||
authors = r.get("data", {}).get(query_name).get("authors", [])
|
||||
return authors
|
||||
|
||||
return followings
|
||||
|
||||
|
||||
async def get_followers(author_id, amount):
|
||||
gql = {
|
||||
"query": """query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) {
|
||||
authorFollowers(author_id: $author_id, limit: $limit) {
|
||||
id slug userpic name
|
||||
}
|
||||
}""",
|
||||
"operation": "LoadAuthors",
|
||||
"variables": {"author_id": author_id, "limit": amount},
|
||||
}
|
||||
try:
|
||||
async with AsyncClient() as client:
|
||||
response = await client.post(API_BASE, headers=headers, json=gql)
|
||||
if response.status_code != 200:
|
||||
return []
|
||||
r = response.json()
|
||||
return r.get("data", {}).get("authorFollowers", [])
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return []
|
||||
members = get_all_authors() # [ { id slug name userpic }, ..]
|
||||
print(f"[services.core] cache members amount: {len(members)}")
|
||||
members_by_slug = {member['slug']: member for member in members}
|
||||
members_by_id = {member['id']: member for member in members}
|
Reference in New Issue
Block a user