-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsky_app.py
118 lines (106 loc) · 5.36 KB
/
bsky_app.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
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
import time
from bs4 import BeautifulSoup
import requests
import os
import math
import csv
from datetime import datetime, timedelta
import subprocess
from app import pull_data, score, push_to_github, pull_tsa
from atproto import Client
BSKY_USERNAME = os.getenv("BSKY_USERNAME")
BSKY_PASSWORD = os.getenv("BSKY_PASSWORD")
TSA_API_KEY = os.getenv("TSA_API_KEY")
client = Client()
client.login(BSKY_USERNAME, BSKY_PASSWORD)
score_metric, most_delayed, most_cancelled, delayed_by_airline, cancelled_by_airline, delayed, cancelled, ontime, total_flights, average_general_wait, average_precheck_wait, average_overall_wait = score(pull_data())
def bsky_post(post_text):
"""
Posts the given text to Bluesky using the AT Protocol SDK.
"""
try:
client.send_post(text=post_text)
print(f"Posted to Bluesky: {post_text}")
except Exception as e:
print(f"Error: {e}")
raise
def post_bsky_status(debug):
"""
Pulls the score and posts the scoring metric on X.
"""
if total_flights == 0: # found out that the the script breaks if theres 0 flights, so added this redundancy
print("No flights found. Skipping tweet.")
return
if score_metric == 0:
neutral = f"💤 MCO is SLEEPING! The airport doesn't have any upcoming flights right now."
if not debug:
bsky_post(neutral)
else:
print(f"Debug Mode: {neutral}")
elif score_metric <= 0.6:
badtext = f"💔 MCO is having a BAD day. Out of {total_flights} upcoming flights:\n\t\n\t⚠️ {delayed} are delayed\n\t⛔️ {cancelled} are cancelled\n\t✅ {ontime} are on time\n\t\n\t🛂 TSA General Avg: {average_general_wait} mins\n\t⏩ TSA PreCheck Avg: {average_precheck_wait} mins\n\t\n\t‼️ Most Cancellations: {most_cancelled}\n\t❗️ Most Delays: {most_delayed}\n\t\n\tScore: {score_metric:.2f}"
if not debug:
bsky_post(badtext)
else:
print(f"Debug Mode: {badtext}")
print(score_metric)
elif 0.6 < score_metric <= 0.8:
oktext = f"❤️🩹 MCO is having an OK day. Out of {total_flights} upcoming flights:\n\t\n\t⚠️ {delayed} are delayed\n\t⛔️ {cancelled} are cancelled\n\t✅ {ontime} are on time\n\t\n\t🛂 TSA General Avg: {average_general_wait} mins\n\t⏩ TSA PreCheck Avg: {average_precheck_wait} mins\n\t\n\t‼️ Most Cancellations: {most_cancelled}\n\t❗️ Most Delays: {most_delayed}\n\t\n\tScore: {score_metric:.2f}"
if not debug:
bsky_post(oktext)
else:
print(f"Debug Mode: {oktext}")
print(score_metric)
elif score_metric > 0.8:
goodtext = f"❤️ MCO is having a GOOD day. Out of {total_flights} upcoming flights:\n\t\n\t⚠️ {delayed} are delayed\n\t⛔️ {cancelled} are cancelled\n\t✅ {ontime} are on time\n\t\n\t🛂 TSA General Avg: {average_general_wait} mins\n\t⏩ TSA PreCheck Avg: {average_precheck_wait} mins\n\t\n\t‼️ Most Cancellations: {most_cancelled}\n\t❗️ Most Delays: {most_delayed}\n\t\n\tScore: {score_metric:.2f}"
if not debug:
bsky_post(goodtext)
else:
print(f"Debug Mode: {goodtext}")
print(score_metric)
if __name__ == "__main__":
while True:
try:
# recalculate data on every loop iteration
flights = pull_data()
tsa_data = pull_tsa()
if not tsa_data or not flights:
print("Data not available. Retrying after delay.")
time.sleep(5400)
continue
(score_metric, most_delayed, most_cancelled, delayed_by_airline, cancelled_by_airline,
delayed, cancelled, ontime, total_flights, average_general_wait, average_precheck_wait,
average_overall_wait) = score(flights)
post_bsky_status(debug=False)
print(f"Most delayed: {most_delayed}")
print(f"Most cancelled: {most_cancelled}")
# write data to CSV
csv_row = {
'timestamp': (datetime.now() + timedelta(hours=1)).isoformat(), # adding 1 to move from CST to EST
'score_metric': score_metric,
'most_delayed': most_delayed,
'most_cancelled': most_cancelled,
'delayed_by_airline': str(delayed_by_airline),
'cancelled_by_airline': str(cancelled_by_airline),
'delayed': delayed,
'cancelled': cancelled,
'ontime': ontime,
'total_flights': total_flights,
'average_general_wait': average_general_wait,
'average_precheck_wait': average_precheck_wait,
'average_overall_wait': average_overall_wait,
'open_checkpoints': tsa_data['open_checkpoints_count'],
'lane_wait_times': str(tsa_data['lane_wait_times']),
'source':'BSKY'
}
file_exists = os.path.isfile('history.csv')
with open('history.csv', 'a', newline='') as f:
writer = csv.DictWriter(f, fieldnames=csv_row.keys())
if not file_exists:
writer.writeheader()
writer.writerow(csv_row)
push_to_github()
time.sleep(1800) # wait for 0.5 hours before next post
except Exception as e:
print(f"Error occurred: {e}")
time.sleep(1800)