-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (38 loc) · 1.42 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
import requests
import re
import json
from fastapi import FastAPI
def get_pnr_status(pnr):
# Specify the URL of the website
url = f"https://www.confirmtkt.com/pnr-status/{pnr}"
# Send an HTTP request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Extract the HTML content
html_content = response.text
# Define a regular expression pattern to match the desired data structure
pattern = r'data\s*=\s*({.*?;)'
# Search for the pattern in the HTML content
match = re.search(pattern, html_content, re.DOTALL)
# Check if a match is found
if match:
# Extract the JSON data
json_data = match.group(1).replace(';', '')
try:
# Load the JSON string to get a Python object
parsed_data = json.loads(json_data)
return parsed_data
except json.JSONDecodeError as e:
print("JSON decoding error:", e)
return None
else:
print("No JSON data found on the webpage.")
else:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")
return None
app = FastAPI()
@app.get("/status")
async def prn_status(pnr: str):
prn_json = get_pnr_status(pnr)
return prn_json