-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests_utils.py
85 lines (67 loc) · 2.66 KB
/
requests_utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
from bs4 import BeautifulSoup
HN_SEARCH_QUERY = "http://hn.algolia.com/api/v1/search?query={}"
HN_FOR_HUMAN_SEARCH_QUERY = "https://hn.algolia.com/?query={}&sort=byPopularity&prefix=false&page=0&dateRange=all&type=all"
REDDIT_SEARCH_QUERY = "https://www.reddit.com/search.json?q={}"
REDDIT_SEARCH_FOR_HUMAN = "https://www.reddit.com/r/search/search?q={}"
BASE_TRENDING_URL = "https://github.com/trending/python?since={}"
trending_times = ["daily", "weekly", "monthly"]
import os, json
import praw
REDDIT_CLIENT_ID = None
REDDIT_CLIENT_SECRET = None
REDDIT_USER_AGENT = None
if REDDIT_CLIENT_SECRET:
reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_USER_AGENT)
else:
reddit = None
GITHUB_API_TOKEN = None
def requests_with_cache(dir):
def decorator(func):
def wrapper(**kwargs):
cache_key = str(kwargs.get("param", "default.json"))
cache_url = dir + "/" + cache_key.replace("/", "-").replace("_", "-")
if os.path.isfile(cache_url):
with open(cache_url, 'r') as f:
print(cache_url)
return json.load(f)
with open(cache_url, 'w+') as f:
ret = func(**kwargs)
json.dump(ret, f)
return ret
return wrapper
return decorator
@requests_with_cache("github_trending_cache")
def get_trending_on_github():
trending_on_python = {}
for trending_time in trending_times:
trend_url = BASE_TRENDING_URL.format(trending_time)
trending_on_python[trend_url] = []
r = requests.get(trend_url)
r.raise_for_status()
soup = BeautifulSoup(r.text, 'html.parser')
all_h3 = soup.find_all("h3")
for h3 in all_h3:
link = h3.find(href=True).get("href")
trending_on_python[link.split("/", 1)[1]] = trend_url
return trending_on_python
@requests_with_cache("github_cache")
def download_github_repo_data(param):
owner, name = param.split("/")
url = "https://api.github.com/repos/{}/{}".format(owner, name)
headers = {'Authorization': 'token %s' % GITHUB_API_TOKEN} if GITHUB_API_TOKEN else None
r = requests.get(url, headers=headers)
return r.json()
@requests_with_cache("reddit_cache")
def get_reddit_mention(param):
if reddit:
has_match = bool(reddit.get("search.json?q={}".format(param)).children)
return has_match
@requests_with_cache("hn_cache")
def get_hn_mention(param, url=HN_SEARCH_QUERY):
url = HN_SEARCH_QUERY.format(param)
r = requests.get(url)
r.raise_for_status()
return r.json()