@@ -0,0 +1,2776 @@
+#####################################################################%0A#googleMaps API tool for pulling time duration from 'home' to 'work'#%0A#Author: Michael Smith%09%09%09%09%09%09                                  #%0A#####################################################################%0A%0A%0Aimport urllib,time,datetime,sys%0Aimport json,pprint%0A%0A# info about locations%0Ahome = '123+Park+Place+Chicago+IL'%0Awork = '456+Other+Place+Chicago+IL'%0A%0Aclass googleMaps:%0A    def __init__(self):%0A%09self.url = 'https://maps.googleapis.com/maps/api/directions/json?'%0A%09self.key = 'API KEY GOES HERE'%0A%09%0A%09  #function for creating new observation given a origin, destination, and model of traffic%0A%09  #options for traffic model are ('best_guess','pessimistic','optimistic')%0A%09  %0A    def get_time(self, origin, destination, traffic_model):%0A      url = self.url+'origin='+origin+'&destination='+destination+'&key='+self.key+'&departure_time=now&traffic_model='+traffic_model%0A%09    json_url = urllib.urlopen(url).read()%0A%09    json_dict = json.loads(json_url)%0A%09    time_estimate = json_dict%5B'routes'%5D%5B0%5D%5B'legs'%5D%5B0%5D%5B'duration_in_traffic'%5D%5B'value'%5D /60%0A%09    current_time = time.localtime(time.time())%0A%09    if origin == home:%0A%09      return %5Btime_estimate,traffic_model,current_time%5B6%5D,current_time%5B3%5D-6,current_time%5B4%5D,'to_work'%5D%0A      else:%0A        return %5Btime_estimate,traffic_model,current_time%5B6%5D,current_time%5B3%5D-6,current_time%5B4%5D,'from_work'%5D%0A    %0A    #function to write the new observation created in get_time() in a csv string%0A    %0A    def to_csv(self,list):%0A        return ','.join(%5Bstr(list%5B0%5D),str(list%5B1%5D),str(list%5B2%5D),str(list%5B3%5D),str(list%5B4%5D),str(list%5B5%5D)+'%5Cn'%5D) %0A        %0A    #function to write take existing csv file (could be nonexistent) and append the new line from get_time()%0A    %0A    def write_csv(self,list,filename):%0A        with open(filename,'a') as f:%0A            f.write(self.to_csv(list))%0A%09    f.close()%0A%0A# put it all together!%0A# call the script with %22python travel_times.py to_work%22 to get time from 'home' to 'work' %0A# or %22python travel_times.py from_work%22 to get time from 'work' to 'home' as defined at the top.%0A# script returns time estimation for all 3 types of traffic models%0A%0Aif __name__ == %22__main__%22:%0A    try:%0A%09arg = sys.argv%5B1%5D%0A    %09if arg == 'to_work':%0A%09    for model in %5B'best_guess','pessimistic','optimistic'%5D:%0A%09        time_list = googleMaps().get_time(home,work,model)%0A%09        googleMaps().write_csv(time_list,'/destination/path/to/csv/file.csv')%0A%0A    %09elif sys.argv%5B1%5D == 'from_work':%0A%09    for model in %5B'best_guess','pessimistic','optimistic'%5D:%0A                time_list = googleMaps().get_time(work,home,model)%0A                googleMaps().write_csv(time_list,'/destination/path/to/csv/file.csv')%0A%0A    except:%0A%09print('Missing argument: needs either %22to_work%22 or %22from_work%22')%0A

Create traffic_times.py