Android and Linux

Thursday, February 3, 2011

Posting HTTP data with Curl/Linux and Tasker/Android

It can often be useful to submit information to a website automatically. This can be anything from a login to a search term and anything else that uses the HTTP post method to send data to a site. This is possible in Linux and Android alike. The first part of this post will be a walkthrough of the principal using Linux, but the final command will be nearly the same with Tasker on Android, so if you're only interested in using this with Tasker, don't skip ahead. You should read the Linux part to understand what's going on.

In the example I will use here, I wanted to get TAFs from NOAA's Aviation Weather Center. TAFs are Terminal Area Forecasts, short term weather forecasts prepared at the airport for the 5 mile radius surrounding it. They are released several times per day and are useful because they are more specific than a general area forecast. This can be useful for me because I live about four miles to the north of the airport and work about 2 miles south of it, so I spend most of my time being subjected to the weather forecasted under the TAF umbrella.

If you click that link, the text box on the right of the page is what I want. You have to put in your airport code, select raw or translated, and select TAF or both METAR and TAF (METARs tell you the current weather conditions).

On Linux:

Submitting this information in Linux can be done with the command line utility called cURL. But first you have to figure out what information to send to the site and how to send it. You can do this by looking through the raw HTML source code, but it's a lot easier to use a perl script from the cURL website named formfind.pl. Once you have that, it's time to get started.

The first thing you want to do is download the webpage. Let's use cURL to download it and save it as index.html:

curl -o index.html http://aviationweather.gov/adds/tafs/index.php

Now we use the formfind script to read the file:

formfind < index.html

From the output, we need to find the correct form by looking at the clues. The clues are the URL that the data is posted to and the descriptions of the information being posted.

This is obviously the text box on the upper left of the webpage:

--- FORM report. Uses POST to URL "/adds/phputils/airport_weather.php"
Input: NAME="inputstring" VALUE="City, St" (TEXT)
Input: NAME="Go2" VALUE="Go" (SUBMIT)
--- end of FORM

The next one posts to "/adds/tafs/displayMetarsTafs.php", maybe that's what we want? No, judging by the rest of the form, it is for the "Plotted TAFs" section.

Finally, this is the section we're interested in:

--- FORM report. Uses POST to URL "/adds/tafs/index.php"
Input: NAME="station_ids" (TEXT)
Input: NAME="std_trans" VALUE="standard" (RADIO)
Input: NAME="std_trans" VALUE="translated" (RADIO)
Input: NAME="submit_taf" VALUE="Get TAFs" (SUBMIT)
Input: NAME="submit_both" VALUE="Get TAFs and METARs" (SUBMIT)
--- end of FORM

station_ids: this is the four letter station identifier, let's use KDEN for Denver, since it's the example on the page.
std_trans: there are two of these because they represent the radio button where you select standard of translated (standard is a short code, translated is a readable forecast)
submit_taf and submit_both: these allow you to select TAF, or both METAR and TAF.

Now we just need to craft our cURL command to assign values for the input names that we want in the format name=value and submit it to the correct page. We want to use KDEN as the station ID, we want the report to be translated and we just want the TAFs.

curl -d station_ids=KDEN -d std_trans=translated -d submit_taf="Get TAFs" "http://aviationweather.gov/adds/tafs/index.php"

That command will give you the raw HTML output for the page we wanted. If you go to the site with a browser and select the same values, you'll see the same output, except the browser obviously doesn't show the raw HTML. But we were successful in submitting the data we wanted and visiting the page we wanted.

With Tasker on Android:

This turns out to be very similar. There isn't a way to find the correct information to post, so you will have to visually examine the source code for the clues I mentioned above or find another way to get it. But once you do, the HTTP Post action in Tasker is very obviously similar:

Server/Port: aviationweather.gov

Path: adds/tafs/index.php

Data:
station_ids=KDEN
std_trans=translated
submit_taf="Get TAFs"

Any website you get or post to with Tasker is saved in the %HTTPD variable, or you can specify a file to save it to. Looking at either the variable or file, you'll see the same output as we had above with cURL in Linux. We've successfully submitted the information into the forms and gotten back the page we wanted.

Another useful tip:

If you're using cURL to login to a website, cURL can save the login cookie to a text file with the -c option. I haven't used this on the xda forum, but this would probably work.

curl -c cookie.txt -d name=yourname pass= yourpass "http://forum.xda-developers.com"

Now that you have the cookie, you can then visit the page and login automatically by loading that cookie file. cURL isn't available on Android, but wget is and it can use that same file:

wget --load-cookies=cookie.txt "http://forum.xda-developers.com/private.php" -O pm.html

That would save your private message page to the file called pm.html. You could, for example, automatically check them every hour and alert yourself if you have a new message.

Followers