#~DamnVid-module:youtube

class DamnModule_ITMSPodcasts(DamnVideoModule):
	def __init__(self,uri,parent=None):
		DamnVideoModule.__init__(self,uri)
		self.name='itmspodcasts'
		self.regex={
			'url':re.compile('^(?:https?|itms)://(?:[-_\w]+\.)+apple\.com.*(Feed/.+$|viewPodcast\?.*?id=(\d+))',re.IGNORECASE),
			'title':(re.compile('<key>itemName</key>\s*<string>\s*([^<>]+)\s*</string>',re.IGNORECASE),re.compile('<title>\s*([^<>]+?)\s*</title>',re.IGNORECASE)),
			'ticket':(re.compile('<key>episodeURL</key>\s*<string>\s*([^<>]+)\s*</string>',re.IGNORECASE),re.compile('<enclosure[^<>]+url="([^"]+)"',re.IGNORECASE))
		}
		self.playlist=False
		self.valid=self.regex['url'].search(uri)
		self.itmspodcasts_itunesu=None
		if self.valid:
			self.playlist=True
			self.id='it:'+self.valid.group(1)
			if self.valid.group(1)[:5].lower()=='feed/':
				self.link=uri
			else:
				self.link='http://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id='+self.valid.group(1)
			self.itmspodcasts_itunesu=uri.find('.edu')!=-1
		else:
			self.id='ie:'+uri
			self.link=uri
	def ITMSPodcasts_setTitle(self,title):
		self.title=title
		return self # Return self for chainability
	def ITMSPodcasts_setiTunesU(self,itunesu):
		self.itmspodcasts_itunesu=itunesu
		return self # Return self for chainability
	def getTitle(self):
		if self.playlist:
			return None
		return DamnVideoModule.getTitle(self)
	def getProfile(self):
		if self.playlist:
			return None
		if self.itmspodcasts_itunesu:
			return self.pref('profileu')
		return self.pref('profile')
	def getOutdir(self):
		if self.playlist:
			return None
		if self.itmspodcasts_itunesu:
			return self.pref('outdiru')
		return self.pref('outdir')
	def getIcon(self):
		if self.playlist:
			return None
		if self.itmspodcasts_itunesu:
			return DamnGetListIcon('itmspodcasts-itunesu')
		return DamnGetListIcon('itmspodcasts')
	def renewTicket(self):
		return
	def getDownload(self):
		if self.playlist:
			return None
		return self.link
	def addVid(self,parent):
		if not self.playlist:
			return DamnVideoModule.addVid(self,parent)
		html=DamnURLOpen(self.link)
		count=0
		if self.itmspodcasts_itunesu:
			maxcount=int(self.pref('maxepisodesu'))
		else:
			maxcount=int(self.pref('maxepisodes'))
		lasturl=None
		for i in html:
			res1,res2=self.regex['ticket'][0].search(i),self.regex['title'][1].search(i)
			if res1:
				lasturl=DamnHtmlEntities(res1.group(1))
			elif res2:
				lasturl=DamnHtmlEntities(res2.group(1))
			elif lasturl is not None and count<=maxcount:
				res1,res2=self.regex['title'][0].search(i),self.regex['ticket'][1].search(i)
				# Allright yeah, 'lasturl' and 'title' are reversed in iTunes U mode, lol
				if res1:
					title=DamnHtmlEntities(res1.group(1))
					self.__class__(lasturl).ITMSPodcasts_setTitle(title).ITMSPodcasts_setiTunesU(self.itmspodcasts_itunesu).addVid(parent)
				elif res2:
					title=DamnHtmlEntities(res2.group(1))
					self.__class__(title).ITMSPodcasts_setTitle(lasturl).ITMSPodcasts_setiTunesU(self.itmspodcasts_itunesu).addVid(parent)
				if res1 or res2:
					lasturl=None
					count+=1
					if count>=maxcount:
						break
DamnRegisterModule({
	'name':'itmspodcasts',
	'title':'iTunes Podcasts / iTunes U',
	'type':'video',
	'version':'1.0',
	'author':{
		'name':'Etienne Perot',
		'email':'admin@biringa.com',
		'url':'http://biringa.com/'
	},
	'icon':{
		'small':'itmspodcasts.png',
		'large':'itmspodcasts-large.png',
	},
	'about':{
		'short':'DamnVid module for iTunes Podcasts and iTunes U.',
		'long':"""This is a video plugin for DamnVid that adds video podcasts downloading capabilities from the iTunes Music Store.
Also works with iTunes U.""",
		'url':'http://code.google.com/p/damnvid/wiki/Modules'
	},
	'sites':[
		{
			'title':'iTunes Podcasts',
			'icon':'itmspodcasts.png',
			'url':'http://www.apple.com/itunes/#podcasts'
		},
		{
			'title':'iTunes U',
			'icon':'itmspodcasts-itunesu.png',
			'url':'http://www.apple.com/education/mobile-learning/'
		}
	],
	'class':DamnModule_ITMSPodcasts,
	'preferences':{
		'profile':{
			'name':'Default profile for podcasts',
			'type':DV.preference_type_profile,
			'kind':'profile',
			'strict':True,
			'default':-1
		},
		'profileu':{
			'name':'Default profile for iTunes U',
			'type':DV.preference_type_profile,
			'kind':'profileu',
			'strict':True,
			'default':-1
		},
		'outdir':{
			'name':'Output directory for podcasts',
			'type':DV.preference_type_misc,
			'kind':'dir',
			'strict':True,
			'default':'?DAMNVID_MY_VIDEOS?/DamnVid/Podcasts/'
		},
		'outdiru':{
			'name':'Output directory for iTunes U',
			'type':DV.preference_type_misc,
			'kind':'dir',
			'strict':True,
			'default':'?DAMNVID_MY_VIDEOS?/DamnVid/iTunesU/'
		},
		'maxepisodes':{
			'name':'Maximum episodes from podcast',
			'type':DV.preference_type_misc,
			'kind':'int:1-50',
			'strict':True,
			'default':'5'
		},
		'maxepisodesu':{
			'name':'Maximum lectures from iTunes U',
			'type':DV.preference_type_misc,
			'kind':'int:1-50',
			'strict':True,
			'default':'10'
		}
	},
	'preferences_order':['profile','profileu','outdir','outdiru','maxepisodes','maxepisodesu'],
	'register':{
		'listicons':{
			'itmspodcasts':'itmspodcasts.png',
			'itmspodcasts-itunesu':'itmspodcasts-itunesu.png'
		}
	}
})
