#~DamnVid-module:clubic

class DamnModule_Clubic(DamnVideoModule):
	def __init__(self,uri,parent=None):
		DamnVideoModule.__init__(self,uri)
		self.name='clubic'
		self.regex={
			'url':re.compile('^https?://(?:[-_\w]+\.)*(clubic\.com|mobinaute\.com|neteco\.com|jeuxvideo\.(?:fr|tv))/(.+)$',re.IGNORECASE),
			'title':(re.compile('<title>\s*(?:\[?Vid(?:.|&eacute;)o\]?)?\s*([^<>]+?)\s*(?:\(?Jeuxvideo.TV\)?)?\s*</title>',re.IGNORECASE),self.regex['title']),
			'ticket':(re.compile('https?://(?:[-_\w]+\.)*(clubic\.com|mobinaute\.com|neteco\.com|jeuxvideo\.(?:fr|tv))/v/(\w+)',re.IGNORECASE),re.compile('/api/tv/xml\.php\?id=(\w+)',re.IGNORECASE),re.compile('https?://(?:[-_\w]+\.)*\w+/[^"\';<>#\s]+',re.IGNORECASE),re.compile('<location>\s*(https?://.+)\s*</location>',re.IGNORECASE))
		}
		self.valid=self.regex['url'].search(uri)
		self.clubic_videoids=[]
		self.clubic_externalvideos=[]
		self.clubic_sistersite='clubic'
		self.clubic_uniquevideo=None
		if self.valid:
			self.id='cl:'+self.valid.group(1)+'@'+self.valid.group(2)
			self.clubic_sistersite=self.valid.group(1)
			self.link='http://www.'+self.clubic_sistersite+'/'+self.valid.group(2)
			if self.valid.group(2)[:2]!='v/':
				self.clubic_uniquevideo=False
			else:
				self.clubic_uniquevideo=True
				self.clubic_id=self.valid.group(2)[2:]
				self.title=self.getStorage()['titles'][self.clubic_sistersite+'@'+self.clubic_id]
	def getTitle(self):
		if self.title is None:
			if self.clubic_uniquevideo:
				return DamnVideoModule.getTitle(self)
			http=DamnURLOpen(self.link)
			html=''
			for i in http:
				res=self.regex['title'][0].search(i)
				if res:
					self.title=DamnHtmlEntities(res.group(1).replace('\n','').replace('\r',''))
				else:
					res=self.regex['title'][1].search(i)
					if res:
						self.title=DamnHtmlEntities(res.group(1).replace('\n','').replace('\r',''))
				res2,res3=self.regex['ticket'][0].search(i),self.regex['ticket'][1].search(i)
				if res2:
					subid=(res2.group(1),res2.group(2))
				if res3:
					subid=(self.clubic_sistersite,res3.group(1))
				if res2 or res3:
					if subid not in self.clubic_videoids:
						self.clubic_videoids.append(subid)
				"""else:
					res3=self.regex['ticket'][2].search(i)
					if res3:
						try:
							mod = DamnGetAlternateModule(res3.group(0))
							if mod is not None:
								self.clubic_externalvideos.append(mod)
						except:
							pass"""
		return DamnVideoModule.getTitle(self)
	def renewTicket(self):
		self.getTitle()
		if not self.clubic_uniquevideo:
			return
		if self.ticket is None:
			html=DamnURLOpen('http://www.'+self.clubic_sistersite+'/api/tv/xml.php?id='+self.clubic_id)
			for i in html:
				res=self.regex['ticket'][3].search(i)
				if res:
					self.newTicket(res.group(1))
	def getDownload(self):
		if not self.clubic_uniquevideo:
			return None
		self.renewTicket()
		return self.ticket
	def addVid(self,parent):
		self.getTitle()
		if self.clubic_uniquevideo:
			return DamnVideoModule.addVid(self,parent)
		counter=0
		count=u''
		self.getStorage()['titles']={}
		for i in self.clubic_videoids:
			counter+=1
			if len(self.clubic_videoids)>1:
				count=u' ('+DamnUnicode(counter)+u')'
			if True:
				self.getStorage()['titles'][i[0]+'@'+i[1]]=DamnUnicode(self.title)+count
				self.__class__('http://www.'+i[0]+'/v/'+i[1]).addVid(parent)
			else:
				pass
		for i in self.clubic_externalvideos:
			i.addVid(parent)
DamnRegisterModule({
	'name':'clubic',
	'title':'Clubic',
	'type':'video',
	'version':'1.0',
	'author':{
		'name':'Etienne Perot',
		'email':'admin@biringa.com',
		'url':'http://biringa.com/'
	},
	'icon':{
		'small':'clubic.png',
		'large':'clubic-large.png',
	},
	'about':{
		'short':'DamnVid module for Clubic.',
		'long':"""This is a video plugin for DamnVid that adds Clubic video downloading capabilities.
Also works with Clubic's sister sites: Neteco, Mobinaute, JeuxVideo.fr, and JeuxVideo.TV.""",
		'url':'http://code.google.com/p/damnvid/wiki/Modules'
	},
	'sites':[
		{
			'title':'Clubic',
			'icon':'clubic.png',
			'url':'http://www.clubic.com/'
		}
	],
	'class':DamnModule_Clubic,
	'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/Clubic/'
		},
		'fetchexternal':{
			'name':'Fetch videos from external sites',
			'type':DV.preference_type_misc,
			'kind':'bool',
			'strict':True,
			'align':True,
			'default':True
		}
	},
	'preferences_order':['profile','outdir'],
	'register':{
		'listicons':{
			'clubic':'clubic.png'
		}
	}
})
