# Path: lib/pirateplay/lib/rerequest.py
# class TemplateRequest:
# 	def __init__(self, re,
# 				encode_vars = lambda x: x, decode_content = lambda c, v: c,
# 				handlerchain = None):
# 		self.re = re
# 		self.encode_vars = encode_vars
# 		self.decode_content = decode_content
# 		self.handlerchain = handlerchain
# 	
# 	def create_vars(self, content, cumulated_vars):
# 		content = self.decode_content(content, cumulated_vars)
# 		
# 		#Make sure req_data and req_headers are empty
# 		if 'req_data' in cumulated_vars:
# 			del cumulated_vars['req_data']
# 		if 'req_headers' in cumulated_vars:
# 			del cumulated_vars['req_headers']
# 		
# 		for match in re.finditer(self.re, content, re.DOTALL):
# 			d = dict(cumulated_vars, **match.groupdict())
# 			d.update(self.encode_vars(d))
# 			yield d
# 	
# 	def create_req(self, cumulated_vars):
# 		url = cumulated_vars['req_url']
# 		if not url.startswith('http'):
# 			url = 'http://' + url
# 		req = urllib2.Request(url)
# 		
# 		if 'req_data' in cumulated_vars:
# 			debug_print('Adding post data to request: ' + cumulated_vars['req_data'])
# 			req.add_data(cumulated_vars['req_data'])
# 		
# 		for header, value in cumulated_vars.get('req_headers', {}).items():
# 			debug_print('Adding header to request: %s = %s' % (header, value))
# 			req.add_header(header, value)
# 		return req
# 	
# 	def create_content(self, req):
# 		debug_print('Opening URL: ' + req.get_full_url())
# 		try:
# 			try:
# 				f = urllib2.build_opener(self.handlerchain).open(req)
# 			except TypeError:
# 				f = urllib2.urlopen(req)
# 			
# 			content = f.read()
# 			f.close()
# 		except (urllib2.HTTPError, urllib2.URLError):
# 			content = ''
# 		
# 		return content
# 	
# 	def to_dict(self):
# 		return { 'test': self.re }


from ..rerequest import TemplateRequest
	from pyamf import remoting
				"/1", 
				remoting.Request(
					target="com.brightcove.player.runtime.PlayerMediaFacade.findMediaById", 
					body=[const, player_id, video_player, publisher_id],
					envelope=env
				)
			)
		)
		return str(remoting.encode(env).read())

	def decode_bc(content, v):
		if content == '':
			return ''
		
		return ''.join(['"%sx%s:%s";' % (rendition['frameWidth'], rendition['frameHeight'], rendition['defaultURL'])
						for rendition in remoting.decode(content).bodies[0][1].body['renditions']])

except ImportError:
	print 'PyAMF not found! Brightcove support dissabled!'
	def brightcovedata(video_player, player_id, publisher_id, const):
		pass
	def decode_bc(content, v):
		pass

services = [{ 'title': 'Elitserien-play', 'url': 'http://elitserienplay.se/',
				'items': [TemplateRequest(
							re = r'(http://)?(www\.)?elitserienplay\.se/.*?video\.(?P<video_player>\d+)',
							encode_vars = lambda v: { 'req_data': brightcovedata(v['video_player'], 1199515803001, 656327052001, '2ba01fac60a902ffc3c6322c3ef5546dbcf393e4'),
														'headers': { 'Content-type': 'application/x-amf' },
														'req_url': 'http://c.brightcove.com/services/messagebroker/amf?playerKey=AQ~~,AAAAmNAkCuE~,RfA9vPhrJwdowytpDwHD00J5pjBMVHD6' } ),

# Path: lib/pirateplay/lib/rerequest.py
# class TemplateRequest:
# 	def __init__(self, re,
# 				encode_vars = lambda x: x, decode_content = lambda c, v: c,
# 				handlerchain = None):
# 		self.re = re
# 		self.encode_vars = encode_vars
# 		self.decode_content = decode_content
# 		self.handlerchain = handlerchain
# 	
# 	def create_vars(self, content, cumulated_vars):
# 		content = self.decode_content(content, cumulated_vars)
# 		
# 		#Make sure req_data and req_headers are empty
# 		if 'req_data' in cumulated_vars:
# 			del cumulated_vars['req_data']
# 		if 'req_headers' in cumulated_vars:
# 			del cumulated_vars['req_headers']
# 		
# 		for match in re.finditer(self.re, content, re.DOTALL):
# 			d = dict(cumulated_vars, **match.groupdict())
# 			d.update(self.encode_vars(d))
# 			yield d
# 	
# 	def create_req(self, cumulated_vars):
# 		url = cumulated_vars['req_url']
# 		if not url.startswith('http'):
# 			url = 'http://' + url
# 		req = urllib2.Request(url)
# 		
# 		if 'req_data' in cumulated_vars:
# 			debug_print('Adding post data to request: ' + cumulated_vars['req_data'])
# 			req.add_data(cumulated_vars['req_data'])
# 		
# 		for header, value in cumulated_vars.get('req_headers', {}).items():
# 			debug_print('Adding header to request: %s = %s' % (header, value))
# 			req.add_header(header, value)
# 		return req
# 	
# 	def create_content(self, req):
# 		debug_print('Opening URL: ' + req.get_full_url())
# 		try:
# 			try:
# 				f = urllib2.build_opener(self.handlerchain).open(req)
# 			except TypeError:
# 				f = urllib2.urlopen(req)
# 			
# 			content = f.read()
# 			f.close()
# 		except (urllib2.HTTPError, urllib2.URLError):
# 			content = ''
# 		
# 		return content
# 	
# 	def to_dict(self):
# 		return { 'test': self.re }


# Path: lib/pirateplay/lib/services/elitserienplay.py
from ..rerequest import TemplateRequest
	from pyamf import remoting

				"/1", 
				remoting.Request(
					target="com.brightcove.player.runtime.PlayerMediaFacade.findMediaById", 
					body=[const, player_id, video_player, publisher_id],
					envelope=env
				)
			)
		)
		return str(remoting.encode(env).read())

	def decode_bc(content, v):
		if content == '':
			return ''
		
		return ''.join(['"%sx%s:%s";' % (rendition['frameWidth'], rendition['frameHeight'], rendition['defaultURL'])
						for rendition in remoting.decode(content).bodies[0][1].body['renditions']])

except ImportError:
	print 'PyAMF not found! Brightcove support dissabled!'
	def brightcovedata(video_player, player_id, publisher_id, const):
		pass
	def decode_bc(content, v):
		pass

services = [{ 'title': 'Elitserien-play', 'url': 'http://elitserienplay.se/',
				'items': [TemplateRequest(
							re = r'(http://)?(www\.)?elitserienplay\.se/.*?video\.(?P<video_player>\d+)',
							encode_vars = lambda v: { 'req_data': brightcovedata(v['video_player'], 1199515803001, 656327052001, '2ba01fac60a902ffc3c6322c3ef5546dbcf393e4'),
														'headers': { 'Content-type': 'application/x-amf' },
														'req_url': 'http://c.brightcove.com/services/messagebroker/amf?playerKey=AQ~~,AAAAmNAkCuE~,RfA9vPhrJwdowytpDwHD00J5pjBMVHD6' } ),

						TemplateRequest(