|
<?php |
|
// ---------------------------------------- |
|
// Fetch Google Reader tag atom feeds |
|
// ---------------------------------------- |
|
// |
|
// Authored by Glenn Marcus |
|
// @glennmarcus |
|
// http://linkedin.com/in/glennmarcus |
|
// |
|
// Derived from original work done by |
|
// Dave Shea. |
|
// http://snipt.net/thejames/php-google-reader-authentication-script/ |
|
// |
|
|
|
// these are the urls we'll need to access various services |
|
$urlAuth = "https://www.google.com/accounts/ClientLogin"; |
|
$urlAtom = "https://www.google.com/reader/atom"; |
|
|
|
// our array of login data |
|
$login = array( |
|
"service" => "reader", |
|
|
|
// Google email address the account holder |
|
"Email" => "YOUR-GOOGLE-USERNAME", |
|
|
|
// the account's password in plaintext |
|
"Passwd" => "YOUR-GOOGLE-PASSWORD", |
|
|
|
// an identifying name for your script, can be anything |
|
"source" => "my-reader-script" |
|
); |
|
|
|
// Check that we have an updated email |
|
if (strpos($login["Email"], '@') == FALSE) { |
|
echo "You need to replace YOUR-GOOGLE-USERNAME and YOUR-GOOGLE-PASSWORD with your credentials." . PHP_EOL; |
|
die(); |
|
} |
|
|
|
// first step is to authenticate |
|
// let's build a POST request using the login data array |
|
$postRequest = ""; |
|
foreach($login as $field => $value) { |
|
$postRequest .= $field . "=" . $value . "&"; |
|
} |
|
|
|
// start buffering what we get back |
|
ob_start(); |
|
$ch = curl_init($urlAuth); |
|
curl_setopt ($ch, CURLOPT_POST, true); |
|
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postRequest); |
|
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); |
|
curl_exec ($ch); |
|
curl_close ($ch); |
|
// throw the buffer into a variable |
|
$loginResult = ob_get_contents(); |
|
ob_end_clean(); |
|
|
|
// we just received three lines of ugliness to contend with. |
|
// each line is a huge string preceded with an ID |
|
// the IDs are: SID, LSID, and Auth; we only want Auth |
|
$loginResultsArray = preg_split('/\r\n|\n|\r/', $loginResult); |
|
$authString = ""; |
|
foreach ($loginResultsArray as $value) { |
|
if (preg_match('/Auth=(.*)/', $value, $matches)) { |
|
$authString = $matches[1]; |
|
} |
|
}; |
|
|
|
|
|
// Build an action to fetch the label tags from Google Reader as json |
|
$action = sprintf("https://www.google.com/reader/api/0/tag/list?output=json&ck=%1", time()); |
|
|
|
// start buffering what we get back |
|
ob_start(); |
|
$ch = curl_init(); |
|
curl_setopt ($ch, CURLOPT_URL, $action); |
|
//curl_setopt ($ch, CURLOPT_HTTPGET, true); |
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, array( |
|
"Authorization: GoogleLogin auth=" . $authString |
|
)); |
|
curl_exec ($ch); |
|
curl_close ($ch); |
|
// throw the buffer into a variable |
|
$json_tags = ob_get_contents(); |
|
ob_end_clean(); |
|
|
|
// Extract all the tag names |
|
$json_tags_decoded = json_decode($json_tags, TRUE); |
|
$tag_items = $json_tags_decoded['tags']; |
|
if (count($tag_items) == 0){ |
|
echo "No tags found. Exiting." . PHP_EOL; |
|
die(); |
|
} |
|
|
|
$tags = array(); |
|
foreach($tag_items as $tag_item) { |
|
$tag_id = $tag_item['id']; |
|
// look for only ids with /label/ in the name |
|
$tag_id_array = explode("/label/", $tag_id); |
|
if (count($tag_id_array) > 1) |
|
$tags[] = $tag_id_array[1]; |
|
} |
|
|
|
$num_tags = count($tags); |
|
echo "Found " . $num_tags . " tags.". PHP_EOL; |
|
|
|
// Make an output folder |
|
$output_folder_name = @"./output"; |
|
if (is_dir($output_folder_name) == 0) { |
|
echo "Making ./output folder" . PHP_EOL; |
|
mkdir($output_folder_name); |
|
} |
|
|
|
// Save out the tag names into a file |
|
$tag_names_file_name = $output_folder_name . "/tagnames.txt"; |
|
$tag_names_string = implode(PHP_EOL, $tags); |
|
echo "Saving list of tag names to '" . $tag_names_file_name . "'" . PHP_EOL; |
|
file_put_contents($tag_names_file_name, $tag_names_string); |
|
|
|
|
|
$num_tags_processed = 1; |
|
foreach($tags as $tag) { |
|
|
|
echo PHP_EOL . "Processing '" . $tag . "' (" . $num_tags_processed . " of ". $num_tags. ")" . PHP_EOL; |
|
|
|
echo "Fetching feed from Google Reader" . PHP_EOL; |
|
|
|
// Fetch the tag atom feed from Google Reader |
|
$action = $urlAtom . "/user/-/label/" . rawurlencode($tag) . "?n=1000"; |
|
|
|
// start buffering what we get back |
|
ob_start(); |
|
$ch = curl_init(); |
|
curl_setopt ($ch, CURLOPT_URL, $action); |
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, array( |
|
"Authorization: GoogleLogin auth=" . $authString |
|
)); |
|
curl_exec ($ch); |
|
curl_close ($ch); |
|
// throw the buffer into a variable |
|
$tag_feed = ob_get_contents(); |
|
ob_end_clean(); |
|
|
|
$tag_feed_file_name = $output_folder_name . "/" . $tag . ".xml"; |
|
echo "Saving feed to '" . $tag_feed_file_name . "'" . PHP_EOL; |
|
file_put_contents($tag_feed_file_name, $tag_feed); |
|
|
|
$num_tags_processed = $num_tags_processed + 1; |
|
} |
|
|
|
?> |