-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps_traffic.py
executable file
·121 lines (89 loc) · 3.76 KB
/
maps_traffic.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
119
120
#!/usr/bin/env python3
import datetime
import pathlib
import sys
from pathlib import Path
from typing import Dict, List
import yaml
from mapsbrowser import FirefoxBrowser, ChromeBrowser
from screenshotscheduler import ScreenshotScheduler
def gen_url(latitude, longitude, zoom, data='!5m1!1e1'):
url = f'https://www.google.be/maps/@{latitude},{longitude},{zoom}z/data={data}'
return url
def gen_image_path(name, screenshot_folder):
now = datetime.datetime.now()
image_name = f'{now:%Y-%m-%d_%H%M}-{name}.png'
base_path = Path(screenshot_folder, name)
base_path.mkdir(parents=True, exist_ok=True)
return base_path.joinpath(image_name)
def get_screenshots_now(locations: Dict, screenshot_folder: pathlib.Path, window_x: int = 1024, window_y: int = 768) -> None:
if sys.platform.startswith('linux'):
Browser = ChromeBrowser
elif sys.platform.startswith('win32'):
Browser = FirefoxBrowser
else:
print('[ERROR]: OS is not linux or windows. Other operating systems are not supported.')
return
with Browser(visual=False) as browser:
browser.setup(window_x, window_y)
print(f'{locations=}')
for name, url in locations.items():
image_path = gen_image_path(name, screenshot_folder)
browser.get_maps_page(url, image_path)
def generate_executetimes(start_time: datetime.datetime, end_time: datetime.datetime, interval: int) -> List[datetime.datetime]:
execute_times = []
pointer = start_time
delta = datetime.timedelta(minutes=interval)
while pointer <= end_time:
execute_times.append(pointer)
pointer += delta
return execute_times
def import_from_flask(name: str, latitude: float, longitude: float, zoom: float,
start_datetime: List[datetime.datetime], end_datetime: List[datetime.datetime],
interval: int) -> [str, str, List[datetime.datetime]]:
url = gen_url(latitude, longitude, zoom)
excute_times = set()
for start, end in zip(start_datetime, end_datetime):
excute_times.update(generate_executetimes(start, end, interval))
print(excute_times)
return name, url, list(excute_times)
def execute_from_config():
print('Started maps traffic screenshot tool')
with open('config.yaml') as configuration_file:
config = yaml.full_load(configuration_file)
print('[INFO]: Beginnen met alle taken te plannen.')
planner = ScreenshotScheduler(get_screenshots_now)
for name in config['locations']:
data = config['locations'][name]
url = gen_url(
latitude=config['locations'][name]['latitude'],
longitude=config['locations'][name]['longitude'],
zoom=config['locations'][name]['zoom'])
base_path = Path('screenshots')
execute_times = []
for time_string in data['time']:
if time_string == 'now':
get_screenshots_now({name: url}, base_path)
else:
try:
time_object = datetime.datetime.strptime(time_string, '%Y-%m-%d %H:%M')
except ValueError:
print(f'[ERROR] Geen geldig moment ingegeven! {time_string = }')
continue
execute_times.append(time_object)
planner.add_tasks(name, url, execute_times, base_path)
print('[INFO]: Geen resterende taken meer, afsluiten')
input('Druk op enter om dit venster te sluiten...')
if __name__ == '__main__':
execute_from_config()
"""
import_from_flask(
name="Test",
latitude=50.357,
longitude=4.257,
zoom=13.5,
start_datetime=datetime.datetime.now(),
end_datetime=datetime.datetime.now() + datetime.timedelta(days=5),
interval=15
)
"""