jonahkh
2020-05-13 4829e49af4e2cd4d8c8dfe0c215bd9f3d65efdfb
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
import json
import random
from flask import abort, Flask, jsonify
from flask_cors import CORS
from markupsafe import escape
 
app = Flask(__name__)
CORS(app)
 
NUM_OF_NEWS = 3
 
def sortByTimestamp(element):
  return element['timestamp']
 
@app.errorhandler(404)
def topicNotFound(error):
    return "Unable to find the specific topic", 404
 
@app.route('/news/<string:topic>')
def getNewsForTopic(topic):
    try:
        # Loading the file which has the topic news
        with open('./data/%s.json' % escape(topic)) as topicFile:
            news = json.load(topicFile)
 
        # Picking only a few elements from the list of news
        randomSelection = random.sample(news['data'], NUM_OF_NEWS)
 
        # Sorting the resulting list by the timestamp
        randomSelection.sort(key=sortByTimestamp)
 
        return jsonify(randomSelection)
 
    # If we can not find a file for the topic, we throw a 404 error
    except IOError:
        abort(404)