Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,99 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
@app.get("/")
|
| 6 |
def greet_json():
|
| 7 |
-
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
import cloudscraper
|
| 3 |
+
import time
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
airport_codes_set = []
|
| 10 |
+
for x in requests.get(
|
| 11 |
+
"https://www.citilink.co.id/get/airports/8a4b6dac-fa62-4db7-a2b2-04ac5018022c?FECU=aT9La93jtcQfpq2Guw5Vrnj4IhWt8iQz9JYvHEw8%2B%2BK2cKqQHloN7WKCsmUpKL5em6xuCStp1pT%2BeO8qzifwsFr4v2aSs633dQOHphB1enuQ5LXNFfLUBHNAAfceZESXx5ECu0nc4CeXtyaACW1BMlZL4Zw41aqdzX1yEM9vPtBkhpAL14Y7bKd3LAgbHEei75",
|
| 12 |
+
headers={"user-agent":"Googlebot"}
|
| 13 |
+
).json():
|
| 14 |
+
for y in x["destinations"]:
|
| 15 |
+
airport_codes_set.append(json.dumps(y))
|
| 16 |
+
|
| 17 |
+
cookie_string = """userlang=en; session_access_token=eyJraWQiOiIwMFhlOVdGODNHbDJ0anN5Qng5dXk3VktSSDNkQVVLWCJ9.eyJhdWQiOiJ0aWtldC5jb20iLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3LnRpa2V0LmNvbSIsImV4cCI6MTczNTYyODk1MX0.nAMuZRxSuEYCKuPfXI4zvprEaksAvhHIYlB8DGwX6P0R3afqz93LMMr9koXwBOJZ;
|
| 18 |
+
session_refresh_token=eyJraWQiOiJpMXVPV2drM1Q1dlBmb1BDczJCODVBSm1obW40Nk5ETCJ9.eyJhdWQiOiJ0aWtldC5jb20vcnQiLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3…ZHkxNFdfjWxz3veFZHYRN8GhkWFe5UJte_yvcewVVvR7e5qVs5J_JbVzIZaDv1Rb39SEVHcb3ZUKoq5eOTaSKpCm7YA;
|
| 19 |
+
cf_clearance=txA3F6mlyQt1XzsIY6OxCM86BmMswcmyX8XmPpOULc0-1734421561-1.2.1.1-Dqk4hE0_.4Y5OQkD6LPXGgC3Z0dfzvirBDr.209VtndL06wBVt.xUN1EGdQ7LBL50dIR_rE1y58KndTzS832O5lp4Iz9KoR8BIBVWX7tbDLfgaF1sABo4rsb_l_mmThS56EODLjin3QLZAC8RMZ10DFAGI6xgqsBv75uQjx.7Tduj3LbjPfjq14T4V6N6uwKn9Hn05t79vC.z_buGQYSZK2qTKcn3tA.pCQqOMG7RjVcffVbIPFWNOMfuYNrK5GC3mh.uERzdAw2FoOY4ddSDR4biGYxxwMDxI0A5v.DBdp4jgUL_BY5YSWwSfaS89ymKXj9gyplQ.FiRG6LmLRT_g"""
|
| 20 |
+
|
| 21 |
@app.get("/")
|
| 22 |
def greet_json():
|
| 23 |
+
return {"Hello": "World!"}
|
| 24 |
+
|
| 25 |
+
# https://www.tiket.com/ms-gateway/tix-flight-search/v3/search?origin=JKTC&originType=CITY&destination=DPS&destinationType=AIRPORT&adult=1&child=0&infant=0&cabinClass=ECONOMY&departureDate=2024-12-20&flexiFare=true&resultType=DEPARTURE&searchType=ROUND_TRIP&returnDate=2024-12-21&enableVI=true
|
| 26 |
+
|
| 27 |
+
@app.get("/get_flights")
|
| 28 |
+
async def get_flights(
|
| 29 |
+
origin: string ="CGK",
|
| 30 |
+
destination: string ="DPS",
|
| 31 |
+
adult: int = 1,
|
| 32 |
+
child: int = 1,
|
| 33 |
+
infant: int = 1,
|
| 34 |
+
cabinClass: string = "ECONOMY",
|
| 35 |
+
departureDate: string ="2024-12-20",
|
| 36 |
+
resultType: stirng = "DEPARTURE",
|
| 37 |
+
searchType: string = "ROUND_TRIP",
|
| 38 |
+
returnDate: string =2024-12-21',
|
| 39 |
+
# enableVI=true
|
| 40 |
+
):
|
| 41 |
+
global cookie_string
|
| 42 |
+
|
| 43 |
+
url = f"https://www.tiket.com/ms-gateway/tix-flight-search/v3/search?origin={origin}&originType=AIRPORT&destination={destination}&destinationType=AIRPORT&adult={adult}&child={child}&infant={infant}&cabinClass={cabinClass}&departureDate={departureDate}&flexiFare=true&resultType={resultType}&searchType={searchType}&returnDate={returnDate}&enableVI=true"
|
| 44 |
+
scraper = cloudscraper.create_scraper()
|
| 45 |
+
|
| 46 |
+
clean_cookie = cookie_string.replace("\n", "").strip()
|
| 47 |
+
|
| 48 |
+
cookie_dict = {}
|
| 49 |
+
|
| 50 |
+
for item in clean_cookie.split("; "):
|
| 51 |
+
if "=" in item:
|
| 52 |
+
key, value = item.split("=", 1)
|
| 53 |
+
print(key)
|
| 54 |
+
if key == "session_refresh_token":
|
| 55 |
+
continue
|
| 56 |
+
cookie_dict[key] = value.encode('cp1252').decode('utf-8')
|
| 57 |
+
headers = {
|
| 58 |
+
"Accept": "*/*",
|
| 59 |
+
"Content-Type": "application/json",
|
| 60 |
+
"User-Agent":"Googlebot",
|
| 61 |
+
"Authorization": "Bearer " + cookie_dict["session_access_token"],
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
for _ in range(3):
|
| 65 |
+
response = scraper.get(
|
| 66 |
+
url,
|
| 67 |
+
headers=headers,
|
| 68 |
+
cookies=cookie_dict,
|
| 69 |
+
)
|
| 70 |
+
if response.status_code == 200:
|
| 71 |
+
out = response.json()
|
| 72 |
+
data = out["data"]
|
| 73 |
+
return {"results":data["airlines"]}
|
| 74 |
+
else:
|
| 75 |
+
print(f"Error: {response.status_code}")
|
| 76 |
+
time.sleep(1.2)
|
| 77 |
+
|
| 78 |
+
return {"error": {response.status_code}, "raw_text":{response.text}}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@app.get("/airport_codes"):
|
| 82 |
+
async def get_airport_codes():
|
| 83 |
+
global airport_codes_set
|
| 84 |
+
return {"results":[json.loads(x) for x in airport_codes_set]}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# origin=JKTC',
|
| 88 |
+
'originType=',
|
| 89 |
+
'destination=',
|
| 90 |
+
'destinationType=',
|
| 91 |
+
'adult=1',
|
| 92 |
+
'child=0',
|
| 93 |
+
'infant=0',
|
| 94 |
+
'cabinClass=ECONOMY',
|
| 95 |
+
'departureDate=2024-12-17',
|
| 96 |
+
'flexiFare=true',
|
| 97 |
+
'resultType=DEPARTURE',
|
| 98 |
+
'searchType=ONE_WAY',
|
| 99 |
+
'enableVI=true
|