Skip to content

Instantly share code, notes, and snippets.

@SamadiPour
Last active April 24, 2023 17:56
Show Gist options
  • Save SamadiPour/c3cc148472813c2bc9e75ee1d1e1b4f5 to your computer and use it in GitHub Desktop.
Save SamadiPour/c3cc148472813c2bc9e75ee1d1e1b4f5 to your computer and use it in GitHub Desktop.
Alibaba.ir - Check if flight is available and alert in telegram
import hashlib
import os
import requests
# ============== Variables ==============
url = "https://ws.alibaba.ir/api/v1/flights/domestic/available/"
telegram_token = "" # os.environ["TOKEN"]
chat_id = "" # os.environ["CHAT_ID"]
searches = [
'',
]
# ============== Program ==============
headers = {
'ab-channel': 'WEB-NEW,PRODUCTION,CSR,www.alibaba.ir,N,Chrome,104.0.0.0,N,N,Mac OS',
'sec-ch-ua-mobile': '?0',
'Accept': 'application/json, text/plain, */*',
'Referer': 'https://www.alibaba.ir/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
'sec-ch-ua-platform': '"macOS"',
'sec-ch-ua': '"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
}
request_list = [requests.request("GET", f'{url}{search}', headers=headers) for search in searches]
def send_message(message):
send_text = 'https://api.telegram.org/bot' + telegram_token + '/sendMessage?chat_id=' + chat_id \
+ '&parse_mode=Markdown&text=' + message
return requests.request('GET', send_text)
# open alerted flights if exists
if os.path.exists('alerted_flights.txt'):
with open('alerted_flights.txt', 'r') as f:
alerted_flights = f.read().splitlines()
else:
alerted_flights = []
for request in request_list:
try:
flights = request.json()['result']['departing']
for flight in flights:
if flight['seat'] != 0:
origin = flight['originName']
destination = flight['destinationName']
time = flight['leaveDateTime'].split('T')[1][:-3]
date = flight['leaveDateTime'].split('T')[0]
status = flight['statusName']
flight_id = flight['flightId']
if flight_id is not None and flight_id != '' and ':' in flight_id:
f_id = ':'.join(str(flight_id).split(':')[:-1]) + f'${status}'
else:
f_id = f'{origin}:{destination}:{flight["flightNumber"]}:{time}:{date}:{status}'
hash_f_id = hashlib.md5(f_id.encode('utf-8')).hexdigest()
if hash_f_id not in alerted_flights:
message = f'{origin} {destination} - {time}\n{status}\n{date}'
send_message(message)
# save alerted flights
alerted_flights.append(f'{hash_f_id}')
with open('alerted_flights.txt', 'a') as f:
f.write(f'{hash_f_id}\n')
except Exception as e:
print(e)
pass
@SamadiPour
Copy link
Author

Reserved for explanation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment