import groovy.json.JsonSlurper
import org.serviio.library.metadata.*
import org.serviio.library.online.*
import org.serviio.util.*
import java.io.FileInputStream;
import java.util.zip.GZIPInputStream;

/**
 * WebResource extractor plugin for pornhub.com.
 * 
 * @author jhb50
 * Version 1 - May. 30, 2012
 *
 */
class PornHub extends WebResourceUrlExtractor {
	
	final VALID_FEED_URL = '^(?:https?://)?(?:www\\.)?pornhub\\.com.*+'
	
	String getExtractorName() {
		return 'PornHub'
	}
	
	Boolean URLExists(URL fileURL){
		if(((HttpURLConnection) fileURL.openConnection()).getResponseCode() == 404){
			return false
		}
		return true
	}

	boolean extractorMatches(URL feedUrl) {
		return feedUrl ==~ VALID_FEED_URL
	}
	
	WebResourceContainer extractItems(URL resourceUrl, int maxItems) {
		List<WebResourceItem> items = []
		def itemsAdded = 0
		String pageTitle = ""
		String pageThumb = ""
		String videoUrl = ""
		String videoTitle = ""
		String thumbUrl = ""

		String html = resourceUrl.getText()


		def titleMatcher = html =~ '(?s)<h1 class="section_title">(.+?)<'
		pageTitle = titleMatcher[0][1].trim()

	    if (html.count('<li class="videoblock') > 0 ){

			def videoMatcher = html =~ '(?s)<li class="videoblock.*?<a href="(.*?)".*?title="(.*?)".*?img src="(.*?)\\?cache'

			for( int i = 0; i < videoMatcher.size() && (maxItems == -1 || itemsAdded < maxItems); i++ ) {
				videoUrl = videoMatcher[i][1].trim()
				videoTitle = videoMatcher[i][2].trim()
				thumbUrl = videoMatcher[i][3].trim()
				
				if (!URLExists(new URL(videoUrl))){
					log ("404 for $videoUrl")
					continue
				}
				
				WebResourceItem item = new WebResourceItem(title: videoTitle, additionalInfo: ['videoUrl':videoUrl,'thumbUrl':thumbUrl])
				
				items << item
				itemsAdded++
			}
		}
		return new WebResourceContainer(title: pageTitle, thumbnailUrl: pageThumb, items: items)
	}

	
	ContentURLContainer extractUrl(WebResourceItem item, PreferredQuality requestedQuality) {		
		
		String videoTitle = item.title
		String videoUrl = item.getAdditionalInfo()['videoUrl']
		String thumbnailUrl = item.getAdditionalInfo()['thumbUrl']
		
		String videohtml = new URL(videoUrl).getText()
		def flashMatch = videohtml ==~ '(?s).*?var.flashvars.*?'
		if (!flashMatch){
			log("NO ENTRIES FOR THIS ITEM - \'$videoTitle\'") 
			return null
		}

		log("Getting item: $videoTitle")
		
		
		String videostart = videohtml.minus(~'(?s).*?var.flashvars.*?')
		videostart = videostart.replaceFirst(~'(?s)var.embedSWF.*?</html>',"")
		String videolink = URLDecoder.decode(videostart)

		def linkMatcher = videolink =~ '(?s).*?video_url":"(.*?)".*?'
		String linkUrl = linkMatcher[0][1]

		def cacheKey = thumbnailUrl + "_" +  videoUrl
	
		return new ContentURLContainer(fileType: MediaFileType.VIDEO, contentUrl: linkUrl, thumbnailUrl: thumbnailUrl, cacheKey: cacheKey, expiresImmediately: true)
	}
	
	static void main(args) {
		PornHub extractor = new PornHub()
				
		assert extractor.extractorMatches( new URL("http://www.pornhub.com") )
		assert !extractor.extractorMatches( new URL("http://google.com/feeds/api/standardfeeds/top_rated?time=today") )
		WebResourceContainer container = extractor.extractItems( new URL("http://www.pornhub.com"), 3)    
		println container
		
		ContentURLContainer result = extractor.extractUrl(container.getItems()[1], PreferredQuality.MEDIUM)
		print result
	}
}
