#~DamnVid-module:deviantart

class DamnModule_deviantART(DamnVideoModule):
	def __init__(self,uri):
		DamnVideoModule.__init__(self,uri)
		self.name='deviantart'
		self.regex={
			'url':re.compile('^https?://.*?deviantart.com/art/.*',re.IGNORECASE),
			'title':re.compile('<title>\s*([^<>]+?) by [^<>]* on deviantART\s*</title>',re.IGNORECASE),
			'ticket':(
				re.compile('[\'"]?film_divx_film_url[\'"]?\s*:\s*[\'"]([^\'"]+)[\'"]?',re.IGNORECASE),
				re.compile('<a[^<>]*class="b"[^<>]*href="(https?://.*?deviantart.com/download/[^"]+)">',re.IGNORECASE),
				re.compile('[\'"]?\s*deviationid\s*[\'"]?\s*:\s*[\'"]?\s*(\d+)',re.IGNORECASE),
				re.compile('^https?://.*?deviantart.*?[?&]s=([^&?#]+)',re.IGNORECASE),
				re.compile('<flv>\s*([^<>]+?)\s*</flv>',re.IGNORECASE)
			)
		}
		self.valid=self.regex['url'].search(uri)
		self.deviantart_urls=[]
		self.deviantart_id=0
		if self.valid:
			self.id='dv:'+self.valid.group(0)
			self.link=self.valid.group(0)
	def getTitle(self):
		if self.title is None:
			html=DamnURLOpen(self.link)
			for i in html:
				res,res1,res2,res3=self.regex['title'].search(i),self.regex['ticket'][0].search(i),self.regex['ticket'][1].search(i),self.regex['ticket'][2].search(i)
				if res:
					self.title=DamnHtmlEntities(res.group(1))
				if res1:
					self.deviantart_urls.append(DamnHtmlEntities(res1.group(1)))
				if res2:
					self.deviantart_urls.append(DamnHtmlEntities(res2.group(1)))
				if res3:
					self.deviantart_id=int(res3.group(1))
		return DamnVideoModule.getTitle(self)
	def renewTicket(self):
		if self.ticket is not None:
			return
		if self.deviantart_id:
			loc=DamnURLOpen('http://www.deviantart.com/film/'+str(self.deviantart_id)+'/1/').geturl()
			res=self.regex['ticket'][3].search(loc)
			if res:
				html=DamnURLOpen('http://deviantart.com/global/difi/?c[]=Film;metadata;'+str(self.deviantart_id)+','+res.group(1)+'&t=xml')
				for i in html:
					res=self.regex['ticket'][4].search(i)
					if res:
						self.deviantart_urls.append(DamnHtmlEntities(res.group(1)))
		self.newTicket(DamnURLPickerBySize(self.deviantart_urls,array=True))
DamnRegisterModule({
	'name':'deviantart',
	'title':'deviantART',
	'type':'video',
	'version':'1.0',
	'author':{
		'name':'Etienne Perot',
		'email':'admin@biringa.com',
		'url':'http://biringa.com/'
	},
	'icon':{
		'small':'deviantart.png',
		'large':'deviantart-large.png',
	},
	'about':{
		'short':'DamnVid module for deviantART.',
		'long':"""This is a video plugin for DamnVid that adds video downloading capabilities from deviantART (only from the Film & Animation category).""",
		'url':'http://code.google.com/p/damnvid/wiki/Modules'
	},
	'sites':[
		{
			'title':'deviantART',
			'icon':'deviantart.png',
			'url':'http://browse.deviantart.com/film/'
		}
	],
	'class':DamnModule_deviantART,
	'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/deviantART/'
		}
	},
	'register':{
		'listicons':{
			'deviantart':'deviantart.png'
		}
	}
})
