-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sopython/dev
Dev
- Loading branch information
Showing
9 changed files
with
206 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
|
||
|
||
ACCESS_KEY = os.environ.get('STACKEXCHANGE_REQUESTS_KEY') | ||
SITE = os.environ.get('ERIDU_SITE', 'stackoverflow') | ||
|
||
POST_IDS_URL = "https://api.stackexchange.com/2.2/posts" | ||
POST_IDS_NUMBER = os.environ.get('ERIDU_POST_IDS_NUMBER', 100) | ||
POST_IDS_FILTER = os.environ.get('ERIDU_POST_IDS_FILTER', '!3tz1WbZW5IHcz*twZ') | ||
POST_IDS_SORT = os.environ.get('ERIDU_POST_IDS_SORT', 'creation') | ||
POST_IDS_ORDER = os.environ.get('ERIDU_POST_IDS_ORDER', 'asc') | ||
|
||
QUESTIONS_URL = "https://api.stackexchange.com/2.2/questions/{}" | ||
QUESTIONS_FILTER = os.environ.get('ERIDU_QUESTIONS_FILTER', '!OfZM.T7F9gRuLlvhzHoyC1Fyd3oEOAMszsZJXvHk4mw') | ||
QUESTIONS_SORT = os.environ.get('ERIDU_QUESTIONS_SORT', 'creation') | ||
QUESTIONS_ORDER = os.environ.get('ERIDU_QUESTIONS_ORDER', 'asc') | ||
|
||
ANSWERS_URL = "https://api.stackexchange.com/2.2/answers/{}" | ||
ANSWERS_FILTER = os.environ.get('ERIDU_ANSWERS_FILTER', '!Fcazzsr2b3Mo6cWaRk)J*C-n25') | ||
ANSWERS_SORT = os.environ.get('ERIDU_ANSWERS_SORT', 'creation') | ||
ANSWERS_ORDER = os.environ.get('ERIDU_ANSWERS_ORDER', 'asc') | ||
|
||
FILTER_TAGS = os.environ.get('ERIDU_FILTER_TAGS', 'python,python-2.x,python-3.x') | ||
SECONDS_BETWEEN_REQUESTS = os.environ.get('ERIDU_SECONDS_BETWEEN_REQUESTS', 300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import time | ||
|
||
import requests | ||
|
||
from eridu.logger import logger | ||
from eridu.config import ACCESS_KEY, SITE | ||
from eridu.config import POST_IDS_URL, POST_IDS_FILTER, POST_IDS_NUMBER, POST_IDS_SORT, POST_IDS_ORDER | ||
from eridu.config import QUESTIONS_URL, QUESTIONS_FILTER, QUESTIONS_SORT, QUESTIONS_ORDER | ||
from eridu.config import ANSWERS_URL, ANSWERS_FILTER, ANSWERS_SORT, ANSWERS_ORDER | ||
|
||
|
||
|
||
def get_questions(question_ids, url=QUESTIONS_URL, filter=QUESTIONS_FILTER, access_key=ACCESS_KEY, site=SITE, sort=QUESTIONS_SORT, order=QUESTIONS_ORDER): | ||
url = url.format(';'.join([str(i) for i in question_ids])) | ||
|
||
payload = { | ||
"pagesize": 100, | ||
"key": access_key, | ||
"site": site, | ||
"sort": sort, | ||
"order": order, | ||
"filter": filter, | ||
} | ||
|
||
logger.info('Getting questions with payload: {}'.format(payload)) | ||
|
||
r = requests.get(url, params=payload) | ||
|
||
data = r.json() | ||
|
||
if data.get('backoff') is not None: | ||
time.sleep(int(data.get('backoff'))) | ||
|
||
return data | ||
|
||
def get_answers(answer_ids, url=ANSWERS_URL, filter=ANSWERS_FILTER, access_key=ACCESS_KEY, site=SITE, sort=ANSWERS_SORT, order=ANSWERS_ORDER): | ||
url = url.format(';'.join([str(i) for i in answer_ids])) | ||
|
||
payload = { | ||
"pagesize": 100, | ||
"key": access_key, | ||
"site": site, | ||
"sort": sort, | ||
"order": order, | ||
"filter": filter, | ||
} | ||
|
||
logger.info('Getting answers with payload: {}'.format(payload)) | ||
|
||
r = requests.get(url, params=payload) | ||
|
||
data = r.json() | ||
|
||
question_ids = [answer['question_id'] for answer in data['items']] | ||
|
||
questions = get_questions(question_ids) | ||
|
||
tags = {question['question_id']: question['tags'] for question in questions['items']} | ||
|
||
for answer in data['items']: | ||
answer['tags'] = tags[answer['question_id']] | ||
|
||
if data.get('backoff') is not None: | ||
time.sleep(int(data.get('backoff'))) | ||
|
||
return data | ||
|
||
|
||
def get_post_ids(page, url=POST_IDS_URL, filter=POST_IDS_FILTER, n_posts=POST_IDS_NUMBER, access_key=ACCESS_KEY, site=SITE, sort=POST_IDS_SORT, order=POST_IDS_ORDER): | ||
payload = { | ||
"pagesize": n_posts, | ||
"page": page, | ||
"key": access_key, | ||
"site": site, | ||
"sort": sort, | ||
"order": order, | ||
"filter": filter, | ||
} | ||
logger.info('Getting post ids with payload: {}'.format(payload)) | ||
|
||
r = requests.get(url, params=payload) | ||
|
||
data = r.json() | ||
|
||
if data.get('backoff') is not None: | ||
time.sleep(int(data.get('backoff'))) | ||
|
||
return data | ||
|
||
|
||
def split_post_ids(post_ids): | ||
logger.info('Splitting post ids into question and answer ids') | ||
|
||
question_ids, answer_ids = [], [] | ||
|
||
for item in post_ids: | ||
if item['post_type'] == "question": | ||
question_ids.append(item['post_id']) | ||
elif item['post_type'] == 'answer': | ||
answer_ids.append(item['post_id']) | ||
|
||
return { | ||
"question_ids": question_ids, | ||
"answer_ids": answer_ids | ||
} | ||
|
||
def filter_posts_by_tag(posts, tags): | ||
tags = set(tags) | ||
return [post for post in posts if set(post['tags']) & tags] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import logging | ||
|
||
logger = logging.getLogger('eridu') | ||
|
||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
|
||
ch = logging.StreamHandler() | ||
ch.setFormatter(formatter) | ||
|
||
logger.setLevel(logging.INFO) | ||
logger.addHandler(ch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters