forked from CUBigDataClass/newsbuff-datafetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (77 loc) · 3.19 KB
/
main.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
import json, sys
from datetime import datetime, timedelta
import pymongo
from bson import ObjectId
from flask import Flask, Response, request
from flask_cors import CORS
from flask_restx import Api, Resource
import mongodb
# import emitlogsBackend
# rabbitMQChannel, rabbitMQ = emitlogsBackend.fetchConnection()
app = Flask(__name__)
CORS(app)
api = Api(app,
version = "1.0",
title = "News Buff Backend",
description = "REST APIs for fetching news articles")
name_space = api.namespace('api', description='Main APIs')
client = mongodb.dbConnection()
mydb = mongodb.createdb(client)
articleCollection = mydb["article"]
locationCollection = mydb["location"]
class MongoDbEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat() + 'Z'
if isinstance(obj, ObjectId):
return str(obj)
return json.JSONEncoder.default(self, obj)
@name_space.route("/ping")
class Class1(Resource):
@api.doc(responses={ 200: 'OK' })
def get(self):
return Response(
json.dumps({ "success": True }),
mimetype='application/json'
)
@name_space.route("/<int:year>/<int:month>/<int:day>")
class Class2(Resource):
@api.doc(responses={ 200: 'OK', 400: 'Invalid parameters', 500: 'Something went wrong' },
params={ 'year': 'Year', 'month': 'Month', 'day': 'Day',
'polygon': {'in': 'query', 'description': 'GeoJSON Polygon with a Single Ring'} })
def get(self, year, month, day):
params = request.args
try:
startDate = datetime(int(year), int(month), int(day))
endDate = startDate + timedelta(days=1)
except:
errorInfo = sys.exc_info()
print(errorInfo)
return Response(
json.dumps({ "success": False, "count": 0, "rows": [] }),
mimetype='application/json'
)
try:
query = { "dateTime": { "$gte": startDate, "$lt": endDate } }
if 'polygon' in params:
polygon = json.loads(params['polygon'])
# polygon = [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
query['locations'] = { '$elemMatch': {'location': { '$geoWithin': { '$geometry': { 'type': 'Polygon', 'coordinates': polygon } } } } }
results = articleCollection.find(query, {'_id': False}).sort('dateTime', pymongo.DESCENDING).limit(100)
resultsList = list(results)
resultsCount = len(resultsList)
# emitlogsBackend.log_info(f'year: {year}, month: {month}, day: {day}, count: {resultsCount}', rabbitMQChannel)
return Response(
json.dumps({ "success": True, "count": resultsCount, "rows": resultsList }, cls=MongoDbEncoder),
mimetype='application/json'
)
except:
errorInfo = sys.exc_info()
print(errorInfo)
# emitlogsBackend.log_error(errorInfo, rabbitMQChannel)
return Response(
json.dumps({ "success": False, "count": 0, "rows": [] }),
mimetype='application/json'
)
if __name__ == '__main__':
app.run(debug=True)