Merge pull request #57 from Cccompany/v3.3

IMDB module (fixes  #11) + clean conjugaison module
This commit is contained in:
nemunaire 2014-07-23 15:13:04 +02:00
commit 0036dcc285
2 changed files with 111 additions and 22 deletions

View File

@ -7,9 +7,26 @@ from urllib.parse import quote
from tools import web from tools import web
from tools.web import striphtml from tools.web import striphtml
from collections import defaultdict
nemubotversion = 3.3 nemubotversion = 3.3
s = [('present', '0'), ('présent', '0'), ('pr', '0'),
('passé simple', '12'), ('passe simple', '12'), ('ps', '12'),
('passé antérieur', '112'), ('passe anterieur', '112'), ('pa', '112'),
('passé composé', '100'), ('passe compose', '100'), ('pc', '100'),
('futur', '18'), ('f', '18'),
('futur antérieur', '118'), ('futur anterieur', '118'), ('fa', '118'),
('subjonctif présent', '24'), ('subjonctif present', '24'), ('spr', '24'),
('subjonctif passé', '124'), ('subjonctif passe', '124'), ('spa', '124'),
('plus que parfait', '106'), ('pqp', '106'),
('imparfait', '6'), ('ii', '6')]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
def help_tiny (): def help_tiny ():
return "Find french conjugaison" return "Find french conjugaison"
@ -26,8 +43,16 @@ def cmd_conjug(msg):
return Response(msg.sender, return Response(msg.sender,
"Demande incorrecte.\n %s" % help_full(), "Demande incorrecte.\n %s" % help_full(),
msg.channel) msg.channel)
tens = msg.cmds[1] tens = msg.cmds[1]
verb = msg.cmds[2]
for i in range(2, len(msg.cmds) - 1):
tens += " " + msg.cmds[i]
print_debug(tens)
verb = msg.cmds[len(msg.cmds) - 1]
try: try:
conjug = get_conjug(verb, tens) conjug = get_conjug(verb, tens)
except: except:
@ -63,36 +88,24 @@ def get_conjug(verb, stringTens):
def compute_line(line, stringTens): def compute_line(line, stringTens):
res = list() res = list()
idTemps = get_conjug_for_tens(stringTens) try:
idTemps = d[stringTens]
except:
res.append("Le temps demandé n'existe pas")
return res
if idTemps is None: if len(idTemps) == 0:
return Response(msg.sender, res.append("Le temps demandé n'existe pas")
"Le temps que vous avez spécifiez n'existe pas", msg.channel) return res
index = line.index('<div id="temps' + idTemps + '\"') index = line.index('<div id="temps' + idTemps[0] + '\"')
endIndex = line[index:].index('<div class=\"conjugBloc\"') endIndex = line[index:].index('<div class=\"conjugBloc\"')
endIndex += index endIndex += index
newLine = line[index:endIndex] newLine = line[index:endIndex]
for elt in re.finditer("[p|/]>([^/]*/b>)", newLine): for elt in re.finditer("[p|/]>([^/]*/b>)", newLine):
# res.append(strip_tags(elt.group(1)))
res.append(striphtml(elt.group(1))) res.append(striphtml(elt.group(1)))
return res return res
def get_conjug_for_tens(stringTens):
dic = {'pr' : '0',
'ps' : '12',
'pa' : '112',
'pc' : '100',
'f' : '18',
'fa' : '118',
'spr' : '24',
'spa' : '124',
'ii' : '6',
'pqp' : '106'}
return dic[stringTens]

76
modules/imdb.py Normal file
View File

@ -0,0 +1,76 @@
# coding=utf-8
import urllib.request
import json
nemubotversion = 3.3
def help_tiny ():
return "Find info about a movie"
def help_full ():
return "!imdb <film>"
def load(context):
from hooks import Hook
add_hook("cmd_hook", Hook(cmd_imdb, "imdb"))
add_hook("cmd_hook", Hook(cmd_search, "imdbs"))
def cmd_imdb(msg):
if (len(msg.cmds) < 2):
return Response(msg.sender,
"Demande incorrecte.\n %s" % help_full(),
msg.channel)
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
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'])
return res
def cmd_search(msg):
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/?s=" + movie_name
print_debug(url)
raw = urllib.request.urlopen(url)
data = json.loads(raw.read().decode())
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