|
Page 1 of 2
|
[ 16 posts ] |
Go to page: 1, 2 Next |
AppleScript /python help - reading google maps traffic time
| Author |
Message |
|
Randaulf
Joined: Jan 31, 2012 Posts: 10
|
 AppleScript /python help - reading google maps traffic time
Hey all,
I'm just starting to dive into some deeper functionalities of Indigo, and I've run into a little roadblock. I'm trying to write a script that updates me on my travel time to work (estimated time with traffic) -using Google Maps. My overarching goal is to integrate this data into my morning alarm, so if traffic is bad my alarm will go off earlier.
I'm an AppleScript novice (and know nothing about Python), so I started out with a script that opens the correct google maps page, but I'm unable to pull any information off. Does anybody have any thoughts, or script ideas as to how I can possibly do this? Any sort of direction that you could provide would be greatly appreciated! Thanks!
|
| Sat Jul 14, 2012 9:44 am |
|
 |
|
kkachurak
Joined: Aug 12, 2011 Posts: 16
|
 Re: AppleScript /python help - reading google maps traffic t
This is an excellent idea. I'd be interested to see how this works out.
|
| Sun Oct 07, 2012 4:40 pm |
|
 |
|
nsheldon
Joined: Aug 09, 2010 Posts: 763 Location: CA
|
 Re: AppleScript /python help - reading google maps traffic t
Cool idea. It would be possible to use the XML parsing extensions of Python to do this, but it'd probably be a lot more code. I came up with a little AppleScript to pull out the estimated drive time from Google Maps driving directions. It's not enough to completely get you to your end goal, but it could be a good start. You'll need to provide the URL for your specific driving directions. The AppleScript can be embedded in any Indigo action. - Code: Select all
set driveTime to "" set theData to do shell script "curl -s \"<Your Driving Directions URL Here>\""
repeat with thisLine in paragraphs of theData if thisLine contains " about \\x3cb\\x3e" then set AppleScript's text item delimiters to "about \\x3cb\\x3e" set driveTime to text item 2 of theData set AppleScript's text item delimiters to "\\x3c/b\\x3e\\x3c" set driveTime to text item 1 of driveTime set AppleScript's text item delimiters to {""} end if end repeat
log driveTime using type "Drive Time"
To get your driving directions URL, - Open your web browser to Google Maps and generate your driving directions using whatever options you normally select.
- Make sure the Traffic layer is visible by clicking on the Traffic menu under "Satellite" at the top right of the map.
- At the top right of the side-bar on the left (with all the directions listed), click the printer icon to bring up the printer-frienly version of the directions.
- Copy the URL in your browser's address bar of the printer-friendly directions and paste it into the above AppleScript where is says <Your Driving Directions URL Here>. Be careful to not erase the quotation marks in the above AppleScript.
Whenever that action is executed, the AppleScript will use the command-line "curl" tool to get the driving directions from Google then look for the drive time estimate. Note that, while using the above AppleScript as an embedded script is convenient, if it takes very long for Google to return data to the script, your entire Indigo server will hang until the embedded AppleScript completes. If you notice this happening, you'll probably want to put the above AppleScript into an externally executed script file that's called from the action instead of embedding the script. Also, all the above script does is show the drive time in the Indigo log. You can remove the line that starts with "log" and put something else like set value of variable "DriveTime" to driveTime which will insert the drive time data into an Indigo variable named "DriveTime" (if it exists) rather than just make a note in the log.
|
| Mon Oct 08, 2012 2:43 pm |
|
 |
|
Randaulf
Joined: Jan 31, 2012 Posts: 10
|
 Re: AppleScript /python help - reading google maps traffic t
Awesome, thanks so much for your insights nsheldon, it is greatly appreciated! I am running into a few issues, most likely caused by user error. I've tried to execute the following script based on your instructions (with my own Google Maps link inserted): - Code: Select all
set driveTime to "" set theData to do shell script "curl -s \"<my driving directions>\""
repeat with thisLine in paragraphs of theData if thisLine contains " about \\x3cb\\x3e" then set AppleScript's text item delimiters to "about \\x3cb\\x3e" set driveTime to text item 2 of theData set AppleScript's text item delimiters to "\\x3c/b\\x3e\\x3c" set driveTime to text item 1 of driveTime set AppleScript's text item delimiters to {""} end if end repeat
set value of variable "DriveTime" to driveTime
I get the following error message, Can't set <<class Valu>> "DriveTime" to "".
I am not sure exactly where I went wrong, but I would like to use the Drive Time as a variable in Indigo so I can base future actions if the drive time goes above a certain number. Any further insights would be greatly appreciated! Thanks again for all of your help thus far.
|
| Sun Oct 28, 2012 6:09 pm |
|
 |
|
ckeyes888
Joined: Nov 26, 2009 Posts: 923 Location: Kalispell, MT
|
 Re: AppleScript /python help - reading google maps traffic t
