File size: 7,650 Bytes
42a8243
83f87dd
 
 
 
42a8243
 
 
83f87dd
 
 
 
 
 
 
6ad9294
83f87dd
 
 
 
 
5a8e0c2
 
 
 
c0ac703
 
 
 
 
fd9202e
 
5a8e0c2
 
 
 
 
 
 
 
42a8243
 
83f87dd
 
 
 
 
 
d9130bf
 
83f87dd
712ab22
 
db9e4d7
 
 
 
 
744d491
 
83f87dd
 
fbd8905
c0ac703
fd9202e
fbd8905
83f87dd
 
fd9202e
 
db1a40e
 
 
 
 
 
 
c0ac703
fbd8905
5509a9d
9a6bbdd
 
83f87dd
f967020
83f87dd
 
 
fbd8905
83f87dd
 
 
 
9473ef1
83f87dd
 
 
744d491
83f87dd
 
 
 
b60327c
83f87dd
 
 
 
 
 
1d77573
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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