-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_fetcher.py
73 lines (58 loc) · 2.84 KB
/
image_fetcher.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
import requests
import datetime
from utils import cam_fetcher, convert_to_datetime, request_to_json
class ImageFetcher:
def __init__(self, cam: str):
self.cam = cam_fetcher(cam)
if self.cam:
self.cam_found = True
self.cam_name = self.cam['title']
self.cam_id = self.cam['_id']
# checking timezone difference
self.tzdiff = self.cam['location']['locale']
print(f"Timezone: {self.tzdiff}.")
else:
self.cam_found = False
def fetch_latest(self):
cam_imgs = request_to_json(f'http://api.deckchair.com/v1/camera/{self.cam_id}/images')
img_id = cam_imgs[0]['_id']
img_date = cam_imgs[0]['taken']
print(datetime.datetime.now(), convert_to_datetime(img_date))
url = f'http://api.deckchair.com/v1/viewer/image/{img_id}'
return requests.get(url).content, convert_to_datetime(img_date)
def fetch_todays(self):
today = datetime.date.today()
print(today)
url = f'http://api.deckchair.com/v1/camera/{self.cam_id}/images?from={today.strftime("%s")}'
cam_imgs = request_to_json(url)
for img in cam_imgs:
# print(img['taken'])
url = f'http://api.deckchair.com/v1/viewer/image/{img["_id"]}'
yield convert_to_datetime(img['taken']), requests.get(url).content
def fetch_monthly(self):
month_ago = (datetime.datetime.now() - datetime.timedelta(days=30))
unixtime_month_ago = month_ago.strftime('%s')
start = datetime.datetime.now()
print(f"Last searched date: {month_ago}")
running = True
# Since this API responds with jsons of a maximum size of 1000 images, we need to
# make several requests to gather images from entire month; with every request
# I check the last image in it and start from that date on the next request, until
# I have all images necessary.
while running:
# range as in FROM later TO earlier, dates are in UnixTime
url = (f'http://api.deckchair.com/v1/camera/{self.cam_id}'
f'/images?from={unixtime_month_ago}&to={start.strftime("%s")}')
cam_imgs = request_to_json(url)
start = convert_to_datetime(cam_imgs[0]['taken'])
end = convert_to_datetime(cam_imgs[-1]['taken'])
print(f"{start} - {end}", url, f"({len(cam_imgs)} images found)")
for img in cam_imgs:
img_date = convert_to_datetime(img['taken'])
if img_date <= month_ago:
running = False
break
else:
img_url = f'http://api.deckchair.com/v1/viewer/image/{img["_id"]}'
yield img_date, requests.get(img_url).content
start = end