-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi.py
40 lines (33 loc) · 909 Bytes
/
api.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
from flask import Flask, jsonify, render_template, json
import requests
from settings import *
from utils import *
app = Flask(__name__)
@app.route('/pnr/<pnr>')
def pnr_api(pnr):
"""
Returns the PNR data in JSON after fetching from Indian Railways website.
"""
if is_pnr_dummy(pnr):
return jsonify(json.load(open("dummy_response.json","r")))
if is_pnr_valid(pnr):
response = requests.post(BASE_URL, data={PARAM_NAME : pnr})
if response.status_code is 200:
pnr_data = parse_html(response.content)
if not pnr_data:
return jsonify({'status' : 'PNR FLUSHED / SERVICE UNAVAILABLE',
'data' : {}
})
return jsonify({'status' : 'OK',
'data' : build_response_dict(pnr_data)
})
else:
return jsonify({'status' : 'ERROR',
'data' : {}
})
else:
return jsonify({'status' : 'INVALID PNR',
'data' : {}
})
if __name__ == '__main__':
app.run(debug=True)