import org.serviio.library.online.*
import org.serviio.library.metadata.*
import java.util.regex.*

/**
 * Make RSS feeds provided by the VDR streamdev plugin usable by Serviio.
 * 
 * @author Andreas Benneke
 * @version 1.0
 */
public class VDRStreamdevRSS extends FeedItemUrlExtractor {

  // Feeds:
  // - http://hostname:3000/channels.rss
  // - http://hostname:3000/TS/channels.rss
  // - http://hostname:3000/PES/channels.rss
  // - http://hostname:3000/ES/channels.rss
  // - http://hostname:3000/EXT/channels.rss
  // - http://hostname:3000/groups.rss?group=1
  // - http://hostname:3000/TS/groups.rss?group=1
  // - http://hostname:3000/PES/groups.rss?group=1
  // - http://hostname:3000/ES/groups.rss?group=1
  // - http://hostname:3000/EXT/groups.rss?group=1
  
  // Recordings do not work:
  // (ffmpeg fails to analyse the stream, they do not show up - and having them all in a flat list
  // which is only updated once per hour does not seem desirable too)
  // - http://hostname:3000/recordings.rss
  // - http://hostname:3000/TS/recordings.rss
  // - http://hostname:3000/PES/recordings.rss
  // - http://hostname:3000/ES/recordings.rss
  // - http://hostname:3000/EXT/recordings.rss

	private static final VALID_CHANNEL_URL = 'http://[^/]+(/|/TS/|/PES/|/ES/|/EXT/)channels.rss';
	private static final VALID_GROUPS_URL = 'http://[^/]+(/|/TS/|/PES/|/ES/|/EXT/)groups.rss\\?group=\\d+';

	@Override
	public String getExtractorName() {
		return 'VDR Streamdev Channel RSS Extractor'
	}

	public boolean extractorMatches(URL feedUrl) {
		return feedUrl ==~ VALID_CHANNEL_URL || 
		       feedUrl ==~ VALID_GROUPS_URL
	}

	protected ContentURLContainer extractUrl(Map links, PreferredQuality quality) {
	  URL url = links['default']
		return new ContentURLContainer(fileType: MediaFileType.VIDEO, contentUrl: url, cacheKey: url, live: true)
	}

	/**
	 * Testing...
	 */
	public static void main(String[] args) {
	
		VDRStreamdevRSS plugin = new VDRStreamdevRSS();

    assert plugin.extractorMatches(new URL('http://hostname:3000/channels.rss'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/TS/channels.rss'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/PES/channels.rss'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/ES/channels.rss'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/EXT/channels.rss'))
    assert !plugin.extractorMatches(new URL('http://hostname:3000/invalidchannels.rss'))
    assert !plugin.extractorMatches(new URL('http://hostname:3000/invalid/channels.rss'))
    assert !plugin.extractorMatches(new URL('http://hostname:3000/channels.rss?invalid'))

    assert plugin.extractorMatches(new URL('http://hostname:3000/groups.rss?group=1'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/TS/groups.rss?group=1'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/PES/groups.rss?group=1'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/ES/groups.rss?group=1'))
    assert plugin.extractorMatches(new URL('http://hostname:3000/EXT/groups.rss?group=1'))
    assert !plugin.extractorMatches(new URL('http://hostname:3000/invalid/groups.rss?group=1'))
    assert !plugin.extractorMatches(new URL('http://hostname:3000/groups.rss?group=invalid'))
    assert !plugin.extractorMatches(new URL('http://hostname:3000/groups.rss')) // this only lists the groups... 

		Map links = ['default': new URL('http://hostname:3000/TS/12345:67890.rec')]
		ContentURLContainer result = plugin.extractUrl(links, PreferredQuality.MEDIUM)
		assert result.fileType == MediaFileType.VIDEO
		assert result.contentUrl == 'http://hostname:3000/TS/12345:67890.rec'
		assert result.cacheKey == 'http://hostname:3000/TS/12345:67890.rec'
		assert result.live == true
	}
}
