import groovy.json.JsonSlurper
import org.serviio.library.metadata.*
import org.serviio.library.online.*
import org.serviio.util.*

class veetleis extends WebResourceUrlExtractor {

	
	final VALID_FEED_URL = 'http*://.*?veetls\\.com/numc$?'
	
	String getExtractorName() {
		return 'veetls'
	}
	
	boolean extractorMatches(URL feedUrl) {
		return feedUrl ==~ VALID_FEED_URL
	}


WebResourceContainer extractItems(URL resourceUrl, int maxItemsToRetrieve)
{

// Example Response Data
def restResponse = new URL("http://veetle.com/index.php/listing/ajaxStreamByCategory/20/0/" + numc +"/0").text 
// Parse the response
def list = new JsonSlurper().parseText( restResponse )
// Print them out to make sure

def toal = list.totalCount as int
def total = numc as int

if ( toal > total ) {

 toal = numc
		}


		List<WebResourceItem> items = []
		
		def itemsAdded = 0;
		for( int i = 0; i < toal ; i++) {
			String programName = list.payload.username [i]
			String programID = list.payload.channelId [i]
			String thumbu = list.payload.thumbnailUrl [i]
                        def testa = new URL ("http://veetle.com/index.php/stream/ajaxStreamLocation/" + programID + "/flash").text
                        def linka = new JsonSlurper().parseText( testa )
			String channurl = linka.payload
			WebResourceItem item = new WebResourceItem( title: programName, additionalInfo : ['thumbID': thumbu , 'programID': channurl])
			println "item = $item"
			items << item
			itemsAdded++			
		}
			
		return new WebResourceContainer(title: 'http://www.veetle.com', items: items)
	}
	ContentURLContainer extractUrl(WebResourceItem mediaitem, PreferredQuality requestedQuality) {		
                String contentUrl = mediaitem.getAdditionalInfo()['programID'] 
		String thumbUrl = mediaitem.getAdditionalInfo()['thumbID']
		return new ContentURLContainer(fileType: MediaFileType.VIDEO, contentUrl: contentUrl, thumbnailUrl: thumbUrl,  live: true)
	}

}
