
import org.serviio.library.metadata.*
import org.serviio.library.online.*

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TVO Big Ideas content URL extractor plugin v1.0 
 *  
 * @author Fan Yang
 * 
 * This is a hack. 
 * Serviio can not parse TVO's video RSS feed.  It should work until Serviio is fixed 
 * This plugin parses the RSS feed as a Web Resource than RSS/Atom feed
 */
 class TVO extends WebResourceUrlExtractor {

    // should cover all TVO video RSS feed
    // all supported feed URLs are in main()
    final VALID_FEED_URL = '^http://feeds.tvo.org/.*$'
    
    // feed specific thumbnail is too hard to parse, use a generic TVO logo
	final THUMB_URL = "http://ww3.tvo.org/sites/all/themes/custom/tvo_org/images/tvo-logo-lrg.png"

    String getExtractorName() {
        return getClass().getName()
    }
    
    boolean extractorMatches(URL feedUrl) {
        return feedUrl ==~ VALID_FEED_URL
    }

    WebResourceContainer extractItems(URL resourceUrl, int maxItemsToRetrieve) {
        String programmeHtml = openURL(resourceUrl, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1')
        log.debug("extractItems: $resourceUrl")
        println "extractItems: resourceURL=$resourceUrl"
        
        // get the title
		def titleMatcher = programmeHtml =~ /(?i)<title>([^<]+)<\/title>/
		println("extractItems: program title=" + titleMatcher[0][1])  // print the program title
		int count = titleMatcher.getCount()
        log.debug("extractItems: #title = $count")
        println "extractItems: #title = $count"
                
        // get the content url 
		def urlMatcher = programmeHtml =~ /(?i)<feedburner:origEnclosureLink>([^<]+)<\/feedburner:origEnclosureLink>/
        count = urlMatcher.getCount()
        log.debug("extractItems: #url = $count")
        println "extractItems: #url = $count"
                        
        // get the thumnail URL: use the default thumb url
        
        // generate the item lists
		List<WebResourceItem> items = []
		for ( i in 0..java.lang.Math.min(urlMatcher.getCount(),maxItemsToRetrieve)-1 ) {
		    String title = titleMatcher[i+2][1] // 1st 2 titles are program titles
		    String contentUrl = urlMatcher[i][1]
		    println "$i: [$title] $contentUrl"
		    log.debug("found item[$i]: [$title] $contentUrl")    
			WebResourceItem item = new WebResourceItem(title: title, 
			    additionalInfo: ['assetId':i, 'thumbnailUrl':THUMB_URL, 'contentUrl':contentUrl])
			items << item    		    
		}
		
		// return with the TVO title
		log.debug("finished: extractItems");
		return new WebResourceContainer(title: titleMatcher[0][1], thumbnailUrl: THUMB_URL, items: items)
    }

    ContentURLContainer extractUrl(WebResourceItem item, PreferredQuality requestedQuality) {
        log.debug("extractUrl: item= $item")      
        String contentUrl = item.getAdditionalInfo()['contentUrl']
        log.debug("extractUrl: $contentUrl")        
        return new ContentURLContainer(fileType: MediaFileType.VIDEO, contentUrl: contentUrl,       
            thumbnailUrl: item.additionalInfo.thumbnailUrl)
    }
    
    // unit tests
    static void main(args) {
    
        TVO extractor = new  TVO()
        
        // big ideas
        String url = "http://feeds.tvo.org/tvobigideasVideo?format=xml"
        assert extractor.extractorMatches( new URL(url))
        WebResourceContainer container = extractor.extractItems( new URL(url), 10)

        // alan gregg
        url = "http://feeds.tvo.org/podcasts/video/AllanGreggInConversation?format=xml"
        assert extractor.extractorMatches( new URL(url))        
        container = extractor.extractItems( new URL(url), 10)         

        // The Agenda
        url = "http://feeds.tvo.org/tvo/TxZN"
        assert extractor.extractorMatches( new URL(url))        
        container = extractor.extractItems( new URL(url), 10)

        // Think Again
        url = "http://feeds.tvo.org/tvo/thinkAgain"
        assert extractor.extractorMatches( new URL(url))        
        container = extractor.extractItems( new URL(url), 10)   
    }
}

