/
app.py
48 lines (40 loc) · 1.6 KB
/
app.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
from flask import Flask, jsonify, Response
from scrape import scrape_news_articles, scrape_news_archives
import json
app = Flask(__name__)
# CORS
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
# Main Index
@app.route('/', methods=['GET'])
def get_home():
return jsonify({
'author': 'Sandip Bhagat',
'author_url': 'http://sandipbgt.com',
'base_url': 'https://indian-embassy-news.herokuapp.com',
'api_url': 'https://indian-embassy-news.herokuapp.com/api',
'project_name': 'indian-embassy-news',
'project_demo': 'http://sandipbgt.com/indian-embassy-news',
'project_url': 'https://github.com/sandipbgt/indian-embassy-news',
})
# API Index
@app.route('/api', methods=['GET'])
def get_api_home():
return jsonify({
'articles': 'https://indian-embassy-news.herokuapp.com/api/articles',
'archives': 'https://indian-embassy-news.herokuapp.com/api/archives',
})
# Articles API
@app.route('/api/articles', methods=['GET'])
def get_articles():
return Response(json.dumps({'articles': scrape_news_articles()}), mimetype='application/json')
# Archives API
@app.route('/api/archives', methods=['GET'])
def get_archives():
return Response(json.dumps({'archives': scrape_news_archives()}), mimetype='application/json')
if __name__ == '__main__':
app.run()