Actualizar el estado de Kopete con Twitter
Posted on Junio 13, 2008
Filed Under Sin categoría | Leave a Comment
Un pequeño demonio en PHP para actualizar nuestro estado de kopete, desde twitter … es simple, pero funciona…
Comentarios y sugerencias son bienvenidas.
// id o username de twitter$usuarioTwitter = "bilsoncl";// tiempo espera en segundos para actualizar$tiempoRefresco = 300;$pid = pcntl_fork();if($pid == -1){ die("Error en el fork");}if($pid) { // Matar el padre exit();}// De aqui en adelante solo se ejecuta si soy el hijo y futuro daemon// Lo siguiente que hacemos es soltarnos de la terminal de controlif (!posix_setsid()) { die ("Error pasando a background");}// De este punto en adelante debemos cambiarnos de directorio y// hacemos las recomendaciones de Wikipedia para un daemonchdir("/");umask(0);// Capturo kill -15pcntl_signal(SIGTERM, "exit_daemon");// Si estamos aqui oficialmente somos un daemon// revisamos la ejecucion por cada linea de codigodeclare(ticks = 1);while(1) { updateTwitter($usuarioTwitter); sleep($tiempoRefresco);}// Esta es mi funcion de salidafunction exit_daemon($signo){ // terminando el demonio exit();}function updateTwitter($usuario){ $twitteruser = "bilsoncl"; $url = "http://twitter.com/statuses/user_timeline/{$twitteruser}.xml"; $twitterupdates = file_get_contents($url); $status = xml2array($twitterupdates); `dcop kopete KopeteIface setAway "Twitteando: {$status['statuses'][0]['status'][0]['text']}" true`;}function xml2array($UPSxml){ $p = xml_parser_create(); xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($p, $UPSxml, $values, $idx); xml_parser_free($p); // tracking used keys $usedKeys = array(); $deepLevel = -1; // start a php array string (evaluated later) $forEvalPrefix = '$xml_array'; // loop throught the value array foreach ($values as $key => $val) { $tagName = $val['tag']; // pass the key tag into a more friendly looking variable $level = $val['level']; // idem if($val['type'] == 'open') { $deepLevel++; // increase deep level $forEvalPrefix .= '[\''. $tagName .'\']'; // begin used keys checks to allow multidimensionatity under the same tag (isset($usedKeys[$level][$tagName])) ? $usedKeys[$level][$tagName]++ : $usedKeys[$level][$tagName] = 0; $forEvalPrefix .= '['. $usedKeys[$level][$tagName] .']'; } if($val['type'] == 'complete') { ($level > $deepLevel) ? $deepLevel++ : ''; // increase $deepLevel only if current level is bigger $tagValue = addslashes($val['value'] ); // format the value for evaluation as a string $forEvalSuffix = '[\''. $tagName .'\'] = \''. $tagValue .'\';'; // create a string to append to the current prefix $forEval = $forEvalPrefix . $forEvalSuffix; // (without "$php_used_prefix"...) eval($forEval); // write the string to the array structure } if($val['type'] == 'close') { unset($usedKeys[$deepLevel]); // Suppress tagname's keys useless $deepLevel--; $forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off the used keys node $forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off the end level of the array string prefix } } return $xml_array;}

Leave a Reply