2023-10-14 15:11:17 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2023-10-04 20:42:39 +00:00
|
|
|
from httpx import AsyncClient
|
|
|
|
|
|
|
|
from settings import API_BASE
|
2023-10-14 14:55:51 +00:00
|
|
|
from validators.member import ChatMember
|
2023-10-04 20:42:39 +00:00
|
|
|
|
2023-10-11 19:12:55 +00:00
|
|
|
headers = {"Content-Type": "application/json"}
|
2023-11-06 16:25:28 +00:00
|
|
|
INTERNAL_AUTH_SERVER = 'v2.' in API_BASE
|
2023-10-04 20:42:39 +00:00
|
|
|
|
|
|
|
async def get_author(author_id):
|
2023-11-14 13:45:18 +00:00
|
|
|
query_name = "getAuthorById" if INTERNAL_AUTH_SERVER else "GetAuthorById"
|
2023-11-06 16:25:28 +00:00
|
|
|
query_type = "query"
|
|
|
|
operation = "GetAuthorById"
|
|
|
|
query_fields = "id slug userpic name lastSeen" if INTERNAL_AUTH_SERVER else "id slug userpic name last_seen"
|
|
|
|
headers = {"Content-Type": "application/json"} # "Bearer " + removed
|
|
|
|
|
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 15:10:06 +00:00
|
|
|
"variables": {
|
|
|
|
"author_id": author_id
|
|
|
|
},
|
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
|
|
|
|
traceback.print_exc()
|
2023-10-14 14:55:51 +00:00
|
|
|
print(f"[services.core] get_author: {response.status_code} {response.text}")
|
|
|
|
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-10-14 15:11:17 +00:00
|
|
|
a = r.get("data", {}).get("getAuthorById")
|
|
|
|
if a:
|
2023-11-06 16:25:28 +00:00
|
|
|
ts_value = a.get("lastSeen", a.get("last_seen"))
|
|
|
|
dt = datetime.strptime(ts_value, "%Y-%m-%dT%H:%M:%S.%f")
|
2023-10-14 15:11:17 +00:00
|
|
|
timestamp = int(dt.timestamp())
|
2023-11-06 16:25:28 +00:00
|
|
|
a["last_seen"] = timestamp
|
2023-10-14 15:11:17 +00:00
|
|
|
author: ChatMember = a
|
|
|
|
return author
|
2023-10-04 20:42:39 +00:00
|
|
|
|
|
|
|
|
2023-10-14 06:38:12 +00:00
|
|
|
async def get_network(author_id: int, limit: int = 50, offset: int = 0) -> list:
|
2023-10-04 20:42:39 +00:00
|
|
|
gql = {
|
2023-10-14 14:55:51 +00:00
|
|
|
"query": """query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) {
|
|
|
|
authorFollowings(author_id: $author_id, limit: $limit, offset: $offset) {
|
|
|
|
id slug userpic name
|
2023-10-14 11:52:04 +00:00
|
|
|
}
|
2023-10-14 14:55:51 +00:00
|
|
|
}""",
|
2023-10-11 19:12:55 +00:00
|
|
|
"operation": "LoadAuthors",
|
2023-10-14 06:38:12 +00:00
|
|
|
"variables": {"author_id": author_id, "limit": limit, "offset": offset},
|
2023-10-04 20:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
followings = []
|
|
|
|
try:
|
|
|
|
async with AsyncClient() as client:
|
2023-10-14 14:55:51 +00:00
|
|
|
response = await client.post(API_BASE, headers=headers, json=gql)
|
2023-10-04 20:42:39 +00:00
|
|
|
if response.status_code != 200:
|
2023-10-11 19:47:25 +00:00
|
|
|
return []
|
2023-10-04 20:42:39 +00:00
|
|
|
r = response.json()
|
2023-10-11 19:12:55 +00:00
|
|
|
followings = r.get("data", {}).get("authorFollowings", [])
|
2023-10-04 20:42:39 +00:00
|
|
|
more_amount = limit - len(followings)
|
|
|
|
if more_amount > 0:
|
2023-10-13 16:45:30 +00:00
|
|
|
followers = await get_followers(author_id, more_amount)
|
|
|
|
followings.extend(followers)
|
2023-10-04 20:42:39 +00:00
|
|
|
except Exception as e:
|
2023-10-11 19:31:44 +00:00
|
|
|
print(e)
|
2023-10-04 20:42:39 +00:00
|
|
|
|
2023-10-11 19:47:25 +00:00
|
|
|
return followings
|
2023-10-11 19:12:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_followers(author_id, amount):
|
|
|
|
gql = {
|
2023-10-14 14:55:51 +00:00
|
|
|
"query": """query LoadAuthors($author_id: Int!, $limit: Int, $offset: Int) {
|
2023-10-14 11:52:04 +00:00
|
|
|
authorFollowers(author_id: $author_id, limit: $limit) {
|
|
|
|
id slug userpic name
|
|
|
|
}
|
2023-10-14 14:55:51 +00:00
|
|
|
}""",
|
2023-10-11 19:12:55 +00:00
|
|
|
"operation": "LoadAuthors",
|
2023-10-14 06:38:12 +00:00
|
|
|
"variables": {"author_id": author_id, "limit": amount},
|
2023-10-11 19:12:55 +00:00
|
|
|
}
|
|
|
|
try:
|
|
|
|
async with AsyncClient() as client:
|
2023-10-14 14:55:51 +00:00
|
|
|
response = await client.post(API_BASE, headers=headers, json=gql)
|
2023-10-11 19:12:55 +00:00
|
|
|
if response.status_code != 200:
|
2023-10-11 19:47:25 +00:00
|
|
|
return []
|
2023-10-11 19:12:55 +00:00
|
|
|
r = response.json()
|
2023-10-14 14:55:51 +00:00
|
|
|
return r.get("data", {}).get("authorFollowers", [])
|
2023-10-11 19:31:44 +00:00
|
|
|
except Exception as e:
|
2023-10-14 11:52:04 +00:00
|
|
|
print(e)
|
2023-10-14 14:55:51 +00:00
|
|
|
return []
|