# guppy Copyright (C) 2010-2011 guppy team members. # # This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. # This is free software, and you are welcome to redistribute it # under certain conditions; type `show c' for details. # yt module # --gry import urllib.request, urllib.parse, urllib.error import socket import json @plugin class yt(object): def __init__(self, server): self.server = server self.commands = ["yt"] self.server.handle("command", self.handle_command, self.commands) 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>.") return request = " ".join(args).replace(" ","+").replace("http://www.youtube.com/watch?v=","") if request.find("&")!=-1: request = request.split("&")[0] sock = urllib.request.urlopen("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json"%request) data = sock.read() sock.close() try: e = json.loads(data)["entry"] 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 self.server.doMessage(channel, user+": "+"'{0}' by {1} ({2}:{3}:{4}) - Rating {5} - Favorite {6}/{7} - Date {8} - Likes {9}+ {10}- /{11}".format( e["title"]["$t"].encode('utf-8'), e["author"][0]["name"]["$t"].encode('utf-8'), h,m,s, e['gd$rating']['average'], e["yt$statistics"]["favoriteCount"], e["yt$statistics"]["viewCount"], e["published"]["$t"].encode('utf-8').split('.')[0].split("T")[0], #e["link"][0]["href"].encode('utf-8').replace("&feature=youtube_gdata",""), e["yt$rating"]["numLikes"], e["yt$rating"]["numDislikes"], int(e["yt$rating"]["numLikes"]) - int(json.loads(data)["entry"]["yt$rating"]["numDislikes"]), ) )