-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathso.py
More file actions
85 lines (68 loc) · 2.33 KB
/
so.py
File metadata and controls
85 lines (68 loc) · 2.33 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import argparse
import requests
import html
parser = argparse.ArgumentParser()
parser.add_argument('question_number', type=int)
parser.add_argument('label', type=str)
args = parser.parse_args()
session = requests.Session()
URL = 'https://api.stackexchange.com/2.2/questions'
class QuotaOverflow(Exception):
pass
def get_pagesizes_range(number):
result = []
for _ in range(number // 100):
result.append(100)
if number % 100 != 0:
result.append(number % 100)
return result
def top_questions(number, label):
pagesizes = get_pagesizes_range(number)
try:
top_ques = []
for page, pagesize in enumerate(pagesizes, 1):
params = {"page": page,
"pagesize": pagesize,
"order": "desc",
"sort": "votes",
"tagged": label,
"site": "stackoverflow"}
resp = session.get(URL, params=params).json()["items"]
for item in resp:
title = html.unescape(item["title"])
question_id = item['question_id']
top_ques.append((title, question_id))
return top_ques
except Exception:
raise QuotaOverflow("only make 300 requests per day")
def top_answer(question_id):
link = f'{URL}/{question_id}/answers'
params = {"pagesize": 1,
"order": "desc",
"sort": "votes",
"site": "stackoverflow"}
try:
resp = session.get(link, params=params).json()["items"]
answer = resp[0]['answer_id']
return answer
except Exception:
raise QuotaOverflow("only make 300 requests per day")
def main():
'''
script takes the top ** N ** highest voted question of the tag ** LABEL **
on stackoverflow.com.
'''
label = args.label
question_number = args.question_number
print(f'Top {question_number} questions with tag {label}')
if question_number >= 300:
print("only make 300 requests per day")
return
questions = top_questions(question_number, label)
for title, question_id in questions:
answer_id = top_answer(question_id)
link_to_answer = f'https://stackoverflow.com/a/{answer_id}'
print(title, ",answer:", link_to_answer)
#python3 N LABEL
if __name__ == "__main__":
main()