From b40ea545e0832bf10543e77066893b0bbd07ae8a Mon Sep 17 00:00:00 2001 From: aLaserShark <a.laser.sharky@gmail.com> Date: Mon, 2 Sep 2013 08:21:30 -0400 Subject: [PATCH] fix google plugin and a few pep 8 violations Signed-off-by: gry <gryllida@gmail.com> --- plugins/google.py | 2 +- plugins/isitup.py | 6 ++++-- plugins/yt.py | 40 ++++++++++++++++++++++++---------------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/plugins/google.py b/plugins/google.py index 4333cc9..7bfd1f1 100644 --- a/plugins/google.py +++ b/plugins/google.py @@ -36,6 +36,6 @@ class Google(object): if cmd == "google" or cmd == "g": search = " ".join(args) req = urllib.request.Request("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + urllib.parse.quote(search) + "&key=ABQIAAAA4B16PYoznYWgfVLfNDV4fxRsamdul3hUHNYXnxki2eGK76NS_RQ795CTZZ3l-TuRCO2d5eibFI1WZA") - data = str(urllib.request.urlopen(req).read()) + data = urllib.request.urlopen(req).read().decode('utf-8') splitdata = data.split("\"") self.server.doMessage(channel, splitdata[31] + " - " + splitdata[11]) diff --git a/plugins/isitup.py b/plugins/isitup.py index b4c8a5b..05d3987 100644 --- a/plugins/isitup.py +++ b/plugins/isitup.py @@ -42,12 +42,14 @@ class IsItUp(object): url = args[0][8:] else: url = args[0] - code = "".join(x.decode('utf8') for x in urllib.request.urlopen("http://isitup.org/" + url + ".json").readlines()) + code = "".join(x.decode('utf8') for x in urllib.request.urlopen("http://isitup.org/" + + url + ".json").readlines()) #code = code.decode('utf8') print(code) arr = json.loads(code) arr['up'] = 'up' if arr['status_code'] == 1 else 'not up' if arr['up'] == 'up': - self.server.doMessage(channel, user + ': %(domain)s:%(port)s is %(up)s. Got HTTP status code %(response_code)03d from %(response_ip)s in %(response_time)f ms.' % arr) + self.server.doMessage(channel, user + ': %(domain)s:%(port)s is %(up)s. Got HTTP status code %(response_' + 'code)03d from %(response_ip)s in %(response_time)f ms.' % arr) else: self.server.doMessage(channel, user + ': %s is not up.' % arr['domain']) diff --git a/plugins/yt.py b/plugins/yt.py index 5a65d0d..8179f4b 100644 --- a/plugins/yt.py +++ b/plugins/yt.py @@ -29,6 +29,7 @@ import re @plugin class yt(object): """`yt <id here>` to see a Youtube.com video information (title, ups, down, time, &c).""" + def __init__(self, server): self.server = server self.commands = ["yt"] @@ -37,25 +38,32 @@ class yt(object): def handle_command(self, channel, user, cmd, args): if cmd == "yt": if len(args) < 1: - self.server.doMessage(channel, user + ": Return http://youtube.com video information by URL or id. Syntax: yt <video url or id>.") + self.server.doMessage( + channel, user + ": Return http://youtube.com video information by URL or id. " + "Syntax: yt <video url or id>.") return self.yt(channel, user, cmd, args) + def yt(self, channel, user, cmd, args): - request = re.sub(r"(?i)(?:https?://)?(?:www.)?(?:youtube.com/watch\?v=|youtu\.be/)([\w-]+)(?:[?&].*)?", r"\1", " ".join(args)) - print("Request is `" + request + "`.") - sock = urllib.request.urlopen("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json" % request) - data = sock.read().decode("utf-8") - sock.close() - try: - e = json.loads(data)["entry"] - except: - self.server.doMessage(channel, user + ": No data found.") - return + request = re.sub(r"(?i)(?:https?://)?(?:www.)?(?:youtube.com/watch\?v=|youtu\.be/)([\w-]+)(?:[?&].*)?", r"\1", + " ".join(args)) + print("Request is `" + request + "`.") + sock = urllib.request.urlopen("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json" % request) + data = sock.read().decode("utf-8") + sock.close() + try: e = json.loads(data)["entry"] - h = int(e["media$group"]["yt$duration"]["seconds"]) // 3600 - m = int(e["media$group"]["yt$duration"]["seconds"]) // 60 - h * 60 - s = int(e["media$group"]["yt$duration"]["seconds"]) - h * 3600 - m * 60 - self.server.doMessage(channel, user + ": " + "'{0}' by {1} ({2}:{3}:{4}) - Rating {5} - Favorite {6}/{7} - Date {8} - Likes {9}+ {10}- /{11} http://youtu.be/{12}".format( + except: + self.server.doMessage(channel, user + ": No data found.") + return + e = json.loads(data)["entry"] + h = int(e["media$group"]["yt$duration"]["seconds"]) // 3600 + m = int(e["media$group"]["yt$duration"]["seconds"]) // 60 - h * 60 + s = int(e["media$group"]["yt$duration"]["seconds"]) - h * 3600 - m * 60 + # pycharm says that the following line is a big PEP 8 violation, + self.server.doMessage(channel, user + ": " + "'{0}' by {1} ({2}:{3}:{4}) - " + "Rating {5} - Favorite {6}/{7} - Date {8} - " + "Likes {9}+ {10}- /{11} http://youtu.be/{12}".format( e["title"]["$t"], e["author"][0]["name"]["$t"], h, m, s, @@ -69,4 +77,4 @@ class yt(object): int(e["yt$rating"]["numLikes"]) - int(json.loads(data)["entry"]["yt$rating"]["numDislikes"]), request ) - ) + ) -- GitLab