import java.util.regex.Matcher
import groovy.util.XmlParser
import groovy.json.JsonSlurper
import org.serviio.library.metadata.*
import org.serviio.library.online.*
import org.serviio.util.*

/**
 * WebResource extractor plugin for beeg.com
 * 
 * @author jhb50
 * Version 1 - Nov. 10, 2012
 *         2 - Nov 12, 2012 by Miksa to support tags
 *
 */
class Beeg extends WebResourceUrlExtractor {
	
	final VALID_FEED_URL = '^(?:https?://)?(?:www\\.)?beeg\\.com/tag.*'
	
	String getExtractorName() {
		return 'Beeg'
	}
	
	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) {
		log("Parsing $resourceUrl")
		
		List<WebResourceItem> items = []
		String pageThumb = ""
		
		String html = openURL(resourceUrl, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1")

		Matcher titleMatcher = html =~ '(?s)<meta name="Description" content="(.+?)"'
		String pageTitle = titleMatcher[0][1].trim()
		
		(html =~ '(?s).*?<div id="thumbs">(.*?)</div>').each {
			List<Matcher> thumbMatcher = it;
			Matcher videoMatcher = thumbMatcher[1] =~ '(?s).*?<a.href="(.*?)".*?<img.src="(.*?)".*?title="(.*?)".*?'

			int videoCount = videoMatcher.size();
			for( int i = 0; i < videoCount && i < maxItems; i++ ) {
				String videoUrl = videoMatcher[i][1].trim()
				String thumbUrl = videoMatcher[i][2].trim()
				String videoTitle = videoMatcher[i][3].trim()
								
				WebResourceItem item = new WebResourceItem(
					  title: videoTitle
					, additionalInfo: ['videoUrl':videoUrl,'thumbUrl':thumbUrl]
				);
				
				items << item;
			}
		}
		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 = openURL(new URL(videoUrl),"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1")
		
		
		
		def javaMatch = videohtml ==~ "(?s).*?<div class=\"media-box\">.*?<div id=\"media\">.*?<script.type='text/javascript'.*?"
		if (!javaMatch){
			log.info("NO ENTRIES FOR THIS ITEM - \'$videoTitle\'")
			println "NO ENTRIES FOR THIS ITEM - \'$videoTitle\'"
			return null
		}

		def linkMatcher = videohtml =~ "(?s).*?'file'.*?'(.*?)'.*?"
		String linkUrl = linkMatcher[0][1]

		def cacheKey = getExtractorName() + "_" +  videoUrl
	
		return new ContentURLContainer(fileType: MediaFileType.VIDEO, contentUrl: linkUrl, thumbnailUrl: thumbnailUrl, cacheKey: cacheKey, expiresImmediately: true)
	}
	
	static void main(args) {
		Beeg extractor = new Beeg();
		String testUrl = "http://beeg.com/tag/orgasm";
		int testCount = 10;
				
		assert extractor.extractorMatches( new URL(testUrl) )
		assert !extractor.extractorMatches( new URL("http://google.com/feeds/api/standardfeeds/top_rated?time=today") )
		WebResourceContainer container = extractor.extractItems( new URL(testUrl), testCount)    
		assert testCount == container.items.size();
		println container.items[0];
		
		ContentURLContainer result = extractor.extractUrl(container.items[0], PreferredQuality.MEDIUM)
		println result
	}
}
