#!/usr/bin/env python
# -*- coding: utf-8 -*-

import demjson, re

HTTP_HEADERS = {
	'Accept	text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
	'Accept-Encoding:	gzip, deflate',
	'Accept-Language:	en-US,en;q=0.5',
	'Connection:		keep-alive',
	'Referer:		http://vidoza.net',
	'User-Agent:		Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0'
}

ART = 'http://i.imgur.com/GwSnkxE.jpg'
COVER = 'http://i.imgur.com/0snKSZt.png'

########################################################################################
def NormalizeURL(url):

	return url

####################################################################################################
def MetadataObjectForURL(url):
	
	try:
		page = HTTP.Request(url).content
		sources = re.findall(r'src.*=.*\"(.*\.mp4)\"', page)[0]
 		thumb = re.findall(r'poster.*:.*\"(.*\.jpg.*)\"', page)[0]
	except:
		thumb = ''
		
	title = 'Vidoza Redirect Page'
	summary = 'Summary info Page'
	
	return VideoClipObject(
		title = title,
		summary = summary + '\n\n',
		art = Resource.ContentsOfURLWithFallback([thumb,ART]),
		thumb = Resource.ContentsOfURLWithFallback([thumb,COVER])
	)
	
####################################################################################################
def MediaObjectsForURL(url):

	source = None
	try:
		page = HTTP.Request(url).content
		source = re.findall(r'src.*=.*\"(.*\.mp4)\"', page)[0]
 		thumb = re.findall(r'poster.*:.*\"(.*\.jpg.*)\"', page)[0]
	except Exception as e:
		Log(e)
		raise Ex.MediaNotAvailable
	
	return [
		MediaObject(
			container = Container.MP4,
			#video_codec = VideoCodec.H264,
			video_resolution = '720',
			audio_codec = AudioCodec.AAC,
			audio_channels = 2,
			optimized_for_streaming = True,
			parts = [PartObject(key=Callback(PlayVideo, url=source))]
		)
	]

####################################################################################################
@indirect
def PlayVideo(url):
	
	return IndirectResponse(VideoClipObject, key=url)
