diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8675c2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.php +queries.txt +cookies.txt \ No newline at end of file diff --git a/README.md b/README.md index 9c53303..6e5c5ac 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,19 @@ google_alerts_creator ===================== -A class for creating Google Alerts in bulk ( even though there is not an official API ). + +Notes +----- +- A class for creating Google Alerts in bulk ( even though there is not an official API ). +- Based on this Stack Overflow question and answer + - http://stackoverflow.com/questions/13528747/how-to-create-new-google-alert-delivering-it-to-feed-using-php-curl + + +Usage +----- +1. copy `config.php.example` to `config.php` + - add your google account credentials + - set your desired alert settings +2. copy `queries.txt.example` to `queries.txt` + - add your alert queries to this file, one per line +3. execute `create_google_alerts.php` \ No newline at end of file diff --git a/config.php.example b/config.php.example new file mode 100644 index 0000000..1de3e97 --- /dev/null +++ b/config.php.example @@ -0,0 +1,41 @@ + \ No newline at end of file diff --git a/create_google_alerts.php b/create_google_alerts.php new file mode 100644 index 0000000..76d2064 --- /dev/null +++ b/create_google_alerts.php @@ -0,0 +1,17 @@ +createAlert( $query ); + } + + echo "\n\n"; + +?> \ No newline at end of file diff --git a/google_alerts_class.php b/google_alerts_class.php new file mode 100644 index 0000000..5080794 --- /dev/null +++ b/google_alerts_class.php @@ -0,0 +1,132 @@ +getFormFields( $data ); + + $formFields['Email'] = GOOGLE_USERNAME; + $formFields['Passwd'] = GOOGLE_PASSWORD; + unset($formFields['PersistentCookie']); + + $post_string = ''; + foreach($formFields as $key => $value) { + $post_string .= $key . '=' . urlencode($value) . '&'; + } + + $post_string = substr($post_string, 0, -1); + + curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth'); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); + + $result = curl_exec($ch); + + if (strpos($result, 'Redirecting') === false) { + var_dump($result); + die("Login failed"); + } else { + + curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts'); + curl_setopt($ch, CURLOPT_POST, 0); + curl_setopt($ch, CURLOPT_POSTFIELDS, null); + + $result = curl_exec($ch); + + // Create alert + + preg_match('/<input type="hidden" name="x" value="([^"]+)"/', $result, $matches); + + $post = array( + "x" => $matches[1], // anti-XSRF key + "q" => $search, // Search term + "t" => ALERT_RESULT_TYPE, // Result type: 7-everything, 8-discussions + "f" => ALERT_FREQUENCY, // Frequency: 0: as it happens, 1: daily, 6: weekly + "l" => ALERT_QUALITY, // All results: 1, best results: 0 + "e" => GOOGLE_USERNAME // Type of delivery: rss: "feed" + ); + + $post_string = ''; + + foreach($post as $key => $value) { + $post_string .= $key . '=' . urlencode($value) . '&'; + } + + $post_string = substr($post_string, 0, -1); + + curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/create'); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); + + $result = curl_exec($ch); + $matches = array(); + preg_match('#<a href="(http://www.google.com/alerts/feeds/[\d/]+)"#', $result, $matches); + + $top_alert = $matches[1]; + + return $top_alert; + } + } + + + private function getFormFields($data) + { + if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) { + $inputs = $this->getInputs($matches[1]); + + return $inputs; + } else { + die("didn't find login form"); + } + } + + private function getInputs($form) + { + $inputs = array(); + + $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); + + if ($elements > 0) { + for($i = 0; $i < $elements; $i++) { + $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); + + if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { + $name = $name[1]; + $value = ''; + + if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { + $value = $value[1]; + } + + $inputs[$name] = $value; + } + } + } + + return $inputs; + } + + } // end class + +?> \ No newline at end of file diff --git a/queries.txt.example b/queries.txt.example new file mode 100644 index 0000000..c8b52d9 --- /dev/null +++ b/queries.txt.example @@ -0,0 +1,2 @@ +some sample queries +a query with some ( boolean OR advanced ) terms \ No newline at end of file