Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
## jsb imports | ||
from jsb.lib.commands import cmnds | ||
from jsb.lib.examples import examples | ||
## system imports | ||
from socket import gethostbyname | ||
from socket import getfqdn | ||
import re | ||
|
||
def handle_dns(bot, event): | ||
if not event.rest: | ||
event.missing("<ip> or <hostname>") | ||
return | ||
query = event.rest.strip() | ||
ippattern = re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", query) | ||
hostpattern = re.match(r"(\w+://)?(?P<hostname>\S+\.\w+)", query) | ||
if ippattern: | ||
try: | ||
answer = getfqdn(ippattern.group(0)) | ||
event.reply("%(hostname)s is %(answer)s" % | ||
{"hostname": query, "answer": answer}) | ||
except: | ||
event.reply("Couldn't lookup ip") | ||
|
||
elif hostpattern: | ||
try: | ||
answer = gethostbyname(hostpattern.group('hostname')) | ||
event.reply("%(ip)s is %(answer)s" % | ||
{"ip": query, "answer": answer}) | ||
except: | ||
event.reply("Couldn't look up the hostname") | ||
|
||
else: return | ||
|
||
cmnds.add("dns", handle_dns, ["OPER", "USER", "GUEST"]) | ||
examples.add("dns", "resolve the ip or the hostname", "dns google.com") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## jsb imports | ||
|
||
from jsb.lib.commands import cmnds | ||
from jsb.utils.url import geturl2 | ||
from jsb.imports import getjson | ||
# import logging | ||
import re | ||
from urllib import quote | ||
|
||
# URL = "http://imdbapi.poromenos.org/js/?name=%s" # for this tweaking is needed, but it doesn't appear to work as adviced. E.g., inception return None | ||
URL = r"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%(text)s&langpair=%(from)s|%(to)s" | ||
|
||
def parse_pair(text): | ||
trans = re.match("^(?P<from>[a-z]{2}) +(?P<to>[a-z]{2}) +(?P<txt>.*)$", | ||
text) | ||
return {'from': trans.group('from'), | ||
'to': trans.group('to'), | ||
'text': quote(trans.group('txt'))} | ||
|
||
def handle_translate(bot, event): | ||
if not event.rest: | ||
event.missing("<from> <to> <text>") | ||
return | ||
query = parse_pair(event.rest.strip()) | ||
if not query: | ||
event.missing("<from> <to> <text>") | ||
return | ||
# event.reply(URL % query) | ||
rawresult = {} | ||
try: | ||
rawresult = getjson().loads(geturl2(URL % query)) | ||
except: | ||
event.reply("Query to Google failed") | ||
return | ||
# debug | ||
# rawresult = {"responseData": {"translatedText":"test"}, "responseDetails": None, "responseStatus": 201} | ||
# logging.warn(URL % query) | ||
# logging.warn(rawresult) | ||
if rawresult['responseStatus'] != 200: | ||
event.reply("Error in the query: ", rawresult) | ||
return | ||
if 'responseData' in rawresult: | ||
if 'translatedText' in rawresult['responseData']: | ||
translation = rawresult['responseData']['translatedText'] | ||
event.reply(translation) | ||
else: | ||
event.reply("No text available") | ||
else: | ||
event.reply("Something is wrong, probably the API changed") | ||
|
||
cmnds.add("translate", handle_translate, ["OPER", "USER", "GUEST"]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
## jsb imports | ||
|
||
from jsb.lib.commands import cmnds | ||
from jsb.utils.url import geturl2, striphtml, decode_html_entities | ||
from jsb.imports import getjson | ||
import logging | ||
|
||
# URL = "http://imdbapi.poromenos.org/js/?name=%s" # for this tweaking is needed, but it doesn't appear to work as adviced. E.g., inception return None | ||
URL = "http://www.deanclatworthy.com/imdb/?q=%s" | ||
|
||
|
||
def handle_imdb(bot, event): | ||
if not event.rest: | ||
event.missing("<query>") | ||
return | ||
query = event.rest.strip() | ||
urlquery = query.replace(" ", "+") | ||
result = {} | ||
rawresult = getjson().loads(geturl2(URL % urlquery)) | ||
|
||
# the API are limited to 30 query per hour, so avoid querying it just for | ||
# testing purposes | ||
|
||
# rawresult = {u'ukscreens': 0, u'rating': u'7.7', u'genres': u'Animation, Drama,Family,Fantasy,Music', u'title': u'Pinocchio', u'series': 0, u'country': u'USA', u'votes': u'23209', u'languages': u'English', u'stv': 0, u'year': None, u'usascreens': 0, u'imdburl': u'http://www.imdb.com/title/tt0032910/'} | ||
if not rawresult: | ||
event.reply("Couldn't look up %s" % query) | ||
return | ||
|
||
if 'error' in rawresult: | ||
event.reply("%s" % rawresult['error']) | ||
return | ||
|
||
for key in rawresult.keys(): | ||
if not rawresult[key]: | ||
result[key] = u"n/a" | ||
else: | ||
result[key] = rawresult[key] | ||
|
||
logging.warn(rawresult, result) | ||
for key in result.keys(): | ||
try: | ||
result[key] = striphtml(decode_html_entities(rawresult[key])) | ||
except AttributeError: | ||
# if the value is not a string, ignore the error and keep going | ||
pass | ||
|
||
event.reply("%(title)s (%(country)s, %(year)s): %(imdburl)s | rating:\ | ||
%(rating)s (out of %(votes)s votes) | Genres %(genres)s | Language: %(languages)s" % result ) | ||
|
||
cmnds.add("imdb", handle_imdb, ["OPER", "USER", "GUEST"]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters