-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter_by_camera.py
More file actions
58 lines (53 loc) · 2.19 KB
/
Copy pathFilter_by_camera.py
File metadata and controls
58 lines (53 loc) · 2.19 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 3 16:30:44 2020
@author: Geovani BM
"""
import requests
import csv
import datetime
import pandas as pd
def getURLS(date_format):
#Filtrate by specific camera
req_camera_id = '1701'
global times
image=[]
time =[]
for i in range(len(date_format)):
url_date = date_format[i].replace('/','-') #Adjust timestamps to correct url format
endpoint = "https://api.data.gov.sg/v1/transport/traffic-images?date_time="+url_date
data = requests.get(endpoint)
if (data):
data = requests.get(endpoint).json()
for item in data['items']:
for cam in item['cameras']:
camera_id = cam['camera_id']
if camera_id == req_camera_id:
image = cam['image']
wanted_cam.append(camera_id)
wanted_url.append(image)
time.append(cam['timestamp'])
print(times[i])
with open('/home/sutd/Downloads/cam1701/Wed11-13-19.csv','w',newline='') as f: #Choose your own path
writer = csv.writer(f)
writer.writerows(zip(wanted_cam, wanted_url, times))
df = pd.read_csv('/home/sutd/Downloads/cam1701/Wed11-13-19.csv', header=None)
df.rename(columns={0: 'Cam_id', 1: 'Image', 2: 'Timestamp_sg_time'}, inplace=True)
df.to_csv('/home/sutd/Downloads/cam1701/Wed11-13-19.csv', index=False) # save csv file
if __name__ == "__main__":
string_date_list = []
#Generate timestamps every N minutes. Parameters can be modified
initial_time = datetime.datetime(2019, 11, 13, 0, 0)
final_time = datetime.datetime(2019, 11, 14, 0, 0)
delta = datetime.timedelta(minutes=10)
times = []
wanted_url=[]
wanted_cam=[]
endpoints =[]
data_lists =[]
while initial_time < final_time:
times.append(initial_time)
initial_time += delta
for i in range(len(times)): #Convert datetime objects to string list
string_date_list.append((times[i].strftime('%Y/%m/%dT%H:%M:%S')))
getURLS(string_date_list)