gql-format-fix

This commit is contained in:
2023-10-11 22:12:55 +03:00
parent 444b504c95
commit 05967c1ce0
2 changed files with 56 additions and 43 deletions

View File

@@ -1,14 +1,19 @@
import json
from httpx import AsyncClient
from settings import API_BASE
headers = {"Content-Type": "application/json"}
async def get_author(author_id):
gql = {
"query": "{ getAuthor(author_id: %s) { id slug userpic name lastSeen } }"
% author_id
"query": "query GetAuthor { getAuthor(author_id: %s) { id slug userpic name lastSeen } }"
% author_id,
"operation": "GetAuthor",
"variables": None
}
headers = {"Content-Type": "application/json"}
try:
async with AsyncClient() as client:
response = await client.post(API_BASE, headers=headers, data=gql)
@@ -18,37 +23,50 @@ async def get_author(author_id):
author = r.get("data", {}).get("getAuthor")
return author
except Exception:
pass
return None
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)
"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=gql)
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("authorFollowers", [])
followings = r.get("data", {}).get("authorFollowings", [])
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", [])
followers = get_followers(author_id, more_amount)
except Exception as e:
pass
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:
followers = []
return followers