#~DamnVid-module:break

class DamnModule_Break(DamnVideoModule):
	def __init__(self,uri):
		DamnVideoModule.__init__(self,uri)
		self.name='break'
		self.regex={
			'url':re.compile('^https?://.*break\.com/.*$',re.IGNORECASE), # Break URLs seem to be very loose, and so is this regex
			'title':re.compile('="hd_player_title">([^<>"]+)</div>',re.IGNORECASE),
			'ticket':re.compile('sGlobalFileName\s*=\s*[\'"]([^\'"]+)[\'"].*sGlobalContentFilePath\s*=\s*[\'"]([^\'"]+)[\'"]',re.IGNORECASE)
		}
		self.valid=self.regex['url'].search(uri)
		if self.valid:
			self.id='br:'+self.valid.group(0)
			self.link=self.valid.group(0)
	def getTitle(self):
		if self.title is not None:
			return self.title
		html=DamnURLOpen(self.link)
		for i in html:
			res1,res2=self.regex['title'].search(i),self.regex['ticket'].search(i)
			if res1:
				self.title=DamnHtmlEntities(res1.group(1))
			if res2:
				self.newTicket(['http://media1.break.com/dnet/media/'+res2.group(2)+'/'+res2.group(1)+'.wmv','http://media1.break.com/dnet/media/'+res2.group(2)+'/'+res2.group(1)+'.flv'])
		return DamnVideoModule.getTitle(self)
	def renewTicket(self):
		if self.ticket is not None:
			return
		html=DamnURLOpen(self.link)
		for i in html:
			res=self.regex['ticket'].search(i)
			if res:
				self.newTicket(['http://media1.break.com/dnet/media/'+res.group(2)+'/'+res.group(1)+'.wmv','http://media1.break.com/dnet/media/'+res.group(2)+'/'+res.group(1)+'.flv'])
DamnRegisterModule({
	'name':'break',
	'title':'Break.com',
	'type':'video',
	'version':'1.0',
	'author':{
		'name':'Etienne Perot',
		'email':'admin@biringa.com',
		'url':'http://biringa.com/'
	},
	'icon':{
		'small':'break.png',
		'large':'break-large.png',
	},
	'about':{
		'short':'DamnVid module for Break.com.',
		'long':"""This is a video plugin for DamnVid that adds Break.com video downloading capabilities.""",
		'url':'http://code.google.com/p/damnvid/wiki/Modules'
	},
	'sites':[
		{
			'title':'Break.com',
			'icon':'break.png',
			'url':'http://www.break.com/'
		}
	],
	'class':DamnModule_Break,
	'preferences':{
		'profile':{
			'name':'Default profile',
			'type':DV.preference_type_profile,
			'kind':'profile',
			'strict':True,
			'default':3
		},
		'outdir':{
			'name':'Output directory',
			'type':DV.preference_type_misc,
			'kind':'dir',
			'strict':True,
			'default':'?DAMNVID_MY_VIDEOS?/DamnVid/Break/'
		}
	},
	'register':{
		'listicons':{
			'break':'break.png'
		}
	}
})
