Newer
Older
# 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.
import urllib.request, urllib.parse, urllib.error
import socket
import json
@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"]
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)
sock.close()
try:
e = json.loads(data)["entry"]
except:
self.server.doMessage(channel, user+": No data found.")
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"],
e["author"][0]["name"]["$t"],
h,m,s,
e['gd$rating']['average'],
e["yt$statistics"]["favoriteCount"],
e["yt$statistics"]["viewCount"],
e["published"]["$t"].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"]),
)
)