-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (100 loc) · 4.39 KB
/
main.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 requests
from requests.exceptions import RequestException
import shutil
import filecmp
import os
import tweepy
import time
import datetime
import random
from pdf2image import convert_from_path
from pdf2image.exceptions import PDFPageCountError
from dotenv import load_dotenv
tweet_url = 'https://saskatchewan.ca/fire'
fire_data = [
{
'pdf': 'activefires.pdf',
'path': 'http://environment.gov.sk.ca/firefiles/',
'title': 'Saskatchewan Daily Wildfire Situation Map',
},
{
'pdf': 'firestodate.pdf',
'path': 'http://environment.gov.sk.ca/firefiles/',
'title': 'Fires to Date',
},
{
'pdf': 'MunicipalFireBans.pdf',
'path': 'http://environment.gov.sk.ca/firefiles/',
'title': 'Saskatchewan Fire Ban Map',
},
{
'pdf': 'today_fwi.pdf',
'path': 'http://environment.gov.sk.ca/firefiles/DailyFireDangerMaps/',
'title': "Saskatchewan Spatial Fire Management System: Today's Forecast",
},
{
'pdf': 'tomorrow_fwi.pdf',
'path': 'http://environment.gov.sk.ca/firefiles/DailyFireDangerMaps/',
'title': "Saskatchewan Spatial Fire Management System: Tomorrow Forecast",
},
]
def download_file(url, path, file):
try:
response = requests.get(url+file, stream=True, timeout=30)
write_file = open(path+file, 'wb')
write_file.write(response.content)
write_file.close()
return True
# All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.
except RequestException as error:
log('RequestException: download_file: ' + url+file, error)
return False
def move_file(path, file, new_path):
if os.path.isfile(path+file):
shutil.move(path+file, new_path+file)
def generate_images_from_pdf(in_path, pdf, out_path):
try:
images = convert_from_path(in_path+pdf)
for i, image in enumerate(images):
filename = pdf+str(i)+'.png'
image.save(out_path+filename, "PNG")
except PDFPageCountError as error:
log("Error: generate_images_from_pdf!", error)
def image_history(path, image, new_path):
if os.path.isfile(path+image):
shutil.copy(path+image, new_path + datetime.datetime.now().strftime('%y%m%d-%H%M-') + image)
def tweet(title, image):
auth = tweepy.OAuthHandler(os.getenv('CONSUMER_KEY'), os.getenv('CONSUMER_SECRET'))
auth.set_access_token(os.getenv('ACCESS_TOKEN'), os.getenv('ACCESS_TOKEN_SECRET'))
hashtags = '#sk #wildfire #skwildfire #skfires'
status = title + '\n\nUpdate Detected: ' + datetime.datetime.now().strftime('%d %b %Y %I:%M %p CST') + '\n\nMore info at: ' + tweet_url + '\n\n' + hashtags
try:
api = tweepy.API(auth)
api.update_with_media(image, status)
log('Tweet:', status.replace('\n', ''))
except tweepy.TweepError as error:
log('Error: update_status: Something went wrong!', error)
def log(text1, text2='', text3=''):
print(f"{datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')}: {text1} {text2} {text3}")
if __name__ == '__main__':
log('Sask Wildfire Bot is now running...')
load_dotenv()
while True:
for item in fire_data:
time.sleep(random.randint(60, 120)) # wait 1 or 2 minutes
move_file('./pdf/', item['pdf'], './pdf_old/')
move_file('./image/', item['pdf']+'0.png', './image_old/')
if download_file(item['path'], './pdf/', item['pdf']):
if not os.path.isfile('./pdf_old/'+item['pdf']) or not filecmp.cmp('./pdf/' + item['pdf'], './pdf_old/' + item['pdf'], shallow=True):
log('PDF is different: ', item['pdf'])
generate_images_from_pdf('./pdf/', item['pdf'], './image/')
image_history('./image/', item['pdf'] + '0.png', './history/')
if not os.path.isfile('./image_old/'+item['pdf']+'0.png') or not filecmp.cmp('./image/' + item['pdf'] + '0.png', './image_old/' + item['pdf'] + '0.png', shallow=False):
tweet(item['title'], './image/'+item['pdf']+'0.png')
else:
log('No Image Changes: ', item['pdf'])
else:
log('No PDF Changes: ', item['pdf'])
else:
log('File Download Issue: ', item['pdf'])
time.sleep(random.randint(1800, 3600)) # wait 30 to 60 minutes