Twitter search feed translation Mk2, command_runner auto-helper. #4

Open
wants to merge 3 commits into
from
View
@@ -1,5 +1,5 @@
#!/usr/bin/python
-import os, sys, re, subprocess
+import os, sys, re, subprocess, difflib
# If this script is set as your command handler, when any ?command is run in IRC it will
# look in the path defined below for a script matching that command name and run it.
@@ -15,9 +15,16 @@
command = bits[3].lower()
found = False
+commands = []
+
if re.match('^[a-z0-9]+$', command):
for file in os.listdir(path):
+ # Build the index, in case we never find a command
+ m = re.match('^([a-z0-9]+)\.[a-z]+$', file)
+ if m:
+ commands.append(m.group(1))
+
if re.match('^%s\.[a-z]+$' % command, file):
found = True
@@ -27,12 +34,25 @@
stdout = proc.stdout
while True:
+
# We do this to avoid buffering from the subprocess stdout
- print os.read(stdout.fileno(), 65536),
+ data = os.read(stdout.fileno(), 65536)
+
+ # Kill useless lines in the output
+ for line in re.split("[\r\n]+", data):
+ line = line.strip()
+ if line:
+ print line
+
sys.stdout.flush()
if proc.poll() != None:
break
+
if found == False:
- print "Unknown command '%s'" % command
+
+ # Didn't find a command to run. Maybe can we help.
+ matches = difflib.get_close_matches(command, commands, 1)
+ if matches:
+ print "%s, I don't understand '%s'. Did you mean '%s'?" % (bits[0], command, matches[0])
View
@@ -18,7 +18,8 @@
*/
- $translateTo = ''; // Set this to a two-letter language code to enable automatic translation
+ $translateTo = 'en'; // Set this to a two-letter language code to enable automatic translation
+ $translationService = 'google'; // Can be 'google' or 'microsoft'
if (count($argv) < 3) {
print "Usage: twitter-feed.php '#channel' 'searchterm' [url filter regexp, [url-filter regexp, ...]]\n";
@@ -54,7 +55,7 @@
}
foreach ($json->results as $result) {
- if ($message = processMessage($result->text, $filters, $translateTo)) {
+ if ($message = processMessage($result->text, $filters, $translateTo, $translationService)) {
print "{$result->from_user}: {$message}\n";
}
}
@@ -68,7 +69,7 @@
sleep(30);
}
- function processMessage ($string, $filters, $lang) {
+ function processMessage ($string, $filters, $lang, $service) {
// Resolve shortened URL's to the full thing for filtering
preg_match_all('/http:\/\/[^ $)]+/i', $string, $urls);
@@ -82,13 +83,13 @@ function processMessage ($string, $filters, $lang) {
$string = cleanString($string);
- if ($lang && ($aTranslation = translateString($string, $lang))) {
+ if ($lang && ($aTranslation = translateString($string, $lang, $service))) {
$string = "{$aTranslation['string']} [Lang: {$aTranslation['detectedLang']}]";
}
if ($filters) {
foreach ($filters as $filter) {
- if (preg_match("/{$filter}/i", $string)) {
+ if (preg_match("/{$filter}/iu", $string)) {
$string = '';
break;
}
@@ -98,7 +99,63 @@ function processMessage ($string, $filters, $lang) {
return $string;
}
- function translateString ($string, $lang) {
+ function translateString ($string, $lang, $service = 'google') {
+
+ if ($service == 'google') {
+ $translation = googleTranslateString($string, $lang);
+
+ } elseif ($service == 'microsoft') {
+ $translation = microsoftTranslateString($string, $lang);
+
+ } else {
+ trigger_error("Unknown translation service '{$service}'\n", E_USER_WARNING);
+ $translation = false;
+ }
+
+ return $translation;
+ }
+
+ function microsoftTranslateString ($string, $lang) {
+
+ $appId = '4BEC23A5017EAAA3ECC828FA6148514879BCD944';
+
+ $sourceLang = file(
+ "http://api.microsofttranslator.com/V2/Http.svc/Detect?appId={$appId}&text="
+ .urlencode($string)
+ );
+
+ $detectedLang = false;
+ if ($sourceLang) {
+ $sourceLang = strip_tags(implode('', $sourceLang));
+
+ if (strtolower($sourceLang) != strtolower($lang) && trim($sourceLang)) {
+
+ $string = file(
+ "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId={$appId}&to={$lang}&text="
+ .urlencode($string)
+ );
+
+ if ($string) {
+ $string = strip_tags(implode('', $string));
+
+ $detectedLang = $sourceLang;
+ $string = cleanString($string);
+ }
+ }
+
+ }
+
+ if ($detectedLang) {
+ return array(
+ 'string' => $string,
+ 'detectedLang' => $detectedLang,
+ );
+ }
+
+ return false;
+ }
+
+ function googleTranslateString ($string, $lang) {
$translation = file(
"http://ajax.googleapis.com/ajax/services/language/translate?langpair=|{$lang}&v=1.0&q="