Menu

[r176]: / plugins / urbandictionary / main.py  Maximize  Restore  History

Download this file

129 lines (118 with data), 6.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
import sys, os, gettext, platform, utils
reload(sys)
#Pluginsystem support
import pluginsystem
#Configsystem support
import configsystem
#Urban Dictionary support
import urbandictionary
#UTF-8 support
sys.setdefaultencoding('utf-8')
#Locale support
try:
LOCALE_DIR = "plugins" + os.path.sep + "urbandictionary" + os.path.sep + "locale"
t = gettext.translation("main", LOCALE_DIR)
_ = t.ugettext
except:
_ = utils.fake_locale
#Global vars
lasturbandictionarypage = 1
lasturbandictionarysearchedstring = ""
capabilities = [_("!urbandictionary"), _("!urbandictionary-more"), _("!urbandictionary-config"),
_("?urbandictionary"), _("?urbandictionary-more"), _("?urbandictionary-config")]
config = (
((_("Appearance")),
(
("plaintextcolor", ("color", "#000000", (_("Color of plain text")))),
("wordnumbercolor", ("color", "#0d0000", (_("Color of word number")))),
("wordcolor", ("color", "#000000", (_("Color of word")))),
("textcolor", ("color", "#000000", (_("Color of text")))),
)
),
(2)
)
#Init event
@pluginsystem.register("init")
def init(*args, **kwargs):
global config
tmp_config = configsystem.load_config("urbandictionary", config)
if not tmp_config == None:
config = tmp_config
#Reload-config event to make a specific plugin reload it's config
@pluginsystem.register("reload_config")
def reload_config(*args, **kwargs):
global config
if "plugin" in kwargs:
if kwargs["plugin"] == "urbandictionary":
tmp_config = configsystem.load_config("urbandictionary", config)
if not tmp_config == None:
config = tmp_config
else:
print _("WARNING from Urbandictionary plugin: reload_config event was called with inappropiate number of arguments! Please check code.")
#Triggered after return/enter hit
@pluginsystem.register("analyze_command")
def analyze_command(*args, **kwargs):
global lasturbandictionarysearchedstring, lasturbandictionarypage, config
if "data" in kwargs and "obj" in kwargs:
qcmd = kwargs["data"] #in case we need the QString instead of...
cmd = str(kwargs["data"]) #...python string
console = kwargs["obj"] #save the console from which we got the call
if cmd.startswith(_("!urbandictionary")) and qcmd.at(len(_("!urbandictionary"))).isSpace():
searchstring = cmd[len(_("!urbandictionary"))+1:]
#Change last page to 1 if search string is not the same as the last search string, and change last searched string
if not lasturbandictionarysearchedstring == searchstring:
lasturbandictionarypage = 1
lasturbandictionarysearchedstring = searchstring
#Make the search
uresults = urbandictionary.search(searchstring, lasturbandictionarypage)
if uresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
output = ""
i = 1
for item in uresults:
output += "<font color='" + configsystem.value(config, 'wordnumbercolor') + "'>" + item[0] + "</font>"
output += "<font color='" + configsystem.value(config, 'wordcolor') + "'>" + item[1] + "</font>"
output += "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + "- " + "</font>"
output += "<font color='" + configsystem.value(config, 'textcolor') + "'>" + item[2] + "</font>" + "<br>"
if i < len(uresults):
output += "<br>"
i += 1
console.printString(output, False, False)
elif cmd == _("!urbandictionary-more"):
#Increase last page before doing the search
lasturbandictionarypage = lasturbandictionarypage + 1
#Make the search
uresults = urbandictionary.search(lasturbandictionarysearchedstring, lasturbandictionarypage)
if uresults == None:
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("No results to show or an error occurred.") + "</font>", False, False)
return
output = ""
i = 1
for item in uresults:
output += "<font color='" + configsystem.value(config, 'wordnumbercolor') + "'>" + item[0] + "</font>"
output += "<font color='" + configsystem.value(config, 'wordcolor') + "'>" + item[1] + "</font>"
output += "<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + "- " + "</font>"
output += "<font color='" + configsystem.value(config, 'textcolor') + "'>" + item[2] + "</font>" + "<br>"
if i < len(uresults):
output += "<br>"
i += 1
console.printString(output, False, False)
elif cmd == _("!urbandictionary-config"):
configsystem.config_dialog(config, "urbandictionary", console)
elif cmd == _("?urbandictionary"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !urbandictionary will search a string in Urban Dictionary and it will print the results in this console. Example of usage: !urbandictionary wtf") + "</font>", False, False)
elif cmd == _("?urbandictionary-more"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !urbandictionary-more will search for more results in Urban Dictionary based on the last made search.") + "</font>", False, False)
elif cmd == _("?urbandictionary-config"):
console.printString("<font color='" + configsystem.value(config, 'plaintextcolor') + "'>" + _("The command !urbandictionary-config will show up a config dialog which will let you configure the behavior of this plugin.") + "</font>", False, False)
else:
print _("WARNING from Urbandictionary plugin: analyze_command event was called with inappropiate number of arguments! Please check code.")
#Send all available commands to the plugin system
@pluginsystem.register("get_capabilities")
def get_capabilities(*args, **kwargs):
if "cap" in kwargs:
kwargs["cap"].extend(capabilities)
else:
print _("WARNING from Urbandictionary plugin: get_capabilities event was called with inappropiate number of arguments! Please check code.")