<commit_before>import re
from urllib.request import urlopen
from xml.etree.ElementTree import ElementTree
from xml.parsers.expat import ExpatError
from lib.bot import Bot
from lib.events import subscribe


ATOM = 'http://www.w3.org/2005/Atom'


@subscribe('channel_message')
def display_youtube_video_title(sender, channel, message):
	""" Checks every message sent for a youtube link and displays title and uploader information """
	client = Bot().client
	results = re.finditer(r'https?://(?:(?:www\.)?youtube\.com/watch\?.*?v=|youtu\.be/)(?P<id>[-_\w]{11})', message) 
	output = []
	for result in results:
		handle = None
		try:
			handle = urlopen('http://gdata.youtube.com/feeds/api/videos/{}?v=2'.format(result.group('id')))
		except Exception as error:
			client.say(channel, error)
		info = ElementTree()
		try:
			info.parse(handle)
		except ExpatError:
			client.say(channel, 'Error: Malformed XML.')
		handle.close()
		title    = info.findtext('{%s}title' % ATOM)
		uploader = info.findtext('{%s}author/{%s}name' % (ATOM, ATOM)) 
		if title != None and uploader != None:
			output.append("'{}' uploaded by {}".format(title, uploader))
	if output:
		# send message back to channel
		client.say(channel, '\n'.join(output))
	
<commit_msg>Youtube module reads out the video duration now<commit_after>import re
from urllib.request import urlopen
from xml.etree.ElementTree import ElementTree
from xml.parsers.expat import ExpatError
from lib.bot import Bot
from lib.events import subscribe


ATOM = 'http://www.w3.org/2005/Atom'
MRSS = 'http://search.yahoo.com/mrss/'


@subscribe('channel_message')
def display_youtube_video_title(sender, channel, message):
	""" Checks every message sent for a youtube link and displays title and uploader information """
	client = Bot().client
	results = re.finditer(r'https?://(?:(?:www\.)?youtube\.com/watch\?.*?v=|youtu\.be/)(?P<id>[-_\w]{11})', message) 
	output = []
	for result in results:
		handle = None
		try:
			handle = urlopen('http://gdata.youtube.com/feeds/api/videos/{}?v=2'.format(result.group('id')))
		except Exception as error:
			client.say(channel, error)
		info = ElementTree()
		try:
			info.parse(handle)
		except ExpatError:
			client.say(channel, 'Error: Malformed XML.')
		handle.close()
		title    = info.findtext('{{{0}}}title'.format(ATOM))
		uploader = info.findtext('{{{0}}}author/{{{0}}}name'.format(ATOM)) 
		duration = int(info.find('{{{0}}}group/{{{0}}}content'.format(MRSS)).get('duration', '0'))
		hours = duration // 3600
		minutes = (duration - hours * 3600) // 60
		seconds = duration - hours * 3600 - minutes * 60
		if title != None and uploader != None:
			output.append('{0} [{1:02d}:{2:02d}:{3:02d}] (by {4})'.format(
				title, hours, minutes, seconds, uploader
			))
	if output:
		# send message back to channel
		client.say(channel, '\n'.join(output))
	

{'fraction_non_alphanumeric': 0.17065446868402534, 'fraction_numerical': 0.017241379310344827, 'mean_word_length': 4.384469696969697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2926204', 'n_tokens_mistral': 1066, 'n_tokens_neox': 962, 'n_words': 231}