2023-10-11 19:12:55 +00:00
|
|
|
import json
|
|
|
|
|
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-10-04 20:42:39 +00:00
|
|
|
|
|
|
|
async def get_author(author_id):
|
|
|
|
gql = {
|
2023-10-12 12:15:06 +00:00
|
|
|
"query": "query GetAuthor { getAuthor(author_id: Int!) { id slug userpic name lastSeen } }",
|
2023-10-11 19:12:55 +00:00
|
|
|
"operation": "GetAuthor",
|
2023-10-12 12:15:06 +00:00
|
|
|
"variables": { "author_id" : author_id }
|
2023-10-04 20:42:39 +00:00
|
|
|
}
|
|
|
|
try:
|
|
|
|
async with AsyncClient() as client:
|
|
|
|
response = await client.post(API_BASE, headers=headers, data=gql)
|
|
|
|
if response.status_code != 200:
|
|
|
|
return False, None
|
|
|
|
r = response.json()
|
|
|
|
author = r.get("data", {}).get("getAuthor")
|
|
|
|
return author
|
|
|
|
except Exception:
|
2023-10-11 19:12:55 +00:00
|
|
|
return None
|
2023-10-04 20:42:39 +00:00
|
|
|
|
|
|
|
|
2023-10-11 19:47:25 +00:00
|
|
|
async def get_network(author_id, limit=50, offset=0) -> list:
|
2023-10-04 20:42:39 +00:00
|
|
|
gql = {
|
2023-10-12 12:15:06 +00:00
|
|
|
"query": "query LoadAuthors { authorFollowings(author_id: Int!, limit: Int, offset: Int) { id slug userpic name } }",
|
2023-10-11 19:12:55 +00:00
|
|
|
"operation": "LoadAuthors",
|
2023-10-12 12:15:06 +00:00
|
|
|
"variables": {
|
|
|
|
"author_id": author_id,
|
|
|
|
"limit": limit,
|
|
|
|
"offset": offset
|
|
|
|
}
|
2023-10-04 20:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
followings = []
|
|
|
|
followers = []
|
|
|
|
try:
|
|
|
|
async with AsyncClient() as client:
|
2023-10-11 19:12:55 +00:00
|
|
|
response = await client.post(API_BASE, headers=headers, data=json.dumps(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-11 19:12:55 +00:00
|
|
|
followers = get_followers(author_id, more_amount)
|
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
|
|
|
followings.extend(followers)
|
|
|
|
|
|
|
|
return followings
|
2023-10-11 19:12:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_followers(author_id, amount):
|
|
|
|
gql = {
|
2023-10-12 12:15:06 +00:00
|
|
|
"query": "query LoadAuthors { authorFollowers(author_id: Int!, limit: Int) { id slug userpic name } }",
|
2023-10-11 19:12:55 +00:00
|
|
|
"operation": "LoadAuthors",
|
2023-10-12 12:15:06 +00:00
|
|
|
"variables": {
|
|
|
|
"author_id": author_id,
|
|
|
|
"limit": amount
|
|
|
|
}
|
2023-10-11 19:12:55 +00:00
|
|
|
}
|
|
|
|
followers = []
|
|
|
|
try:
|
|
|
|
async with AsyncClient() as client:
|
|
|
|
response = await client.post(API_BASE, headers=headers, data=json.dumps(gql))
|
|
|
|
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()
|
|
|
|
followers = r.get("data", {}).get("authorFollowers", [])
|
2023-10-11 19:31:44 +00:00
|
|
|
except Exception as e:
|
2023-10-11 19:12:55 +00:00
|
|
|
followers = []
|
|
|
|
return followers
|