Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Commit

Permalink
Add automagic translation of tweets to english (or whatever language you
Browse files Browse the repository at this point in the history
like), drop refresh interval to 30s.
  • Loading branch information
Jonty authored and RJ committed May 12, 2010
1 parent 800666e commit e96a16a
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions examples/twitter-feed.php
Expand Up @@ -28,11 +28,13 @@

$originalUrl = "?since_id=4028479400&q=".urlencode($search);
$baseUrl = "http://search.twitter.com/search";
$lang = 'en';

$firstRun = true;
$refreshUrl = $originalUrl;

while (1) {

$source = file("{$baseUrl}{$refreshUrl}&refresh=true");
if ($source) {
$json = json_decode(implode('', $source));
Expand All @@ -46,7 +48,7 @@
}

foreach ($json->results as $result) {
if ($message = processMessage($result->text, $filters)) {
if ($message = processMessage($result->text, $filters, $lang)) {
print "{$result->from_user}: {$message}\n";
}
}
Expand All @@ -57,11 +59,10 @@
}
}

sleep(60);
sleep(30);
}

function processMessage ($string, $filters) {
$string = utf8_decode($string);
function processMessage ($string, $filters, $lang) {

// Resolve shortened URL's to the full thing for filtering
preg_match_all('/http:\/\/[^ $)]+/i', $string, $urls);
Expand All @@ -74,6 +75,11 @@ function processMessage ($string, $filters) {
}

$string = cleanString($string);

if ($aTranslation = translateString($string, $lang)) {
$string = "{$aTranslation['string']} [Lang: {$aTranslation['detectedLang']}]";
}

if ($filters) {
foreach ($filters as $filter) {
if (preg_match("/{$filter}/i", $string)) {
Expand All @@ -86,8 +92,34 @@ function processMessage ($string, $filters) {
return $string;
}

function translateString ($string, $lang) {

$translation = file(
"http://ajax.googleapis.com/ajax/services/language/translate?langpair=|{$lang}&v=1.0&q="
.urlencode($string)
);

$detectedLang = false;
if ($translation) {
$json = json_decode(implode('', $translation));
$sourceLang = $json->responseData->detectedSourceLanguage;
if ($sourceLang != $lang && trim($sourceLang)) {
$detectedLang = $sourceLang;
$string = cleanString($json->responseData->translatedText);
}
}

if ($detectedLang) {
return array(
'string' => $string,
'detectedLang' => $detectedLang,
);
}

return false;
}

function cleanString ($string) {
$string = preg_replace('/\\\u([[:alnum:]]{4})/u', '?', $string);
$string = str_replace('\/','/', urldecode($string));
$string = html_entity_decode($string, null, 'UTF-8');
return htmlspecialchars_decode($string);
Expand All @@ -103,5 +135,5 @@ function getRedirect ($url) {
curl_close($curl);

preg_match('/Location:(.*?)[\r\n]/i', $headers, $redirect);
return $redirect ? urlencode(trim($redirect[1])) : '';
return $redirect ? urlencode(trim($redirect[1])) : false;
}

0 comments on commit e96a16a

Please sign in to comment.