import org.serviio.library.metadata.*
import org.serviio.library.online.*
import org.ccil.cowan.tagsoup.*

/**
 * Livestation.com content URL extractor plugin. 
 * 
 *
 *  
 * @author Stefan Andersson
 *
 */
class LiveStation extends WebResourceUrlExtractor
{

    final VALID_RESOURCE_URL = '^https?://.*livestation.com$'
        
    String getExtractorName()
    {
        return getClass().getName()
    }
    
    boolean extractorMatches(URL resourceUrl)
    {
        return resourceUrl ==~ VALID_RESOURCE_URL
    }
    
    WebResourceContainer extractItems(URL resourceUrl, int maxItemsToRetrieve)
    {
        def html = openURL(resourceUrl, "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)")

        def node = new XmlSlurper(new Parser()).parseText(html).depthFirst().find() {it.@class.text() == "items"}    
        def items = []                           
            
        node.div.children().each()
        { 
          if (it.a.@class.text().contains("world_news"))
            if (!it.a.@class.text().contains("multilingual"))
            {
              def title = "(Native)"
              def href = it.a.@href.text()
              def img = it.a.img.@src.text()

              items.add(new WebResourceItem(title : title, additionalInfo : ['content' : href, 'thumb' : img]))

              log ("img: $img")
              log ("title: $title")
              log ("href: $href")

            }
            else
            {
              def title = "(" + it.div.ul.li[0].a.text() + ")"
              def href = it.div.ul.li[0].a.@href.text()
              def img = it.a.img.@src.text()

              items.add(new WebResourceItem(title : title, additionalInfo : ['content' : href, 'thumb' : img]))

              log ("img: $img")
              log ("title: $title")
              log ("href: $href")

              title = "(" + it.div.ul.li[1].a.text() + ")"
              href = it.div.ul.li[1].a.@href.text()
              img = it.a.img.@src.text()

              items.add(new WebResourceItem(title : title, additionalInfo : ['content' : href, 'thumb' : img]))

              log ("img: $img")
              log ("title: $title")
              log ("href: $href")
            }                           
        }        
                                    
        return new WebResourceContainer(title : "", items: items)
    }

    ContentURLContainer extractUrl(WebResourceItem item, PreferredQuality requestedQuality)
    {
       def linkUrl = "http://livestation.com" + 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 contentUrl = html.find(/ipadUrl:([\s\S])*/).find(/".*?"/).replaceAll('"', "")

       if (requestedQuality == PreferredQuality.HIGH)
         contentUrl = contentUrl.replaceAll(",264,528,828,", "828")
       else if (requestedQuality == PreferredQuality.MEDIUM)
         contentUrl = contentUrl.replaceAll(",264,528,828,", "528")
       else
         contentUrl = contentUrl.replaceAll(",264,528,828,", "264")

       return new ContentURLContainer(contentUrl: contentUrl, thumbnailUrl: thumbnailUrl, live : true)
    }

       static void main(args)
       {
          def l = new LiveStation()
          l.extractItems(new URL("http://www.livestation.com"), -1)
          
       }
 }