Hello all,
I've been attempting to write a bash script that will query Google Trends for specified keywords (instead of having to use the site). One doesn't have to be logged in to view data, but to download csv files you must use your Google account. As you will see from the code below, I haven't gotten much past the authentication phase of the process.
This page explains how to authenticate for Google Analytics, but it doesn't seem to work when applied to Trends.
When I send user/pass info to Google, it replies with an authentication key, which the script grabs; this key (presumably) must be used each time one makes a request for a csv file, but I can't seem to get it to work. It just replies that I must be logged in to download the files.
Script below (please forgive my naivety, I'm not really a programmer.. I'm a political scientist):
Code:
#!/bin/bash
# trends
#
# A script to download Google Trends data
#
# Get login/password information
echo
echo -e Please Enter Google Login:
read login
echo
echo -e Please Enter Google Password:
stty -echo
read pass
stty echo
# Authenticate with Given Information
auth="$(curl https://www.google.com/accounts/ServiceLoginBoxAuth \
-d Email=$login -d Passwd=$pass \
-d accountType=GOOGLE \
-d source=mikegruz-cURL-0.1 \
-d service=trends \
| awk /Auth=.*/ | sed s/Auth=// )"
# Request search term from user
echo
echo -e Please enter desired search term:
echo
read search
# Transmit Trends Request to Google
feedUri="https://www.google.com/accounts/ServiceLoginBoxAuth"
curl $feedUri -s --header "Authorization: GoogleLogin auth=$auth" \ "http://www.google.com/trends/viz?q=$search&graph=all_csv&sa=N"