If the script is external I think you may need to add a tell block.
tell application "Indigoserver" set the value...etc. end tell
Carl
|
| Sun Oct 28, 2012 8:21 pm |
|
 |
|
nsheldon
Joined: Aug 09, 2010 Posts: 763 Location: CA
|
 Re: AppleScript /python help - reading google maps traffic t
Carl's correct. If you're using an external AppleScript as opposed to an embedded one, you must put the "tell application..." portion in. Also, be sure the "DriveTime" variable exists in Indigo, otherwise that code won't work and you'd get an error like the one you're seeing. Third, the script will insert the text of what Google believes to be your drive time (e.g. "45 minutes" or "1 hour 10 minutes"). The script does not extract the "hours" or "minutes" text or do any kind of time calculation. It just tells you what Google is saying for the drive time. Getting usable numbers out of that would require quite a bit more code (though it is possible to do).
|
| Sun Oct 28, 2012 8:28 pm |
|
 |
|
Randaulf
Joined: Jan 31, 2012 Posts: 10
|
 Re: AppleScript /python help - reading google maps traffic t
Thanks Carl and nsheldon! nsheldon, you hit it right on the nose, I hadn't created a variable called "Drive Time" yet. Now that I have, I think you're correct that I need to find a way to make those numbers useable. When you say Getting usable numbers out of that would require quite a bit more code (though it is possible to do).
do you have any ideas as to how I could move forward with this code? I'm not sure what I need to do to make this workable (I think it would involve translating from hours and minutes to one concrete variable -- yes, I know, oxymoron) I think it'd be great to setup this script to run in the early morning and possibly setup a trigger to update my alarm to an earlier time (especially if there is snow). Thanks again for all of your help with this. Any further direction you could take me would be great!
|
| Tue Oct 30, 2012 4:41 pm |
|
 |
|
ckeyes888
Joined: Nov 26, 2009 Posts: 923 Location: Kalispell, MT
|
 Re: AppleScript /python help - reading google maps traffic t
Not much of an Applescripter myself but a great resource for code and help with it, besides here, I've found is a site called MacScripter.
Carl
|
| Tue Oct 30, 2012 8:40 pm |
|
 |
|
nsheldon
Joined: Aug 09, 2010 Posts: 763 Location: CA
|
 Re: AppleScript /python help - reading google maps traffic t
Randaulf wrote:do you have any ideas as to how I could move forward with this code? I'm not sure what I need to do to make this workable (I think it would involve translating from hours and minutes to one concrete variable -- yes, I know, oxymoron)
Yes. The translation from worded time to numerical time would involve identifying the hours and minutes in the output, stripping off the words, and converting the hours portion (if it exists) to minutes so it can be added to the minutes portion (if it exists). So, to put those steps into code (AppleScript)... - Code: Select all
-- Define the hour and minute AppleScript variables. set driveHours to 0 set driveMinutes to 0 -- If the drive time includes hours... if driveTime contains "hour" then -- Redefine AppleScript's method of separating text. set AppleScript's text item delimiters to " hour" -- Extract everything before "hour" to use as the numerical hour. set driveHours to text item 1 of driveTime -- Take everything after "hour" and use that later to get the numerical minutes. set driveMinutes to text item 2 of driveTime -- Return AppleScript's text separation method back to what it was. set AppleScript's text item delimiters to {""} -- Convert the hours we just extracted from text to a number. set driveHours to driveHours as number -- If the drive time includes minutes... if driveTime contains "minute" then -- If there were multiple hours in the extracted hours above, the driveMinutes will start with the remaining "s" from "hours". If that's the case... if driveMinutes starts with "s" then -- Start splitting text at the "s". set AppleScript's text item delimiters to "s" -- And take everything after the "s". set driveMinutes to text item 2 of driveMinutes end if -- Now start splitting text at " minute". set AppleScript's text item delimiters to " minute" -- And take everything that comes before " minute". set driveMinutes to text item 1 of driveMinutes -- Now return back to normal text splitting behavior. set AppleScript's text item delimiters to {""} -- Convert the minutes we just extracted into a number. set driveMinutes to driveMinutes as number else -- If the drive time didn't contain any minutes, just set the minutes to zero. set driveMinutes to 0 end if -- Convert the hours into minutes and add the drive minutes for the total drive time. set driveTime to driveHours * 60 + driveMinutes else -- If the drive time didn't contain any hours, but only contained minutes... if driveTime contains "minute" then -- Begin splitting text at " minute". set AppleScript's text item delimiters to " minute" -- We want everything before " minute" so we can use it as a number. set driveMinutes to text item 1 of driveTime -- Return text splitting to normal. set AppleScript's text item delimiters to {""} -- Convert the minutes we just extracted to a number. set driveMinutes to driveMinutes as number end if -- The total drive time is equal to the number of minutes. set driveTime to driveMinutes end if
This will extract the numbers (hours and/or minutes), convert hours into minutes, add the hours and minutes together and assign the total to the "driveTime" AppleScript variable (which you can then assign to an Indigo variable if you like). Hopefully the commenting will help explain how the code does that.
|
| Wed Oct 31, 2012 12:41 am |
|
 |
