-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstackoverflow_api.py
More file actions
30 lines (26 loc) · 938 Bytes
/
stackoverflow_api.py
File metadata and controls
30 lines (26 loc) · 938 Bytes
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
import os
import configparser
from dotenv import load_dotenv
from stackapi import StackAPI
directory = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.read(f"{directory}/config.ini")
load_dotenv()
API_KEY = str(config.get("stackapi","api_key"))
site = StackAPI("stackoverflow", key=API_KEY)
site.page_size = 5
site.max_pages = 1
def fetch_res(query:str):
responses = site.fetch("search/excerpts", q=query, filter="!nKzQURF6Y5", sort='votes')
res_list = []
for response in responses['items']:
if response['item_type'] == 'question' or response['item_type'] == 'answer':
question = {}
title = f"{response['title']}"
url = f"https://stackoverflow.com/q/{response['question_id']}"
question['title'] = title
question['url'] = url
res_list.append(question)
else:
pass
return res_list