inbox/services/core.py

55 lines
1.8 KiB
Python
Raw Normal View History

2023-10-04 20:42:39 +00:00
from httpx import AsyncClient
from settings import API_BASE
async def get_author(author_id):
gql = {
"query": "{ getAuthor(author_id: %s) { id slug userpic name lastSeen } }"
% author_id
}
headers = {"Content-Type": "application/json"}
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:
pass
async def get_network(author_id, limit=50, offset=0):
headers = {"Content-Type": "application/json"}
gql = {
"query": "{ authorFollowings(author_id: %s, limit: %s, offset: %s) { id slug userpic name } }"
% (author_id, limit, offset)
}
followings = []
followers = []
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()
followings = r.get("data", {}).get("authorFollowers", [])
more_amount = limit - len(followings)
if more_amount > 0:
gql = {
"query": "{ authorFollowers(author_id: %s, limit: %s) { id slug userpic name } }"
% (author_id, more_amount)
}
response = await client.post(API_BASE, headers=headers, data=gql)
if response.status_code != 200:
return False, None
r = response.json()
followers = r.get("data", {}).get("authorFollowers", [])
except Exception as e:
pass
return followings + followers