from fastapi import FastAPI import cloudscraper import time import requests import json app = FastAPI() airport_codes_set = [] for x in requests.get( "https://www.citilink.co.id/get/airports/8a4b6dac-fa62-4db7-a2b2-04ac5018022c?FECU=aT9La93jtcQfpq2Guw5Vrnj4IhWt8iQz9JYvHEw8%2B%2BK2cKqQHloN7WKCsmUpKL5em6xuCStp1pT%2BeO8qzifwsFr4v2aSs633dQOHphB1enuQ5LXNFfLUBHNAAfceZESXx5ECu0nc4CeXtyaACW1BMlZL4Zw41aqdzX1yEM9vPtBkhpAL14Y7bKd3LAgbHEei75", headers={"user-agent":"Googlebot"} ).json(): for y in x["destinations"]: airport_codes_set.append(json.dumps(y)) airport_codes_set = set(airport_codes_set) cookie_string = """userlang=en; session_access_token=eyJraWQiOiIwMFhlOVdGODNHbDJ0anN5Qng5dXk3VktSSDNkQVVLWCJ9.eyJhdWQiOiJ0aWtldC5jb20iLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3LnRpa2V0LmNvbSIsImV4cCI6MTczNTYyODk1MX0.nAMuZRxSuEYCKuPfXI4zvprEaksAvhHIYlB8DGwX6P0R3afqz93LMMr9koXwBOJZ; session_refresh_token=eyJraWQiOiJpMXVPV2drM1Q1dlBmb1BDczJCODVBSm1obW40Nk5ETCJ9.eyJhdWQiOiJ0aWtldC5jb20vcnQiLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3…ZHkxNFdfjWxz3veFZHYRN8GhkWFe5UJte_yvcewVVvR7e5qVs5J_JbVzIZaDv1Rb39SEVHcb3ZUKoq5eOTaSKpCm7YA; cf_clearance=txA3F6mlyQt1XzsIY6OxCM86BmMswcmyX8XmPpOULc0-1734421561-1.2.1.1-Dqk4hE0_.4Y5OQkD6LPXGgC3Z0dfzvirBDr.209VtndL06wBVt.xUN1EGdQ7LBL50dIR_rE1y58KndTzS832O5lp4Iz9KoR8BIBVWX7tbDLfgaF1sABo4rsb_l_mmThS56EODLjin3QLZAC8RMZ10DFAGI6xgqsBv75uQjx.7Tduj3LbjPfjq14T4V6N6uwKn9Hn05t79vC.z_buGQYSZK2qTKcn3tA.pCQqOMG7RjVcffVbIPFWNOMfuYNrK5GC3mh.uERzdAw2FoOY4ddSDR4biGYxxwMDxI0A5v.DBdp4jgUL_BY5YSWwSfaS89ymKXj9gyplQ.FiRG6LmLRT_g""" clean_cookie = cookie_string.replace("\n", "").strip() cookie_dict = {} headers = {'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.5', 'Authorization': 'Bearer eyJraWQiOiIwMFhlOVdGODNHbDJ0anN5Qng5dXk3VktSSDNkQVVLWCJ9.eyJhdWQiOiJ0aWtldC5jb20iLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3LnRpa2V0LmNvbSIsImV4cCI6MTczNTYyODk1MX0.nAMuZRxSuEYCKuPfXI4zvprEaksAvhHIYlB8DGwX6P0R3afqz93LMMr9koXwBOJZ', 'Content-Type': 'application/json', 'Cookie': 'cf_clearance=txA3F6mlyQt1XzsIY6OxCM86BmMswcmyX8XmPpOULc0-1734421561-1.2.1.1-Dqk4hE0_.4Y5OQkD6LPXGgC3Z0dfzvirBDr.209VtndL06wBVt.xUN1EGdQ7LBL50dIR_rE1y58KndTzS832O5lp4Iz9KoR8BIBVWX7tbDLfgaF1sABo4rsb_l_mmThS56EODLjin3QLZAC8RMZ10DFAGI6xgqsBv75uQjx.7Tduj3LbjPfjq14T4V6N6uwKn9Hn05t79vC.z_buGQYSZK2qTKcn3tA.pCQqOMG7RjVcffVbIPFWNOMfuYNrK5GC3mh.uERzdAw2FoOY4ddSDR4biGYxxwMDxI0A5v.DBdp4jgUL_BY5YSWwSfaS89ymKXj9gyplQ.FiRG6LmLRT_g; session_access_token=eyJraWQiOiIwMFhlOVdGODNHbDJ0anN5Qng5dXk3VktSSDNkQVVLWCJ9.eyJhdWQiOiJ0aWtldC5jb20iLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3LnRpa2V0LmNvbSIsImV4cCI6MTczNTYyODk1MX0.nAMuZRxSuEYCKuPfXI4zvprEaksAvhHIYlB8DGwX6P0R3afqz93LMMr9koXwBOJZ; userlang=en', 'User-Agent': 'Googlebot', 'X-Amzn-Trace-Id': 'Root=1-67615322-49c2abfb075610f458a05a02'}} headers = headers["headers"] scraper = cloudscraper.create_scraper() for item in clean_cookie.split("; "): if "=" in item: key, value = item.split("=", 1) if key == "session_refresh_token": continue cookie_dict[key] = value.encode('cp1252').decode('utf-8') @app.get("/") def greet_json(): return {"Hello": "World!"} # 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 @app.get("/get_flights") async def get_flights( origin: str = "CGK", destination: str ="DPS", adult: int = 1, child: int = 0, infant: int = 0, cabinClass: str = "ECONOMY", departureDate: str ="2024-12-20", resultType: str = "DEPARTURE", searchType: str = "ROUND_TRIP", returnDate: str = "2024-12-21", num_retry: int = 1, retry_delay: float = 2.1 # enableVI=true ): # global cookie_dict global headers global scraper # print(cookie_dict) 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" # url = f"https://www.tiket.com/ms-gateway/tix-flight-search/v3/search?origin=CGK&originType=AIRPORT&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" # headers = { # "Accept": "*/*", # "Content-Type": "application/json", # "User-Agent":"Googlebot", # "Authorization": "Bearer " + "eyJraWQiOiIwMFhlOVdGODNHbDJ0anN5Qng5dXk3VktSSDNkQVVLWCJ9.eyJhdWQiOiJ0aWtldC5jb20iLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3LnRpa2V0LmNvbSIsImV4cCI6MTczNTYyODk1MX0.nAMuZRxSuEYCKuPfXI4zvprEaksAvhHIYlB8DGwX6P0R3afqz93LMMr9koXwBOJZ", # # "Authorization": "Bearer " + cookie_dict["session_access_token"], # } # print(cookie_dict["session_access_token"]) # headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.5', 'Authorization': 'Bearer ' + cookie_dict["session_access_token"], 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'Googlebot', 'X-Amzn-Trace-Id': 'Root=1-67615322-49c2abfb075610f458a05a02'} #'Cookie': 'cf_clearance=txA3F6mlyQt1XzsIY6OxCM86BmMswcmyX8XmPpOULc0-1734421561-1.2.1.1-Dqk4hE0_.4Y5OQkD6LPXGgC3Z0dfzvirBDr.209VtndL06wBVt.xUN1EGdQ7LBL50dIR_rE1y58KndTzS832O5lp4Iz9KoR8BIBVWX7tbDLfgaF1sABo4rsb_l_mmThS56EODLjin3QLZAC8RMZ10DFAGI6xgqsBv75uQjx.7Tduj3LbjPfjq14T4V6N6uwKn9Hn05t79vC.z_buGQYSZK2qTKcn3tA.pCQqOMG7RjVcffVbIPFWNOMfuYNrK5GC3mh.uERzdAw2FoOY4ddSDR4biGYxxwMDxI0A5v.DBdp4jgUL_BY5YSWwSfaS89ymKXj9gyplQ.FiRG6LmLRT_g; session_access_token=eyJraWQiOiIwMFhlOVdGODNHbDJ0anN5Qng5dXk3VktSSDNkQVVLWCJ9.eyJhdWQiOiJ0aWtldC5jb20iLCJzdWIiOiI2NzYxMjM5NzkzZGNjODJhNWZmNDE1YTMiLCJuYmYiOjE3MzQ0MTkzNTEsImlzcyI6Imh0dHBzOi8vd3d3LnRpa2V0LmNvbSIsImV4cCI6MTczNTYyODk1MX0.nAMuZRxSuEYCKuPfXI4zvprEaksAvhHIYlB8DGwX6P0R3afqz93LMMr9koXwBOJZ; userlang=en', #'Host': 'httpbin.org', 'User-Agent': 'Googlebot', 'X-Amzn-Trace-Id': 'Root=1-67615322-49c2abfb075610f458a05a02'} for _ in range(num_retry): response = scraper.get( url, headers=headers, # cookies=cookie_dict, ) if response.status_code == 200: out = response.json() data = out["data"] print("SUCCESS!!") return {"results":data["airlines"]} else: print(f"Error: {response.status_code}") time.sleep(retry_delay) return {"error": {response.status_code}, "raw_text":{response.text}} @app.get("/airport_codes") async def get_airport_codes(): global airport_codes_set return {"results":[json.loads(x) for x in airport_codes_set]} # origin=JKTC', # 'originType=', # 'destination=', # 'destinationType=', # 'adult=1', # 'child=0', # 'infant=0', # 'cabinClass=ECONOMY', # 'departureDate=2024-12-17', # 'flexiFare=true', # 'resultType=DEPARTURE', # 'searchType=ONE_WAY', # 'enableVI=true