|
| 1 | +import time |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from eridu.logger import logger |
| 6 | +from eridu.config import ACCESS_KEY, SITE |
| 7 | +from eridu.config import POST_IDS_URL, POST_IDS_FILTER, POST_IDS_NUMBER, POST_IDS_SORT, POST_IDS_ORDER |
| 8 | +from eridu.config import QUESTIONS_URL, QUESTIONS_FILTER, QUESTIONS_SORT, QUESTIONS_ORDER |
| 9 | +from eridu.config import ANSWERS_URL, ANSWERS_FILTER, ANSWERS_SORT, ANSWERS_ORDER |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +def get_questions(question_ids, url=QUESTIONS_URL, filter=QUESTIONS_FILTER, access_key=ACCESS_KEY, site=SITE, sort=QUESTIONS_SORT, order=QUESTIONS_ORDER): |
| 14 | + url = url.format(';'.join([str(i) for i in question_ids])) |
| 15 | + |
| 16 | + payload = { |
| 17 | + "pagesize": 100, |
| 18 | + "key": access_key, |
| 19 | + "site": site, |
| 20 | + "sort": sort, |
| 21 | + "order": order, |
| 22 | + "filter": filter, |
| 23 | + } |
| 24 | + |
| 25 | + logger.info('Getting questions with payload: {}'.format(payload)) |
| 26 | + |
| 27 | + r = requests.get(url, params=payload) |
| 28 | + |
| 29 | + data = r.json() |
| 30 | + |
| 31 | + if data.get('backoff') is not None: |
| 32 | + time.sleep(int(data.get('backoff'))) |
| 33 | + |
| 34 | + return data |
| 35 | + |
| 36 | +def get_answers(answer_ids, url=ANSWERS_URL, filter=ANSWERS_FILTER, access_key=ACCESS_KEY, site=SITE, sort=ANSWERS_SORT, order=ANSWERS_ORDER): |
| 37 | + url = url.format(';'.join([str(i) for i in answer_ids])) |
| 38 | + |
| 39 | + payload = { |
| 40 | + "pagesize": 100, |
| 41 | + "key": access_key, |
| 42 | + "site": site, |
| 43 | + "sort": sort, |
| 44 | + "order": order, |
| 45 | + "filter": filter, |
| 46 | + } |
| 47 | + |
| 48 | + logger.info('Getting answers with payload: {}'.format(payload)) |
| 49 | + |
| 50 | + r = requests.get(url, params=payload) |
| 51 | + |
| 52 | + data = r.json() |
| 53 | + |
| 54 | + question_ids = [answer['question_id'] for answer in data['items']] |
| 55 | + |
| 56 | + questions = get_questions(question_ids) |
| 57 | + |
| 58 | + tags = {question['question_id']: question['tags'] for question in questions['items']} |
| 59 | + |
| 60 | + for answer in data['items']: |
| 61 | + answer['tags'] = tags[answer['question_id']] |
| 62 | + |
| 63 | + if data.get('backoff') is not None: |
| 64 | + time.sleep(int(data.get('backoff'))) |
| 65 | + |
| 66 | + return data |
| 67 | + |
| 68 | + |
| 69 | +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): |
| 70 | + payload = { |
| 71 | + "pagesize": n_posts, |
| 72 | + "page": page, |
| 73 | + "key": access_key, |
| 74 | + "site": site, |
| 75 | + "sort": sort, |
| 76 | + "order": order, |
| 77 | + "filter": filter, |
| 78 | + } |
| 79 | + logger.info('Getting post ids with payload: {}'.format(payload)) |
| 80 | + |
| 81 | + r = requests.get(url, params=payload) |
| 82 | + |
| 83 | + data = r.json() |
| 84 | + |
| 85 | + if data.get('backoff') is not None: |
| 86 | + time.sleep(int(data.get('backoff'))) |
| 87 | + |
| 88 | + return data |
| 89 | + |
| 90 | + |
| 91 | +def split_post_ids(post_ids): |
| 92 | + logger.info('Splitting post ids into question and answer ids') |
| 93 | + |
| 94 | + question_ids, answer_ids = [], [] |
| 95 | + |
| 96 | + for item in post_ids: |
| 97 | + if item['post_type'] == "question": |
| 98 | + question_ids.append(item['post_id']) |
| 99 | + elif item['post_type'] == 'answer': |
| 100 | + answer_ids.append(item['post_id']) |
| 101 | + |
| 102 | + return { |
| 103 | + "question_ids": question_ids, |
| 104 | + "answer_ids": answer_ids |
| 105 | + } |
| 106 | + |
| 107 | +def filter_posts_by_tag(posts, tags): |
| 108 | + tags = set(tags) |
| 109 | + return [post for post in posts if set(post['tags']) & tags] |
0 commit comments