forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigetpost.py
70 lines (65 loc) · 2.55 KB
/
apigetpost.py
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
import requests
from parsing import fetch_post_id_and_site_from_url, url_to_shortlink
from globalvars import GlobalVars
import time
import HTMLParser
class PostData:
def __init__(self):
self.post_id = None
self.post_url = None
self.post_type = None
self.site = None
self.owner_url = None
self.owner_name = None
self.owner_rep = None
self.title = None
self.body = None
self.score = None
self.up_vote_count = None
self.down_vote_count = None
self.question_id = None
def api_get_post(post_url):
# Respect backoff, if we were given one
if GlobalVars.api_backoff_time > time.time():
time.sleep(GlobalVars.api_backoff_time - time.time() + 2)
d = fetch_post_id_and_site_from_url(post_url)
if d is None:
return None
post_id, site, post_type = d
if post_type == "answer":
api_filter = "!FdmhxNQy0ZXsmxUOvWMVSbuktT"
req_url = "http://api.stackexchange.com/2.2/answers/" + post_id +\
"?site=" + site + "&filter=" + api_filter +\
"&key=IAkbitmze4B8KpacUfLqkw(("
resp_json = requests.get(req_url).json()
else:
assert post_type == "question"
api_filter = "!)Ehu.SHRfXhu2eCP4p6wd*Wxyw1XouU5qO83b7X5GQK6ciVat"
req_url = "http://api.stackexchange.com/2.2/questions/" + post_id +\
"?site=" + site + "&filter=" + api_filter +\
"&key=IAkbitmze4B8KpacUfLqkw(("
resp_json = requests.get(req_url).json()
if "backoff" in resp_json:
if GlobalVars.api_backoff_time < time.time() + resp_json["backoff"]:
GlobalVars.api_backoff_time = time.time() + resp_json["backoff"]
if 'items' not in resp_json or len(resp_json['items']) == 0:
return False
item = resp_json['items'][0]
post_data = PostData()
post_data.post_id = post_id
post_data.post_url = url_to_shortlink(item['link'])
post_data.post_type = post_type
h = HTMLParser.HTMLParser()
post_data.title = h.unescape(item['title'])
if 'owner' in item and 'owner' is not None:
post_data.owner_name = item['owner']['display_name']
post_data.owner_url = item['owner']['link']
post_data.owner_rep = item['owner']['reputation']
post_data.site = site
post_data.body = item['body']
post_data.score = item['score']
post_data.up_vote_count = item['up_vote_count']
post_data.down_vote_count = item['down_vote_count']
if post_type == "answer":
post_data.question_id = item['question_id']
return post_data