-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoting.py
More file actions
60 lines (52 loc) · 1.65 KB
/
Copy pathvoting.py
File metadata and controls
60 lines (52 loc) · 1.65 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
import sys
import requests
def get_top_question(N, label):
params = {
"order": "desc",
"sort": "votes",
"tagged": label,
"pagesize": N,
"site": "stackoverflow.com",
}
resp = requests.get("https://api.stackexchange.com/2.2/questions", params=params)
return resp.json()
def get_top_answer(N, label):
try:
params = {
"order": "desc",
"sort": "votes",
"pagesize": 1,
"site": "stackoverflow.com",
}
for i in range(int(N)):
title = get_top_question(N, label).get("items")[i].get("title")
question_id = get_top_question(N, label).get("items")[i].get("question_id")
resp_answer = requests.get(
"https://api.stackexchange.com/2.2/questions/{}/answers".format(
question_id
),
params=params,
).json()
answer_id = resp_answer.get("items")[0].get("answer_id")
print("label{}: {}".format(i, title))
print(
"link{}: /https://stackoverflow.com/questions/{}".format(i, answer_id)
)
except IndexError:
print("input another question")
def main():
try:
if len(sys.argv) != 3:
print(
"input 2 arguments by the way: number of questions and question label"
)
sys.exit()
N = int(sys.argv[1])
label = sys.argv[2]
if N <= 0:
print("input number > 0")
get_top_answer(N, label)
except ValueError:
print("input with N is a number!")
if __name__ == "__main__":
main()