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

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

Open
wants to merge 3 commits into
base: 76367ef
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions examples/command_runner.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand All @@ -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])
69 changes: 63 additions & 6 deletions examples/twitter-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
}
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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="
Expand Down