-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphql_api.py
53 lines (40 loc) · 1.63 KB
/
graphql_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging
import time
import requests
from utils import check_initialized
class GraphqlAPI():
initialized = False
def __new__(cls):
raise Exception('Do not instantiate this class!')
@classmethod
def init(cls) -> None:
cls.logger = logging.getLogger('api')
while not cls.update_api_data():
time.sleep(10)
cls.initialized = True
@classmethod
def update_api_data(cls):
response = requests.get('https://github.com/fa0311/TwitterInternalAPIDocument/raw/master/docs/json/API.json',
timeout=300)
if response.status_code != 200:
cls.logger.error('Request returned an error: {} {}.'.format(response.status_code, response.text))
return False
json_data = response.json()
if not json_data.get('graphql', {}):
cls.logger.error('Can not get Graphql API data from json')
return False
if not json_data.get('header', {}):
cls.logger.error('Can not get header data from json')
return False
cls.graphql_api_data = json_data['graphql']
cls.headers = json_data['header']
cls.logger.info('Pull GraphQL API data success, API number: {}'.format(len(cls.graphql_api_data)))
return True
@classmethod
@check_initialized
def get_api_data(cls, api_name):
if api_name not in cls.graphql_api_data:
raise ValueError('Unkonw API name: {}'.format(api_name))
api_data = cls.graphql_api_data[api_name]
return api_data['url'], api_data['method'], cls.headers, api_data['features']
GraphqlAPI.init()