Nat Friedman

playnice: make your iPhone autoupdate Google Latitude

Image by dunechaser on flickr

Last week Google released a version of Latitude for the iPhone. Unfortunately it’s a web page that you have to visit every time you want to update your location.

In my opinion, it would be nicer to have my iPhone automatically update my location in the background without having to visit google.com/latitude every time I want to let my friends know that I’ve moved.

So this weekend I wrote a script that does just that.

I call it playnice. Run it in cron to keep Latitude apprised of your iPhone’s location.

Update: Andy Blyler has taken over maintenance of playnice, so the link above now points to his playnice github project.

It works by scraping the iPhone location from MobileMe‘s Find My iPhone feature, using the code from Tyler Hall’s sosumi project. When you enable Find My iPhone, your iPhone will regularly send its location to MobileMe.

Google doesn’t provide an API to update Latitude yet, so there is some scraping involved on that end too.  Thanks to Jack Catchpoole for his help with the Google scraping.

Of course, you need a MobileMe subscription for this to work.

With all the scraping, playnice could be fragile and subject to any minor changes Google and Apple might make.

It would be nice if Google and Apple would offer APIs for these services in the future.

But at least for now, it works.

2 August 2009
Show comments
  1. Thanks, nice to know. I have a couple friends with iPhones that may be interested. On the other side, it’s all automatic and well integrated in android phones :)

    Reply

    1. I hope it helps your friends!

      There’s also a nice Latitude app for the Blackberry.

      Reply

  2. Awesome. Shame a MobileMe subscription is needed as I have never seen the value (for my own personal needs) to subscribe to it.

    Bravo for the workaround though!

    Reply

  3. Clever. I may have to push this on my iPhone-using friends as well. My poor G1 has a fresh list of other Android and Blackberry users, and then a trailing list of several-day-old locations of iPhones. Thanks!

    Reply

  4. Nat, I can see from the reflection of the Lego mini-figure that you took this picture naked :-)

    Reply

    1. you made me look to the original size.

      Reply

  5. This is awesome, but…

    How exactly do I get it working/install it? I know, that might be a stupid question, but I’m not (yet) a code guy.

    Reply

  6. I second Alex’s request for some basic info on how to use this.

    Please & Thank You!

    Reply

  7. Super easy to use,

    Just run from command line:

    php5 /path/to/playnice.php

    it will ask you some questions the first time and should work fine.. make sure you have php5 and php5-curl installed.

    the part that perplexes me is that when i run it in cron the google cookie file never updates!!!?? only when i run manually.. any ideas??

    Reply

    1. It writes the google cookie file into the present working directory, so when you run it from Cron, make sure that you run it in a shell script which changes directory first:

      cd /home/bikr/playnice
      php ./playnice.php

      Enjoy!

      Reply

  8. Ok. little closer. when i run it in cron it’s reasking for the login info.. no way to specify it though, trying to find the path it’s looking for now to drop the pass file in there.

    Reply

  9. Got it..

    here’s how to do it:

    make a bash script:

    #!/bin/bash
    cd /home/user/www/Latitude
    /usr/bin/php5 ./playnice.php

    then your cron should look like this:

    * * * * * /home/user/www/Latitude/run > /home/user/www/Latitude/error.log

    The above pipes the output into an error file , i used this to debug once you have it working you can remove it.. be sure to have already ran it once manually , and change the paths above to your dirs.

    Reply

  10. Awesome script – got it working first time.

    Thanks!

    Reply

  11. Can someone please give a guide as to what to download, how to install

    I really have no idea what any of this code stuff means cron etc

    Thank you

    Reply

    1. Adam.. You can goto

      http://github.com/natfriedman/playnice/tree/master

      And click the download button…

      Choose zip and extract it to your server…

      Make sure you have PHP5 installed if you’re using ubuntu you can run

      sudo apt-get install php5 php5-curl

      Then you can run the script by going into the directory and typing:

      php5 playnice.php

      It will ask you some questions like your username and passwords for mobile me and google, then it will write 2 files..

      The first file is the cookie used to update google with your locaiton , the other is your username and passwords stored in it (encrypted i’m sure, but haven’t checked).

      To make this run automatically, you can make a file in the directory for example called “run” in that file put the following:

      #!/bin/bash
      cd /home/user/www/Latitude
      /usr/bin/php5 ./playnice.php

      Save it and chmod it to 755 and +x

      run the script to make sure it works..

      ./run

      You should get some feedback on the screen just like when you ran php playnice.php..

      now you type

      crontab -e

      to edit your crontab (your auto runs)

      Put this in the file on its own line:
      * * * * * /home/user/www/Latitude/run > /dev/null

      and save.. ( :wq! if using vi as your editor)

      It will now update your location every minute..

      Reply

      1. Nice step-by-step instructions, thanks for posting these for everyone.

        One note. The MobileMe username/password are not encrypted when they are stored on disk, they are stored in plaintext (and playnice warns you about this when you run it).

        Reply

  12. One thing I’ve noticed with this script.. Mobile Me doesn’t just check every so often. It appears that every time you ask mobile me for the location, it polls the device.. I had mine updating every minute over night, and my charger couldn’t keep up with the battery usage..

    Just a warning.. You may want to have your script check every 15 minutes etc.

    Reply

    1. hi Bikr, i am looking in the files for that, but have you found the place or right file to adjust the polling time?

      kind regards,

      Ivan

      Reply

      1. found it, it’s the cronjob.

        Reply

  13. I just tried loading this up on one of my servers.

    I enter my MobileMe username (tried with @me.com and without) and Google password. It fetches the Google Session cookie find, and attempts to get the iPhone location. It seems to get it fairly quickly, but then in iPhone Location it says ” , ” (excluding quotes)

    It when proceeds to update Latitude with nothing

    Cheers
    Dave

    Reply

    1. Just to add:
      I’ve done some tests and by the look of it the Sosumi class isn’t retrieving the devices properly. On my account it gets 0. I do have a MobileMe family pack so maybe that has something to do with it. The Sosumi call on line 149 (curlPost) seems to return nothing.

      Anyone got any ideas?

      Reply

      1. Make sure you have the ca-certificates package installed if you are on Ubuntu (this is documented in the INSTALL file).

        Hopefully that will fix your issue.

        Reply

      2. I have the same problem and I also have the family pack.

        I have apt-get install’d ca-certificates – do I need to configure it or something?

        Reply

        1. No, that should just work. I wonder if there’s an issue with the mobileme scraping and the family pack.

          Reply

          1. This is something I’ve been looking for for quite a while! Thanks for creating it. Not so much interested in Latitude, but I have been looking for an automated way to retrieve my location. But getting the same problem as others have reported. Ubuntu/php5/ca-certificates/php5-curl . Just getting location ” , ”. So I tried accessing the class directly and using sendMessage and message isn’t being received by the phone either.

            $ssm = new Sosumi(‘Ben174′, ‘*****’);
            $ssm->sendMessage(‘Daisy, daisy…’);

            Phone receives nothing. Any idea what I might be doing wrong?

            Reply

            1. Update: Just tried it on my co-located server and it works great! Must be something with my work firewall on my local box. Thanks again for the awesome tool!

              Reply

          2. I think that the issue is related to the family packs.

            I have got ca-certificates installed (had it installed the whole time) and still the same result. By the look of it, nothing in the get_devices() call within the Sosumi class gets anything back from curl.

            My email address is included in this post so just shoot me an email if there are any diagnostics you would like run (I can use both the family master and family child accounts).

            Reply

            1. I’ve had a few reports of it working for some people on some hosts and not on others (like Ben174 up there). So I’m not convinced it’s family-pack related.

              Reply

              1. Using current code on family pack – no issues here either.

      3. trying to run it using php 5.3 on my winxp machine, i get…

        ‘stty’ is not recognized as an internal or external command,
        operable program or batch file.
        [ Lots of other errors removed by editor ... ]

        Reply

        1. The script doesn’t work on Windows. If you run it on Linux or a mac it will work. I don’t have a Windows system but if someone submits a patch to get it working on Windows I’ll gladly merge it.

          Reply

          1. Noted. I will run it on my mac. Thanks.

            Reply

          2. In that case then perhaps it is an issue with my setup. I just deployed a brand new server under Ubuntu Jaunty using Rackspace. If it is a clean installation, what are the steps I should follow to get this working?

            My current installation simply has php5 curl and php-cli (with ca-certs already installed). Is this enough or should there be additional things. (I also modified the php.ini to support curl obviously)

            I can deploy brand new servers at will, so happy to do any testing needed.

            Cheers
            Dave

            Reply

  14. Is there any way to set this up without SSH/telnet access to the server? My host gives me access to cron through cpanel, but no SSH access. I tried manually creating the mobileme password text file, but then it was asking about the cookie file or whatever. is there anyway to manually get those values and plug them into the setup files, or do i have to run the PHP file from the server to complete setup?

    Reply

    1. You could run it on your computer at home and upload those files to your server.

      Reply

      1. why didn’t i think of that? :)

        thanks

        Reply

  15. if you’re in a country where the igoogle latitude widget isn’t available, append “?gl=us” to the end of the url on line 22 of class.google.php

    Reply

    1. Great tip. It didn’t seem to do any harm here so I added this parameter to the playnice code upstream. If it causes a problem for anyone please let me know and I’ll remove it.

      Reply

  16. Hi Nat, thanks for the script. I also have to do it local before loading it up to my part on the webserver of my host.
    But do i have to do something extra locally. because running the: sudo apt-get install php5 php5-curl in the directory where youre files are, it says: apt-get – command not found, do i have to install php5 and ubuntu on the mac?

    Kind regards and many thanks!
    Ivan

    Reply

    1. I think macs have php installed already.

      Reply

  17. Any one out htere ho managed to get it installed (the first part before uploading the script to a webserver) on a Mac?
    Could someone please help me out with it, downloaded Mamp, Fink but can not get past that apt-get command.

    Kind regards,

    Ivan

    Reply

  18. a bit further, installed ubuntu under vmware and got the cookies, busy on the second part, also not so easy for not insiders :-)

    ivan

    Reply

    1. Ivan, any luck getting it to run in Ubuntu?

      Reply

      1. hi nathan yes i did, ubuntu was the easiest way for me.

        ivan

        Reply

        1. but something else is happening. in the root it makes a file called playnice.php.1 and so on till what i saw today playnice.php.485, is there a way i can put this out or is it the way i made the cronjob that makes the file??
          cronjob command is:
          /usr/bin/wget /dev/null http://www.doman.com/directory/playnice.php

          Reply

          1. The command should rather be
            wget -q -O /dev/null http://www.doman.com/directory/playnice.php

            Reply

            1. thanks Frederik, that worked!!
              toppie!

              ivan

              Reply

  19. Has anyone else run into Google authentication problems? Perhaps a problem with non-alpha-numeric characters in a password? I’m getting failures despite having confirmed the correct credentials.

    Also, I’m on OS X 10.5.8 + MacPorts.

    Reply

  20. Has anyone been able to resolve the issue regarding the “iPhone Location: , ” error message? I’m running Ubuntu with PHP5 and ca-certificates installed. I got it working on my Mac, but I would like it to run all the time on my Ubuntu Desktop.

    Reply

  21. Any chance step by step instructions for newbies could be posted?

    Judging from the comments getting this to work requires hosting the files and running them on a server. Is that right? These are the kinds of things I’d love covered in a howto.

    Either way it’s great to see this functionality is possible.

    Reply

  22. It seems like the error many people are seeing is caused by either an incopatibility with PHP 5.2 or the older cURL module installed in Debian/Ubuntu.

    This is how I fixed it:
    I added these two lines to /etc/apt/sources.list

    deb http://php53.dotdeb.org stable all
    deb-src http://php53.dotdeb.org stable all

    Then I did an
    apt-get update && apt-get dist-upgrade

    Be warned: PHP 5.3 may brake your webservers PHP setup.

    Reply

    1. This fixed it for me. I’m getting some other errors now, but it still updates the iPhone location. Thanks!

      Reply

  23. Sounds nice. I would suggest to add an option to sync Fire Eagle location to Google Latitude. I think it should be possible to fetch the location from Fire Eagle with php and then update to Google Latitude like in this script. Or should I do it if I have time to do it? :)

    This would allow me to update my location with Navizon to Fire Eagle which would then update my Latitude location. On jailbroken iPhones you can install Navizon from Cydia which is able to update the location in the background every 10 minutes. Navizon is also availabe from iTunes Store but without the background update.

    And, if you are new to Navizon and you register you can put my referral code 565E585B595B so I get some points ;)

    Reply

  24. I’m having the same problem with no iphone location :(

    Runnin PHP 5.2.8 on a public web server – any tips to get this working?

    Cheers

    Simon

    Reply

  25. Ah – got it working – hadn’t enabled findmyiphone feature on the iphone!

    Reply

    1. simon are you also getting alllll the playnice.php.1 ….. files in the root of your directory after succesfull location tracing?

      kind regards.
      ivan

      Reply

  26. Has anyone figured out how to do something similar without a subscription to MobileMe?

    I’ve jailbroken my iPhone and know how to run scripts directly on it. Is there a command-line way on the iPhone terminal to get my current location? If there is, then I could probably figure out the rest.

    Thanks in advance,
    Steve

    Reply

  27. Nat – Just a quick thank you!~ This is exactly what I was looking for to allow me to track my family’s iphones. Especially with the Latitude piece, all I have to do is go to Latitude on my iPhone and have an instant update where everyone (well, at least their iphones anyhoo) is located.

    Made a few modifications to your excellent script to accept some cmd line args for the different members of the family, placed into cron on the iMac and we are in business.

    The last piece I want to do is add a Growl notification >Prowl so I can be sure the kids are at home after school with a scheduled Notification and AT&T “Track family members” can kiss my arse!

    Thanks again!

    Reply

    1. Could you please post your cmd line args changes? That’s exactly what I need and my PHP skillz are definitely not 7334! :)

      Reply

  28. could just be me, but i think this may have been broken with some of the recent changes to mobileme. anyone else having problems?

    Reply

  29. Yep, looks like it broke after they performed the maintenance last night. Bummer… :(

    Reply

  30. Would it be possible to set it up so that it sends out your position automatically every hour if your speed is less than 10 mph, but when it goes over 10mph it would do it every 30 seconds or so? That would save alot of battery life.

    Reply

  31. I have created a patch that fixes sosumi. Get it at http://github.com/ablyler/sosumi

    Reply

    1. Your wbseite has to be the electronic Swiss army knife for this topic.

      Reply

  32. I’ve merged Andy’s fixes back into Sosumi. All appears to be working again :)

    Reply

    1. I’ve just incorporated Andy’s fix into playnice so it works again.

      Thanks a lot Andy!

      Reply

    2. Your articles are for when it absolutely, positively, needs to be understood ovregnhit.

      Reply

  33. It looks like updating via the iGoogle widget is failing now. I took this as motivation to update playnice to use the mobile Google Latitude site for updating. Feel free to try my code out at: http://github.com/ablyler/playnice

    Reply

  34. Anyone tried this script with a single user account that’s enabled on more than one iPhone? I have a trial account that I enabled on mine and my wife’s iPhones so when I go to the Find My iPhone page I see both devices under my single user trial account. How does the script determine which iPhone to scrape from? Many thanks!!!

    Reply

  35. Does anyone else get

    Fatal error: Uncaught exception ‘Exception’ with message ‘Error during POST of ‘https://secure.me.com/device_mgmt/en’: Failed to open/read local data from file/application’ in …. class.sosumi.php:226

    I’m trying to figure out why this error is caused but I’m not making much progress.

    Reply

    1. I don’t see an exception but it’s not getting the location at all.

      Reply

      1. I lied. It looks like it’s working.

        Are you using the latest version from Andy’s GitHub repo?

        http://github.com/ablyler/playnice

        Reply

  36. FYI Andy was kind enough to set up a Google Group to discuss the playnice project:

    http://groups.google.com/group/playnice-developers

    See you there!

    Reply

  37. Anyone hear of a Windows version/patch?

    Reply

  38. I would like to run this on my Macbook, but have no idea what I’m doing. Nice idea though. Is it possible to compress this into an executable file that you could just download and run?

    Reply

  39. I just tried it, and it worked great, Google always stand out, lol.

    Reply

  40. Pants!

    Trying to run on my Linux server. Enter credentials but received the below error twice:

    “Fetching iPhone location…Error obtaining location: Unable to find location within ’300′ seconds”

    Reply

  41. This script is awesome! Thank you Everybody.

    Set this up on my www server under it’s own folder and it runs great.

    Just one question. the google-password.txt and mobileme-password.txt are scary.

    I’ve chmod 700 the playnice folder

    and i see those files are 600

    so is that everything? or can those files still be accessed or read by a bad guy or the spiders?

    answers appreciated!

    Reply

  42. Is the code for updating Google code outdated? I got everything from scraping to logging the scraped locations using Sosumi working, but Google Latitude doesn’t update.

    Reply


Copyright © 1998 - 2011 Nat Friedman