-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_alerts.py
75 lines (62 loc) · 2.5 KB
/
get_alerts.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
import json
import requests
import subprocess
from playwright.sync_api import sync_playwright
url = "https://www.washingtonpost.com/prism/api/alerts"
local_file_path = 'alerts.json'
def fetch_new_alerts_with_playwright(url):
with sync_playwright() as p:
browser = p.firefox.launch(headless=True) # Use headless=False for debugging
page = browser.new_page()
page.goto(url)
# Wait for a specific element if necessary, or delay for content loading
# page.wait_for_selector("selector-to-wait-for")
content = page.content() # Get the full page content
json_data = page.evaluate("() => JSON.parse(document.body.innerText)") # Adjust as needed
browser.close()
return json_data
def fetch_new_alerts_with_requests(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:118.0) Gecko/20100101 Firefox/118.0'
}
try:
response = requests.get(url, headers=headers, verify=False)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching alerts: {e}")
return []
def fetch_new_alerts_with_curl(url):
# Use curl to download the JSON data with SSL verification disabled
try:
result = subprocess.run(
['curl', '-k', url],
check=True,
capture_output=True,
text=True
)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error fetching alerts: {e}")
return []
def load_existing_alerts(file_path):
try:
with open(file_path, 'r') as file:
return json.load(file)
except FileNotFoundError:
return []
def add_new_alerts(existing_alerts, new_alerts):
existing_ids = {alert['airshipId'] for alert in existing_alerts}
new_alerts_to_add = [alert for alert in new_alerts if alert['airshipId'] not in existing_ids]
return existing_alerts + new_alerts_to_add
def save_alerts_to_file(alerts, file_path):
with open(file_path, 'w') as file:
json.dump(alerts, file, indent=4)
def update_alerts(url, local_file_path):
new_alerts = fetch_new_alerts_with_playwright(url)
# new_alerts = fetch_new_alerts_with_curl(url)
existing_alerts = load_existing_alerts(local_file_path)
updated_alerts = add_new_alerts(existing_alerts, new_alerts)
save_alerts_to_file(updated_alerts, local_file_path)
# Run the update function
update_alerts(url, local_file_path)