import org.serviio.library.metadata.*
import org.serviio.library.online.*
import org.ccil.cowan.tagsoup.*

/**
 * HardcoreinHD.com content URL extractor plugin. 
 * 
 *
 *  
 * @author Pornoman
 *
 */
class Hardcore extends WebResourceUrlExtractor {
    
    final VALID_RESOURCE_URL = '^https?://www.hardcoreinhd.com/porn-video.*$'
        
    String getExtractorName() {
        return getClass().getName()
    }
    
    boolean extractorMatches(URL resourceUrl) {
        return resourceUrl ==~ VALID_RESOURCE_URL
    }
    
    WebResourceContainer extractItems(URL resourceUrl, int maxItemsToRetrieve) {

        def conn = resourceUrl.openConnection()
        conn.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)")

        def root = new XmlSlurper(new Parser()).parseText(conn.getInputStream().getText())

 
        def node = root.depthFirst().find() {it.@class.text() == "listThumbs"}
       
        def items = []

        node.children().each() {               

          if (!it.a[0].@href.text().contains("wankz")) {

            def src = it.a[0].img.@src.text()
            def ref = it.a[0].@href.text().find(/\d.*$/)
            def title = it.a[1].@title.text()
                     
            items.add(new WebResourceItem(title : title, additionalInfo : ['content' : ref, 'thumb' : src]))
                           
            log("src: $src")
            log("title: $title")
            log("ref: $ref")

         }

        }        
                                    
        return new WebResourceContainer(title : "", items: items)
    }

    ContentURLContainer extractUrl(WebResourceItem item, PreferredQuality requestedQuality) {
        
        def linkUrl = "http://www.hardcoreinhd.com/porn-video/${item.additionalInfo['content']}"
        def thumbnailUrl = item.additionalInfo['thumb']

        def html = openURL(new URL(linkUrl), "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)")

        def resolution
        if (requestedQuality == PreferredQuality.HIGH) resolution = html.find(/streams([\s\S])*/).find(/"1080p"([\s\S])*/)
        else if (requestedQuality == PreferredQuality.MEDIUM) resolution = html.find(/streams([\s\S])*/).find(/"720p"([\s\S])*/)
        else resolution = html.find(/streams([\s\S])*/).find(/"480p"([\s\S])*/)
        def contentUrl
        if (resolution)
          contentUrl = resolution.find(/"http.*?"/).replaceAll('"', "")
        else contentUrl = null
         
        return new ContentURLContainer(contentUrl: contentUrl, thumbnailUrl: thumbnailUrl, expiresImmediately : true, cacheKey : linkUrl)

    }

    static void main(args) {
      
      def extractor = new Hardcore()
            
      def container = extractor.extractItems(new URL("http://www.hardcoreinhd.com/porn-video"), -1)
      println container

      def result = extractor.extractUrl(container.getItems()[2], PreferredQuality.MEDIUM)
      println result	  
    } 

 }