inbox/services/core.py
2023-10-11 22:31:44 +03:00

73 lines
2.2 KiB
Python

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