-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_maps.py
57 lines (47 loc) · 1.95 KB
/
get_maps.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
import requests
import os
import json
# Configuration
IMAGE_URLS = [
"https://ims.gov.il/sites/default/files/ims_data/map_images/c3RainForecast/c3RainForecast.png",
"https://ims.gov.il/sites/default/files/ims_data/map_images/ecRainForecast/ecRainForecast.png"
]
LAST_MODIFIED_FILE = "./last_modified_data.json"
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
CHANNEL_ID = os.getenv('CHANNEL_ID')
def download_image(url):
response = requests.get(url)
response.raise_for_status()
return response.content
def send_telegram_photo(bot_token, chat_id, photo_data):
url = f"https://api.telegram.org/bot{bot_token}/sendPhoto"
files = {'photo': ('image.png', photo_data)}
data = {'chat_id': chat_id}
response = requests.post(url, files=files, data=data)
response.raise_for_status()
def load_last_modified_data():
print("Looking for last modified data in:", LAST_MODIFIED_FILE)
if os.path.exists(LAST_MODIFIED_FILE):
with open(LAST_MODIFIED_FILE, 'r') as file:
return json.load(file)
return {}
def save_last_modified_data(data):
print("Saving last modified data to:", LAST_MODIFIED_FILE)
with open(LAST_MODIFIED_FILE, 'w') as file:
json.dump(data, file)
def main():
print("Script started. Checking for new content.")
last_modified_data = load_last_modified_data()
for url in IMAGE_URLS:
response = requests.head(url)
last_modified = response.headers.get('Last-Modified')
previous_last_modified = last_modified_data.get(url)
if not last_modified or last_modified != previous_last_modified:
print(f"New content detected for {url}! Downloading image.")
image_data = download_image(url)
send_telegram_photo(TELEGRAM_TOKEN, CHANNEL_ID, image_data)
print("Image sent successfully.")
last_modified_data[url] = last_modified
save_last_modified_data(last_modified_data)
if __name__ == "__main__":
main()