|
JS9
Joined: Mar 06, 2012 Posts: 9
|
 Re: AppleScript /python help - reading google maps traffic t
Just coming across this thread from a few months back - great idea!
Has anyone figured out how to pull the estimated driving time based on traffic conditions? Seems using the "print icon" to grab the URL doesn't take into consideration any delays caused by traffic.. Been searching for a way to get it to appear on that page but haven't been able to thus far and figured I'd check back in here to see if anyone had found a workaround?
|
| Wed Jan 02, 2013 6:14 pm |
|
 |
|
nsheldon
Joined: Aug 09, 2010 Posts: 763 Location: CA
|
 Re: AppleScript /python help - reading google maps traffic t
JS9 wrote:Just coming across this thread from a few months back - great idea!
Has anyone figured out how to pull the estimated driving time based on traffic conditions? Seems using the "print icon" to grab the URL doesn't take into consideration any delays caused by traffic.. Been searching for a way to get it to appear on that page but haven't been able to thus far and figured I'd check back in here to see if anyone had found a workaround?
Yes. Since my original post above, I've modified the procedure I use in my own setup to correctly obtain the adjusted drive time taking traffic into account. To do that, skip step 3 in my original post (don't click the printer icon) then use the following AppleScript code instead. - Code: Select all
-- Character replacement method. on replaceText(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replaceText
set driveTime to "" set theData to do shell script "curl -s \"<Your Driving Directions URL Here>\""
repeat with thisLine in paragraphs of theData if thisLine contains "In current traffic: " then set AppleScript's text item delimiters to "In current traffic: " set driveTime to text item 2 of theData set AppleScript's text item delimiters to " </span>" set driveTime to text item 1 of driveTime set AppleScript's text item delimiters to {""} end if end repeat
-- Replace abbreviated times with full time descriptions. set driveTime to replaceText(driveTime, "min", "minute") set value of variable "DriveTime" to driveTime
|
| Thu Jan 03, 2013 12:06 am |
|
 |
|
JS9
Joined: Mar 06, 2012 Posts: 9
|
 Re: AppleScript /python help - reading google maps traffic t
Works great - thanks for passing this along!
|
| Thu Jan 03, 2013 8:19 am |
|
 |
|
roussell
Joined: Aug 18, 2008 Posts: 104 Location: Alabama
|
 Re: AppleScript /python help - reading google maps traffic t
As an alternative, I used this on a Linux box for a while and then transferred it over to the Mac when I started running Indigo. Same concept, in gMaps/directions I use the "Link" icon next to the printer to grab the URL, and then pass it through cURL, grep and sed (psed on Mac) to strip out the current drive time. - Code: Select all
curl --silent -A "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" "<Your Driving Directions URL Here>" |grep traffic |psed -e 's,.*traffic:\ \([^<]*\)</span>.*,\1,g'
I fake an iPhone4 in the agent string to get back a shorter page but the end result is something like: - Code: Select all
17 mins
that I can then pipe to an Indigo variable. Terry
|
| Mon Jan 07, 2013 12:29 pm |
|
 |
|
bmcgowan13
Joined: Jan 13, 2013 Posts: 42
|
 Re: AppleScript /python help - reading google maps traffic t
This is unrelated to the original question but this is the closest topic I've seen since this one deals with both Applescript and Python.
I'm picking stuff up by reading the scripts posted in this topic but it's time to dig in and get some books and into a class somewhere but just wondering which way to go.
I've done some (very basic) programming years ago. Would you recommend starting with Applescript or Python? For instance, in this scenario which language is a better fit for the problem? Applescript or Python? Or is this totally user preference?
I love the idea of rolling back the alarm clock based on projected traffic--what a great idea. I'd love to be able to play along but some of the commodes are throwing me...
|
| Tue Jan 29, 2013 3:58 pm |
|
 |
|
jay (support)
Site Admin
Joined: Mar 19, 2008 Posts: 6651 Location: Austin, Texas
|
 Re: AppleScript /python help - reading google maps traffic t
We're not going to be updating AppleScript with new features so from that perspective it's better to learn Python.
AppleScript, however, is how you integrate to other Mac applications - so if you think that's more of what you'll be doing then go with AS.
_________________ Jay (Indigo Support)
|
| Tue Jan 29, 2013 4:06 pm |
|
|
|
Page 1 of 2
|
[ 16 posts ] |
Go to page: 1, 2 Next |
Who is online |
Users browsing this forum: No registered users and 0 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|