-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
179 lines (145 loc) · 5.02 KB
/
app.py
File metadata and controls
179 lines (145 loc) · 5.02 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from flask import Flask, jsonify
import requests
import feedparser
import time
import json
app = Flask(__name__)
def normalize_reddit_webdev(url):
"""
Takes in a URL string to WebDev Subreddit.
Awaits a fetch coroutine, then normalizes the payload.
Returns the normalized entries.
"""
headers = {
'User-Agent': 'Heroku:42:1.0.0 (by /u/sburger)'
}
print('url start', url)
response = requests.get(url, headers=headers).json()
print('url done', url)
entries = response['data']['children']
normalized_entries = {
'source': 'reddit',
'category': 'webdev',
'data':[]
}
for entry in entries:
normalized_entries['data'].append({
'title': entry['data'].get('title', None),
'link': entry['data'].get('permalink', None),
#some results have thumbnail urls
'thumbnail': entry['data'].get('thumbnail', None),
})
return normalized_entries
def normalize_reddit_programmerhumor(url):
"""
Takes in a URL string to Programmer Humor Subreddit.
Awaits a fetch coroutine, then normalizes the payload.
Returns the normalized entries.
"""
headers = {
'User-Agent': 'Heroku:42:1.0.0 (by /u/sburger)'
}
print('url start', url)
response = requests.get(url, headers=headers).json()
print('url done', url)
entries = response['data']['children']
normalized_entries = {
'source': 'reddit',
'category': 'programmer_humor',
'data':[]
}
for entry in entries:
normalized_entries['data'].append({
'title': entry['data'].get('title', None),
'link': entry['data'].get('permalink', None),
'image': entry['data'].get('url', None),
})
return normalized_entries
def normalize_reddit_no_image(url, category):
"""
Takes in a URL string to Python Subreddit and a category string.
Awaits a fetch coroutine, then normalizes the payload.
Returns the normalized entries.
"""
headers = {
'User-Agent': 'Heroku:42:1.0.0 (by /u/sburger)'
}
print('url start', url)
response = requests.get(url, headers=headers).json()
print('url done', url)
entries = response['data']['children']
normalized_entries = {
'source': 'reddit',
'category': category,
'data':[]
}
for entry in entries:
normalized_entries['data'].append({
'title': entry['data'].get('title', None),
'link': entry['data'].get('permalink', None),
})
return normalized_entries
def normalize_pypi(url, category):
"""
Takes in a URL string to PyPI and a category string.
Awaits a fetch coroutine, then normalizes the payload.
Returns the normalized entries.
"""
print('url start', url)
r = requests.get(url)
feed_data = feedparser.parse(r.content)
print('url done', url)
entries = feed_data.entries
normalized_entries = {
'source': 'pypi',
'category': category,
'data':[]
}
for entry in entries:
normalized_entries['data'].append({
'title': entry['title'],
'link': entry['link'],
'desc': entry['summary']
})
return normalized_entries
def normalize_github(url, category):
"""
Takes in a URL string to GitHub and a category string.
Awaits a fetch coroutine, then normalizes the payload.
Returns the normalized entries.
"""
print('url start', url)
response = requests.get(url).json()
print('url done', url)
entries = response['items']
normalized_entries = {
'source': 'github',
'category': category,
'data':[]
}
for entry in entries:
normalized_entries['data'].append({
'title': entry['name'],
'link': entry['html_url'],
'desc': entry['description'],
'stars': entry['stargazers_count']
})
return normalized_entries
# ======
# ROUTES
# ======
@app.route('/')
def main():
start_time = time.perf_counter()
entries = []
entries.append(normalize_github('https://api.github.com/search/repositories?q=language:python&sort=stars&order=desc', 'popular'))
entries.append(normalize_github('https://api.github.com/search/repositories?q=language:python&sort=updated&order=desc', 'updated'))
entries.append(normalize_reddit_webdev('https://www.reddit.com/r/webdev/.json?'))
entries.append(normalize_reddit_programmerhumor('https://www.reddit.com/r/programmerhumor/.json?'))
entries.append(normalize_reddit_no_image('https://www.reddit.com/r/python/.json?', 'python'))
entries.append(normalize_reddit_no_image('https://www.reddit.com/r/learnprogramming/.json?', 'learnprogramming'))
entries.append(normalize_pypi('https://pypi.org/rss/updates.xml', 'updated'))
entries.append(normalize_pypi('https://pypi.org/rss/packages.xml', 'newest'))
elapsed_time = time.perf_counter() - start_time
print(f'Elapsed time: {elapsed_time:0.2f}')
return jsonify(entries)