-
Notifications
You must be signed in to change notification settings - Fork 77
/
medium.py
63 lines (54 loc) · 1.76 KB
/
medium.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
54
55
56
57
58
59
60
61
62
63
try:
import argparse
import requests
import json
except ModuleNotFoundError:
print("Please download dependencies from requirement.txt")
except Exception as ex:
print(ex)
class Medium:
@staticmethod
def build_payload(username):
query = ''
with open('medium_graphql_query.graphql', 'r', encoding='utf-8') as file:
query = file.read()
json_data = [
{
'operationName': 'UserProfileQuery',
'variables': {
'includeDistributedResponses': True,
'id': None,
'username': '@{}'.format(username),
'homepagePostsLimit': 1,
},
'query': query
},
]
return json_data
@staticmethod
def make_request(URL, json_data):
try:
response = requests.post(URL, json=json_data)
if response.status_code == 200:
return response.json()
except Exception as ex:
print('Error at Make Request: {}'.format(ex))
@staticmethod
def scrap(username):
"""scrap medium's profile"""
try:
URL = "https://medium.com/_/graphql"
payload = Medium.build_payload(username)
response = Medium.make_request(URL, payload)
if response:
return json.dumps(response)
else:
print("Failed to make response!")
except Exception as ex:
return {"error": ex}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("username", help="username to search")
args = parser.parse_args()
print(Medium.scrap(args.username))
# last modified on : 24th October, 2022