XChat Youtube Link Information

Grabs data using google’s gdata api and displays information about You tube links

Dependencies: python-gdata – Google Data Python client library

Ubuntu: sudo apt-get install python-gdata

import re
import gdata.youtube
import gdata.youtube.service

__module_name__ = "YouTube Links Info" 
__module_version__ = "1.0" 
__module_description__ = "Displays information about a youtube link"


def parse_youtube(link):


	validate = re.compile(r'((?:http://)?(?:\w+\.)?youtube\.com/(?:(?:v/)|(?:(?:watch(?:\.php)?)?\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$')
	valid = validate.search(link)
	if valid is None:
		return [None]
	
	yt_service = gdata.youtube.service.YouTubeService()

	youtube_id = valid.group(2)
	
	try:
		entry = yt_service.GetYouTubeVideoEntry(video_id=youtube_id)
		PrintEntryDetails(entry)
	except:
		return youtube_id

	return youtube_id

def check_link(word,word_eol,userdata):

	if "youtube.com" in word_eol[3]:
		id = parse_youtube(word_eol[3])

	return xchat.EAT_NONE
	
def unload_cb(userdata):
  	xchat.prnt('%s version %s unloaded.' % (__module_name__ , __module_version__)) 
	return xchat.EAT_ALL

def truncate(s, width):

	if len(s) <= 120:
		return s	
	try:
		if s[width].isspace():
			return s[0:width];
		else:
			return s[0:width].rsplit(None, 1)[0]
	except:
		return s

def PrintEntryDetails(entry):

	color1 = "\0032"
	color2 = "\0033"
	color3 = "\0034"
	nocolor = "\003"
 
	print '%s[YOUTUBE Link Info]%s' % (color2,nocolor)
	print '%s[TITLE]:%s %s %s [RATING]: %s %s %s [VIEWS]: %s %s' % (color1, nocolor, entry.media.title.text, color3, nocolor, entry.rating.average, color1, nocolor, entry.statistics.view_count)
	#print 'Video published on: %s ' % entry.published.text
	vid_desc = entry.media.description.text.strip()
	if not vid_desc:
		return

	if "\n" in vid_desc: # merge lines together
		vid_desc = truncate(entry.media.description.text.strip(), 120)
		if vid_desc:
			vid_desc_lines = vid_desc.split('\n')
			vid_desc = " ".join(vid_desc_lines)

	print '%s[DESCRIPTION]%s %s' % (color3,nocolor,vid_desc)

	return

xchat.hook_server("PRIVMSG", check_link)
xchat.hook_unload(unload_cb)
More Reading