Nerds on Linux

November 4, 2010

XChat Youtube Link Information

Filed under: — karl @ 6:02 pm

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import xchat
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)

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress