Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: XVicarious on August 30, 2011, 12:31:16 am
-
xchat.emit_print problem? I don't know!
Postby XVicarious ยป 31 Aug 2011 16:28
Okay I have been writing a script in Python for XChat over the past few days... I have gotten everything to work through trial and error, except one thing, replacing incoming text. That just seems to not want to work!
My script is as follows:
__module_name__ = "XTranzlator"
__module_version__ = "0.7.0"
__module_description__ = "Translate and Send!"
print "\0034",__module_name__, __module_version__, "has been loaded\003"
import xchat
import re, sys, urllib, traceback, codecs, json
if (sys.getdefaultencoding() != "utf-8"):
oldout, olderr = sys.stdout, sys.stderr
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter('utf-8')(oldout)
sys.stderr = codecs.getwriter('utf-8')(olderr)
api_url = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate"
app_id = 'myappidgoeshere'
def _unicode_urlencode(params):
"""
A unicode aware version of urllib.urlencode.
Borrowed from pyfacebook :: http://github.com/sciyoshi/pyfacebook/
"""
if isinstance(params, dict):
params = params.items()
return urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params])
def _run_query(args):
"""
takes arguments and optional language argument and runs query on server
"""
data = _unicode_urlencode(args)
sock = urllib.urlopen(api_url + '?' + data)
result = sock.read()
if result.startswith(codecs.BOM_UTF8):
result = result.lstrip(codecs.BOM_UTF8).decode('utf-8')
elif result.startswith(codecs.BOM_UTF16_LE):
result = result.lstrip(codecs.BOM_UTF16_LE).decode('utf-16-le')
elif result.startswith(codecs.BOM_UTF16_BE):
result = result.lstrip(codecs.BOM_UTF16_BE).decode('utf-16-be')
print result
return json.loads(result)
def set_app_id(new_app_id):
global app_id
app_id = new_app_id
def translate(text, source, target, html=False):
"""
action=opensearch
"""
if not app_id:
raise ValueError("AppId needs to be set by set_app_id")
query_args = {
'appId': app_id,
'text': text,
'from': source,
'to': target,
'contentType': 'text/plain' if not html else 'text/html',
'category': 'general'
}
return _run_query(query_args)
def intercept(word, word_eol, userdata):
line = word_eol[0]
line = translate(line, myLang, theirLang)
xchat.command(" ".join(["msg", xchat.get_info("channel"), line]))
return xchat.EAT_ALL
def transall(word, word_eol, userdata):
line = translate(word[1], theirLang, myLang)
xchat.emit_print("Channel Message", word[0], line, "@", None)
return xchat.EAT_XCHAT
def lang_cmd(word, word_eol, userdata):
global myhook
global myLang
global theirLang
bold = "\033[1m"
stop = "\003[0;0m"
if word[1] == "off":
xchat.unhook(interceptHook)
xchat.unhook(printHook)
print "\0034XTranzlator is now off, to turn on, type /LANG on\003"
if word[1] == "on":
myLang = "en"
theirLang = "fr"
print "\0034The langauges have been set to default values (myLang = \"en\" and theirLang = \"fr\"!\003"
interceptHook = xchat.hook_command("",intercept)
printHook = xchat.hook_print("Channel Message", transall)
print "\0034XTranzlator is now on, to turn off, type /LANG off\003"
if word[1] == "my":
if len(word[2]) < 2:
print "\0034Sorry, that isn't a vaild 2 letter language code!\003"
else:
myLang = word[2]
print "\0034Your langauge is now ", myLang, "\003"
if word[1] == "their":
if len(word[2]) < 2:
print "\0034Sorry, that isn't a vaild 2 letter language code!\003"
else:
theirLang = word[2]
print "\0034You are now speaking in ", theirLang, "\003"
if word[1] == "supported":
print " \002ar\002 Arabic \n \002cs\002 Czech\n \002da\002 Danish\n \002de\002 German \n \002en\002 English \n \002et\002 Estonian \n \002fi\002 Finnish \n \002fr\002 French \n \002nl\002 Dutch \n \002el\002 Greek \n \002he\002 Hebrew \n \002ht\002 Haitian Creole \n \002hu\002 Hungarian \n \002id\002 Indonesian \n \002it\002 Italian \n \002ja\002 Japanese \n \002ko\002 Korean \n \002lt\002 Lithuanian \n \002lv\002 Latvian \n \002no\002 Norwegian \n \002pl\002 Polish \n \002pt\002 Portuguese \n \002ro\002 Romanian \n \002es\002 Spanish \n \002ru\002 Russian \n \002sk\002 Slovak \n \002sl\002 Slovene \n \002sv\002 Swedish \n \002th\002 Thai \n \002tr\002 Turkish \n \002uk\002 Ukrainian \n \002vi\002 Vietnamese \n \002zh-CHS\002 Simplified Chinese \n \002zh-CHT\002 Traditional Chinese"
return xchat.EAT_ALL
xchat.hook_command("LANG", lang_cmd, help="/LANG <my/their> <lang> Changes your langauge (my) to <lang> or their langauge (their) to <lang>")
I have traced it back to the transall function, but I can not seem to figure out what is wrong. That one piece of code crashes XChat when someone else says something. Please please help me :(.
edit: got it not to crash, but line seems to not get run through translate... And I get a traceback "TypeError: must be string, not None"
-
If in function translate json.load(...) returns None for some reason, then you'll get the error. (Seeing as None['responseData'] makes no sense)
-
Sorry calcdude84se, this is old I should have updated. The first post is updated.
-
Could you give the full error that you're getting, like you had it earlier? The line numbers etc. are incredibly helpful.
-
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Application Data\X-Chat 2\tranzlator.py", line 74, in transall
xchat.emit_print("Channel Message", word[0], lineAll, "@", None)
TypeError: must be string, not None
I have a feeling its that "None" at the end, but if I leave it out, it crashes, if its a string it messes up... IDK...