AppleScript /python help - reading google maps traffic time

Posted on
Tue Jan 29, 2013 5:07 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: AppleScript /python help - reading google maps traffic t

While there are certainly practical reasons for choosing a particular scripting/programming language, many times it's about how comfortable you are with one over the other. To address the practical side:

AppleScript
  • More "English-like" syntax (faster to learn for English speakers)
  • Easier high level inter-application integration on Mac OS X than with other scripting languages.
  • Can be used as an advanced condition for Indigo triggers and schedules (Python cannot).
  • Mac OS X comes with a basic IDE/code editor already (AppleScript Editor).

Python
  • Is (mostly) platform independent and thus usable with more than just Indigo.
  • More flexible (can be used to access lower level hardware components and can be easily extended from libraries found online).
  • Better multi-threading support (especially useful within Indigo plugins). A hung process is much less likely to hang your entire Indigo server or other Indigo plugins.
  • Is the only language supported for creating Indigo plugins (as implied by Jay).

I'm sure there are plenty of things I missed, but I think those are probably the biggest ones for me anyway.

Posted on
Mon May 05, 2014 3:05 pm
TheTechnoPilot offline
Posts: 46
Joined: Mar 31, 2014
Location: Montreal, QC, Canada

Re: AppleScript /python help - reading google maps traffic t

Hi all,

Looking at all the fabulous code that everyone has created for determining the travel time from Google Maps, it inspired me to create my own implementation for my automation, however I wanted to figure out a way to make it not rely on having a predetermined Google Maps direction web address. The idea was to implement it in such a way that it can determine the travel time to any of the appointments in my calender that have location information, which I already fairly religiously include. Sort of like my own implementation of the "when to leave" functionality of Google Now. However, my plan is to make it transportation type aware, which is determined by distance and current/forecast weather conditions. The great news on making this work is that there is actually an open Google API for this functionality called the "Directions API" which has a very reasonable free limit of 2,500 calls per day, click here if you would like to read more on the API.

I just wrote up an AppleScript sub-routine to output the distance and travel time for a given address or lat./long. that utilizes the API with error handling and parsing. I think is an awesome expansion of the capability of the original code.

--EDITED TO INCLUDE THE CODE
Code: Select all
on getDirections(destination, transportationType)
   set googleAPIkey to "<YOUR GOOGLE API KEY GOES HERE>" as string -- Enter your Google Directions API key here
   set origin to "<YOUR HOME LAT/LONG GOES HERE>" as string -- Enter your home's latitude and longitude here in the form of (-)##.######,(-)##.######
   
   try
      -- Format the Address into a form that the Google Directions API can process
      set AppleScript's text item delimiters to the " "
      set destination to every text item of destination
      set AppleScript's text item delimiters to the "+"
      set destination to destination as string
      set AppleScript's text item delimiters to ""
      log destination
      
      -- Query Google Servers for the direction information
      set the_response to do shell script "curl -s -m 10 'https://maps.googleapis.com/maps/api/directions/json?origin=" & origin & "&destination=" & destination & "&mode=" & transportationType & "&key=" & googleAPIkey & "&sensor=false'"
      
      set AppleScript's text item delimiters to "\"status\" : \""
      set directionsStatus to text item 2 of the_response
      set AppleScript's text item delimiters to "\""
      set directionsStatus to text item 1 of directionsStatus
      set AppleScript's text item delimiters to ""
      
      -- Determine if the Google Directions API was successful and if so, parse out the distance and travel time from the origin to the destination
      if directionsStatus is "OK" then
         set AppleScript's text item delimiters to "\"legs\" : ["
         set the_response to text item 2 of the_response
         set AppleScript's text item delimiters to "\"steps\""
         set the_response to text item 1 of the_response
         set AppleScript's text item delimiters to ""
         set distance to paragraph 5 of the_response
         set travelTime to paragraph 9 of the_response
         set distance to ("Distance: " & last word of distance)
         set travelTime to ("Time: " & last word of travelTime)
         set AppleScript's text item delimiters to ""
         
         -- Return the values in multi-line text
         set the_response to (distance & return & travelTime)
      else
         set the_response to ("Error: " & directionsStatus)
      end if
      return the_response
   on error
      return "Error: CONNECTION_ERROR"
   end try
end getDirections

-- Sub-Routine Test
set the_address to "<SOME ADDRESS>"
set the_directions to getDirections(the_address, "walking")
log the_directions


It is important to note that to use the service you will need to click here to access your Google API Console from where you can activate the "Directions API" on your Google account under the "APIs & auth" tab and grab your API key to copy into the AppleScript Sub-Routine code block.

Also be aware that all this does currently is pull the data and parse out the information, it must be integrated into your own code to access any of the addresses in your calendar or do anything within Indigo.

Who is online

Users browsing this forum: No registered users and 15 guests

cron