veilleperso / nabaztwit

Scripts to monitor twitter searches and to get results and your twitter replies in TTS on your Nabaztag

This URL has Read+Write access

nabaztwit / twitscan.php
100755 122 lines (105 sloc) 3.444 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/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);