-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Timothy Brian Jones
authored and
Timothy Brian Jones
committed
Aug 9, 2013
1 parent
214ea30
commit 982fe8b
Showing
6 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
config.php | ||
queries.txt | ||
cookies.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
|
||
// --- GOOGLE ACCOUNT SETTINGS ------------------------- | ||
|
||
|
||
define( 'GOOGLE_USERNAME', YOUR-GOOGLE-EMAIL-ADDRESS ); | ||
define( 'GOOGLE_PASSWORD', YOUR-GOOGLE-EMAIL-PASSWORD ); | ||
|
||
|
||
// --- ALERT SETTINGS ---------------------------------- | ||
|
||
|
||
// alert frequency ( how often you recieve emails ) | ||
// - as it happens: 0 | ||
// - daily: 1 | ||
// - weekly: 6 | ||
define( 'ALERT_FREQUENCY', 1 ); | ||
|
||
// result type ( what to seaerch ) | ||
// - everything: 7 | ||
// - discussions: 8 | ||
// - news: 1 | ||
// - blogs: 4 | ||
// - video: 9 | ||
// - books: 22 | ||
define( 'ALERT_RESULT_TYPE', 7 ); | ||
|
||
// result quality filter | ||
// - best results: 0 | ||
// - all results: 1 | ||
define( 'ALERT_QUALITY', 0 ); | ||
|
||
|
||
// --- CURL SETTINGS ---------------------------------- | ||
|
||
|
||
define( 'GOOGLE_COOKIEFILE', 'cookies.txt' ); | ||
|
||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
require( dirname( __FILE__ ) . '/google_alerts_class.php' ); | ||
|
||
$Ga = new Google_alerts_class(); | ||
|
||
$query_file_contents = file_get_contents( dirname( __FILE__ ) . '/queries.txt' ); | ||
$queries = explode( "\n", $query_file_contents ); | ||
|
||
foreach( $queries as $query ) { | ||
echo "\n - '$query' Alert Created"; | ||
$Ga->createAlert( $query ); | ||
} | ||
|
||
echo "\n\n"; | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
require( dirname( __FILE__ ) . '/config.php' ); | ||
|
||
class Google_alerts_class | ||
{ | ||
|
||
public function createAlert( $search ) { | ||
|
||
$ch = curl_init(); | ||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); | ||
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | ||
curl_setopt($ch, CURLOPT_COOKIEJAR, GOOGLE_COOKIEFILE); | ||
curl_setopt($ch, CURLOPT_COOKIEFILE, GOOGLE_COOKIEFILE); | ||
curl_setopt($ch, CURLOPT_HEADER, 0); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | ||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); | ||
curl_setopt($ch, CURLOPT_TIMEOUT, 120); | ||
|
||
curl_setopt($ch, CURLOPT_URL, | ||
'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage'); | ||
$data = curl_exec($ch); | ||
|
||
$formFields = $this->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, '<title>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 | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
some sample queries | ||
a query with some ( boolean OR advanced ) terms |