-
Notifications
You must be signed in to change notification settings - Fork 2
/
BFF.py
227 lines (200 loc) · 7.77 KB
/
BFF.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import flask
import requests
import json
from feedgen.feed import FeedGenerator
from datetime import datetime
import pytz
app = flask.Flask(__name__)
@app.teardown_appcontext
def close_connection(exception):
print("We are shutting down")
@app.cli.command('init')
def init_db():
with app.app_context():
pass
# gets the 25 most recent posts to a particular community
@app.route('/BFF/<_community>/recent', methods=['GET'])
def recent_posts(_community):
url = 'http://localhost:2015/posts/{community}/recent/{num}'
url = url.format(community = _community, num = 25)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
final_data = data['data']
fg = FeedGenerator()
fg.id(url)
fg.title('Microservice - view community')
fg.link(href=url, rel='alternate')
fg.description('The 25 most recent posts to a particular community')
fg.language('en')
for i in final_data:
fe = fg.add_entry()
fe.id(i['post_id'])
fe.author({'name': i['author'], 'email':""})
fe.title(i['title'])
fe.description(i['community'])
date = datetime.fromtimestamp(i['date'])
date = date.replace(tzinfo=pytz.utc)
fe.published(date)
fe.content(content=i['text'])
if 'url' in i:
fe.source(url=i['url'], title=i['title'])
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
resp = flask.Response(rssfeed, mimetype='text/xml', status = 200, content_type = 'application/rss+xml; charset=UTF-8')
return resp
# gets the 25 most recent posts to any community
@app.route('/BFF/All/recent', methods=['GET'])
def all_recent_posts():
url = 'http://localhost:2015/posts/all/recent/{num}'
url = url.format(num = 25)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
final_data = data['data']
fg = FeedGenerator()
fg.id(url)
fg.title('Microservice - view all')
fg.link( href=url, rel='alternate' )
fg.description('The 25 most recent posts to any community')
fg.language('en')
#This is genating the RSS feeds
for i in final_data:
fe = fg.add_entry()
fe.id(i['post_id'])
fe.author({'name': i['author'], 'email':""})
fe.title(i['title'])
fe.description(i['community'])
date = datetime.fromtimestamp(i['date'])
date = date.replace(tzinfo=pytz.utc)
print(date)
fe.published(date)
fe.content(content=i['text'])
if 'url' in i:
fe.source(url=i['url'], title=i['title'])
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
resp = flask.Response(rssfeed, mimetype='text/xml', status = 200, content_type = 'application/rss+xml; charset=UTF-8')
return resp
# gets the top 25 posts to a particular community, sorted by score
@app.route('/BFF/<_community>/top', methods=['GET'])
def top_posts(_community):
url = 'http://localhost:2015/votes/{community}/top/{num}'
url = url.format(community = _community, num = 25)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
post_id = data['data']
fg = FeedGenerator()
fg.id(url)
fg.title('Microservice - view top community posts')
fg.link( href=url, rel='alternate' )
fg.description('The 25 top posts to a particular community')
fg.language('en')
for i in post_id:
url = 'http://localhost:2015/posts/{community}/post/{id}'
url = url.format(community = _community, id = i)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
post_info = data['data']
fe = fg.add_entry()
fe.id(post_info['post_id'])
fe.author({'name': post_info['author'], 'email':""})
fe.title(post_info['title'])
fe.description(post_info['community'])
date = datetime.fromtimestamp(post_info['date'])
date = date.replace(tzinfo=pytz.utc)
print(date)
fe.published(date)
fe.content(content=post_info['text'])
if 'url' in post_info:
fe.source(url=post_info['url'], title=post_info['title'])
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
resp = flask.Response(rssfeed, mimetype='text/xml', status = 200, content_type = 'application/rss+xml; charset=UTF-8')
return resp
# gets the top 25 posts to any community, sorted by score
@app.route('/BFF/All/top', methods=['GET'])
def all_top_posts():
url = 'http://localhost:2015/votes/all/top/{num}'
url = url.format(num = 25)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
print(str(r.status_code)+ "<<<-----======")
data = r.json()
post_id = data['data']
fg = FeedGenerator()
fg.id(url)
fg.title('Microservice - view all top posts')
fg.link( href=url, rel='alternate' )
fg.description('The 25 top posts to any community')
fg.language('en')
for i in post_id:
print("Does it break here?")
url = 'http://localhost:2015/posts/all/post/{id}'
url = url.format(id = i)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
print("Or Here?")
data = r.json()
post_info = data['data']
fe = fg.add_entry()
fe.id(post_info['post_id'])
fe.author({'name': post_info['author'], 'email':""})
fe.title(post_info['title'])
fe.description(post_info['community'])
date = datetime.fromtimestamp(post_info['date'])
date = date.replace(tzinfo=pytz.utc)
print(date)
fe.published(date)
fe.content(content=post_info['text'])
if 'url' in post_info:
fe.source(url=post_info['url'], title=post_info['title'])
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
resp = flask.Response(rssfeed, mimetype='text/xml', status = 200, content_type = 'application/rss+xml; charset=UTF-8')
return resp
# IN PROGRESS
# gets the top 25 posts to any community, ranking using "hot ranking" alg.
@app.route('/BFF/All/hot', methods=['GET'])
def all_hot_posts():
url = 'http://localhost:2015/votes/all/hot/{num}'
url = url.format(num = 25)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
post_id = data['data']
fg = FeedGenerator()
fg.id(url)
fg.title('Microservice - view all hot posts')
fg.link( href=url, rel='alternate' )
fg.description('The 25 hot posts to any community')
fg.language('en')
for i in post_id:
url = 'http://localhost:2015/posts/all/post/{id}'
url = url.format(id = i)
headers = {'content-type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
post_info = data['data']
fe = fg.add_entry()
fe.id(post_info['post_id'])
fe.author({'name': post_info['author'], 'email':""})
fe.title(post_info['title'])
fe.description(post_info['community'])
date = datetime.fromtimestamp(post_info['date'])
date = date.replace(tzinfo=pytz.utc)
print(date)
fe.published(date)
fe.content(content=post_info['text'])
if 'url' in post_info:
fe.source(url=post_info['url'], title=post_info['title'])
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
resp = flask.Response(rssfeed, mimetype='text/xml', status = 200, content_type = 'application/rss+xml; charset=UTF-8')
return resp
@app.errorhandler(404)
def page_not_found(e):
message = {
'status' : 404,
'message' : '404 not found',
}
resp = jsonify(message)
resp.status_code = 404
return resp