Last PR modules: made some stabilization modifications

This commit is contained in:
nemunaire 2014-07-25 12:53:07 +02:00
parent d575c0d6d3
commit 9b010544b5
3 changed files with 62 additions and 95 deletions

View File

@ -40,63 +40,42 @@ def load(context):
def cmd_conjug(msg):
if len(msg.cmds) < 3:
return Response(msg.sender,
"Demande incorrecte.\n %s" % help_full(),
msg.channel)
tens = msg.cmds[1]
for i in range(2, len(msg.cmds) - 1):
tens += " " + msg.cmds[i]
raise IRCException("donne moi un temps et un verbe, et je te donnerai sa conjugaison!")
tens = ' '.join(msg.cmds[1:-1])
print_debug(tens)
verb = msg.cmds[len(msg.cmds) - 1]
verb = msg.cmds[-1]
try:
conjug = get_conjug(verb, tens)
except:
conjug = None
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value,
exc_traceback)
conjug = get_conjug(verb, tens)
if conjug is None:
return Response(msg.sender,
"Une erreur s'est produite durant la recherche"
" du verbe %s" % verb, msg.channel)
elif len(conjug) > 0:
return Response(msg.sender, conjug, msg.channel,
if len(conjug) > 0:
return Response(msg.sender, conjug, channel=msg.channel,
title="Conjugaison de %s" % verb)
else:
return Response(msg.sender,
"Aucune conjugaison de %s n'a été trouvé" % verb,
msg.channel)
return False
raise IRCException("aucune conjugaison de '%s' n'a été trouvé" % verb)
def get_conjug(verb, stringTens):
url = "http://leconjugueur.lefigaro.fr/conjugaison/verbe/" + quote(verb.encode("ISO-8859-1")) + ".html"
url = ("http://leconjugueur.lefigaro.fr/conjugaison/verbe/%s.html" %
quote(verb.encode("ISO-8859-1")))
print_debug (url)
page = web.getURLContent(url)
if page is not None:
for line in page.split("\n"):
if re.search('<div class="modeBloc">', line) is not None:
return compute_line(line, stringTens)
else:
return None
return list()
def compute_line(line, stringTens):
res = list()
try:
idTemps = d[stringTens]
idTemps = d[stringTens]
except:
res.append("Le temps demandé n'existe pas")
return res
raise IRCException("le temps demandé n'existe pas")
if len(idTemps) == 0:
res.append("Le temps demandé n'existe pas")
return res
raise IRCException("le temps demandé n'existe pas")
index = line.index('<div id="temps' + idTemps[0] + '\"')
endIndex = line[index:].index('<div class=\"conjugBloc\"')
@ -104,8 +83,7 @@ def compute_line(line, stringTens):
endIndex += index
newLine = line[index:endIndex]
res = list()
for elt in re.finditer("[p|/]>([^/]*/b>)", newLine):
res.append(striphtml(elt.group(1)))
res.append(striphtml(elt.group(1).replace("<b>", "\x02").replace("</b>", "\x0F")))
return res

View File

@ -6,10 +6,10 @@ import json
nemubotversion = 3.3
def help_tiny ():
return "Find info about a movie"
return "Show many information about a movie or serie"
def help_full ():
return "!imdb <film>"
return "Search a movie title with: !imdbs <approximative title> ; View movie details with !imdb <title>"
def load(context):
from hooks import Hook
@ -18,59 +18,51 @@ def load(context):
def cmd_imdb(msg):
if (len(msg.cmds) < 2):
return Response(msg.sender,
"Demande incorrecte.\n %s" % help_full(),
msg.channel)
if len(msg.cmds) < 2:
raise IRCException("precise a movie/serie title!")
movie_name = msg.cmds[1]
for x in range(2, len(msg.cmds)):
movie_name += urllib.parse.quote(' ') + urllib.parse.quote(msg.cmds[x])
url = "http://www.omdbapi.com/?t=" + movie_name
url = "http://www.omdbapi.com/?t=%s" % urllib.parse.quote(' '.join(msg.cmds[1:]))
print_debug(url)
response = urllib.request.urlopen(url)
data = json.loads(response.read().decode())
string = "\x02IMDB Rating\x0F: " + data['imdbRating'] + "\n\x02Plot\x0F: " + data['Plot']
res = Response(msg.sender,
string,
msg.channel)
res.append_message("\x02Released\x0F: " + data['Released']
+ " \x02Type\x0F: " + data['Type']
+ " \x02Genre\x0F: " + data['Genre']
+ " \x02Director\x0F: " + data['Director']
+ " \x02Writer\x0F: " + data['Writer']
+ " \x02Actors\x0F: " + data['Actors']
+ " \x02Country\x0F: " + data['Country'])
if "Error" in data:
raise IRCException(data["Error"])
elif "Response" in data and data["Response"] == "True":
res = Response(msg.sender, channel=msg.channel,
title="%s (%s)" % (data['Title'], data['Year']),
nomore="No more information, more at http://www.imdb.com/title/%s" % data['imdbID'])
res.append_message("\x02rating\x0F: %s (%s votes); \x02plot\x0F: %s" %
(data['imdbRating'], data['imdbVotes'], data['Plot']))
res.append_message("%s \x02from\x0F %s \x02released on\x0F %s; \x02genre:\x0F %s; \x02directed by:\x0F %s; \x02writed by:\x0F %s; \x02main actors:\x0F %s"
% (data['Type'], data['Country'], data['Released'], data['Genre'], data['Director'], data['Writer'], data['Actors']))
return res
else:
raise IRCException("An error occurs during movie search")
return res
def cmd_search(msg):
url = "http://www.omdbapi.com/?s=%s" % urllib.parse.quote(' '.join(msg.cmds[1:]))
print_debug(url)
movie_name = msg.cmds[1]
raw = urllib.request.urlopen(url)
data = json.loads(raw.read().decode())
for x in range(2, len(msg.cmds)):
movie_name += urllib.parse.quote(' ') + urllib.parse.quote(msg.cmds[x])
if "Error" in data:
raise IRCException(data["Error"])
url = "http://www.omdbapi.com/?s=" + movie_name
print_debug(url)
elif "Search" in data:
movies = list()
raw = urllib.request.urlopen(url)
data = json.loads(raw.read().decode())
for m in data['Search']:
movies.append("\x02%s\x0F (%s of %s)" % (m['Title'], m['Type'], m['Year']))
search = data['Search']
movie_list = ""
for i in range(0, len(search)):
movie_list += "\x02Title\x0F: " + search[i]['Title']
movie_list += " \x02Year\x0F: " + search[i]['Year']
movie_list += " \x02Type\x0F:" + search[i]['Type']
movie_list += " |--| "
res = Response(msg.sender, movie_list, msg.channel)
return res
return Response(msg.sender, movies, title="Titles found", channel=msg.channel)
else:
raise IRCException("An error occurs during movie search")

View File

@ -20,23 +20,20 @@ def load(context):
def cmd_tcode(msg):
if (len(msg.cmds) < 2):
return Response(msg.sender,
"Demande incorrecte.\n %s" % help_full(),
msg.channel)
if len(msg.cmds) != 2:
raise IRCException("indicate a transaction code or a keyword to search!")
res = Response(msg.sender, None, msg.channel)
request = urllib.parse.quote(msg.cmds[1])
url = "http://www.tcodesearch.com/tcodes/search?q=" + request
url = "http://www.tcodesearch.com/tcodes/search?q=%s" % urllib.parse.quote(msg.cmds[1])
page = web.getURLContent(url)
res = Response(msg.sender, channel=msg.channel,
nomore="No more transaction code", count=" (%d more tcodes)")
if page is not None:
index = page.index('<div id="searchresults">') + len('<div id="searchresults">')
end = page[index:].index('</div>')+index
strscope = page[index:end]
for tcode in re.finditer('<strong> ([a-zA-Z0-9_]*)</strong> - ([^\n]*)\n', strscope):
res.append_message("\x02" + tcode.group(1)+"\x0F - "+striphtml(tcode.group(2)))
return res
else:
return None
res.append_message("\x02%s\x0F - %s" % (tcode.group(1), striphtml(tcode.group(2))))
return res