#!/usr/bin/env php
<?php
/* Process twitter search atom feeds and send them as DM on your Twitter account */
$params = @parse_ini_file("config.ini", TRUE);
if (empty($params)) die("Unable to find conf.ini file\n");
if (empty($params["twitter"]["cache"])) $localfile = "/tmp/twitter.cache";
else $localfile = $params["twitter"]["cache"];
$extra_query = "&rpp=10&show_user=true";
$debug = false;
// Show debug info
function debug_log($msg) {
if ($GLOBALS["debug"] == true) {
print "$msg\n";
}
}
function direct_message($txt) {
global $params;
$login = $params["twitscan"]["login"];
$pass = $params["twitscan"]["password"];
if (empty($login)) {
$login = $params["twitter"]["login"];
$pass = $params["twitter"]["password"];
}
$data = array();
$data["user"] = $login;
$data["text"] = substr($txt, 0, 140);
$query = array();
foreach($data as $key => $value) {
$query[] = "$key=".urlencode($value);
}
$query = join("&", $query);
$userpass = $login . ":" . $pass;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/direct_messages/new.xml');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERPWD, $userpass);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
$error = curl_error($ch);
if (!empty($error)) print "ERROR: $error";
curl_close($ch);
}
function fetch_atom($url){
global $debug_with, $extra_query;
if ($debug_with) $url = $debug_with;
debug_log("URL: $url$extra_query");
$content = @file_get_contents($url.$extra_query);
if (empty($content)) return false;
else {
$xml = @simplexml_load_string($content);
return $xml;
}
}
function process_atom($xml, $previous_id) {
$results = array();
foreach ($xml->entry as $entry) {
$id = end(split(":", $entry->id));
if (intval($id)>$previous_id) {
$title = $entry->title;
$results["$id"] = $title;
}
}
if (empty($results)) return $previous_id;
else {
$last_id = reset(array_keys($results));
foreach ($results as $id => $title) direct_message("@${title}");
return $last_id;
}
}
function load_previous_ids() {
$previous_ids = @parse_ini_file($GLOBALS["localfile"].".twitscan", false);
if (empty($previous_ids)) return array();
else return $previous_ids;
}
function save_last_ids($data) {
$result = array();
foreach ($data as $key => $value) $result[]="$key = $value\n";
$result = join("", $result);
debug_log("CACHE:\n$result");
$fp = fopen($GLOBALS["localfile"].".twitscan", "w");
fwrite($fp, $result);
fclose($fp);
}
function monitor_twits($searches) {
if (empty($searches)) die("No search configured\n\n");
$cache = load_previous_ids();
foreach ($searches as $search) {
$url_key = sha1($search);
$previous_id = intval($cache["$url_key"]);
if (empty($previous_id)) $previous_id = 0;
$xml = fetch_atom($search);
if (!$xml) $last_id = $previous_id;
else $last_id = process_atom($xml, $previous_id);
$cache["$url_key"] = $last_id;
save_last_ids($cache); // we don't wait in case of pb
}
}
if (!empty($params['twitscan'])) $searches = $params['twitscan']['search'];
else $searches = false;
monitor_twits($searches);