IrelandLivePuppet.groovy 10.6 KB
/*
 * Copyright (c) 2016 PuppetMaster.TV
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details:
 *
 *     https://www.gnu.org/licenses/gpl-2.0.txt
 */

/*
 * @author  MasterPuppeteer
 * @credit  pianoplink
 */

package tv.puppetmaster.ireland

import tv.puppetmaster.data.i.*
import tv.puppetmaster.data.i.Puppet.PuppetIterator
import tv.puppetmaster.data.i.SourcesPuppet.SourceDescription

public class IrelandLivePuppet implements InstallablePuppet {

    static final int VERSION_CODE = 5

    static final SOURCES = [
            "Featured": [
                    [
                            name       : "TV3",
                            description: null,
                            urls       : [
                                    "http://csm-e.cds1.yospace.com/csm/extlive/tv3ie01,tv3-prd.m3u8",
                                    "http://csm-e.cds1.yospace.com/csm/extlive/tv3ie01,tv3-prd.m3u8?yo.ac=true&yo.sl=3&yo.po=5&yo.ls=1,2,3"
                            ],
                            image      : "https://pbs.twimg.com/profile_images/683994318852485125/eQ0KQ53-.jpg",
                            background : "http://digitalspyuk.cdnds.net/13/45/640x320/landscape_soaps-tv3-logo.jpg"
                    ],
                    [
                            name       : "3e",
                            description: null,
                            urls       : [
                                    "http://csm-e.cds1.yospace.com/csm/live/109793242.m3u8",
                                    "http://csm-e.cds1.yospace.com/csm/extlive/tv3ie01,3e-prd.m3u8?yo.ac=true&yo.sl=3&yo.po=5&yo.ls=1,2,3"
                            ],
                            image      : "http://cce.gno.ie/_images/_newsphotos/3e.jpg",
                            background : "http://presentationarchive.com/3e/3e-bounce-f.jpg"
                    ],
                    [
                            name       : "TG4",
                            description: null,
                            urls       : [
                                    "http://csm-e.cds1.yospace.com/csm/live/74246540.m3u8"
                            ],
                            image      : "https://pbs.twimg.com/profile_images/781495637212880897/xkdUPw5N.jpg",
                            background : "http://wallpapercave.com/wp/7nG1B5h.jpg"
                    ],
            ],
    ]

    def ParentPuppet mParent
    def boolean mIsTopLevel
    def String mName
    def String mDescription
    def mSection = []

    IrelandLivePuppet() {
        this(null, true, "Ireland Live", "TV3, 3E, TG4", null)
    }

    IrelandLivePuppet(ParentPuppet parent, boolean isTopLevel, String name, String description, def section) {
        mParent = parent
        mIsTopLevel = isTopLevel
        mName = name
        mDescription = description
        mSection = section
    }

    @Override
    PuppetIterator getChildren() {
        PuppetIterator children = new IrelandLivePuppetIterator()
        if (!mSection) {
            SOURCES.each { k, section ->
                if (k == "Featured") {
                    section.each { source ->
                        IrelandLiveSourcesPuppet sourcesPuppet = new IrelandLiveSourcesPuppet(
                                this,
                                source.urls,
                                source.name,
                                source.containsKey("description") ? source.description : null,
                                source.image,
                                source.background
                        )
                        children.add(sourcesPuppet)
                    }
                } else {
                    children.add(new IrelandLivePuppet(this, true, k, null, section))
                }
            }
        } else {
            mSection.each { source ->
                IrelandLiveSourcesPuppet sourcesPuppet = new IrelandLiveSourcesPuppet(
                        this,
                        source.urls as ArrayList<String>,
                        source.name as String,
                        source.containsKey("description") ? source.description as String : null,
                        source.image as String,
                        source.background as String
                )
                children.add(sourcesPuppet)
            }
        }

        return children
    }

    @Override
    boolean isTopLevel() {
        return mIsTopLevel
    }

    @Override
    String getName() {
        return mName
    }

    @Override
    String getCategory() {
        return "Ireland"
    }

    @Override
    String getShortDescription() {
        return mDescription
    }

    @Override
    String getImageUrl() {
        return "http://www.irishjobs.ie/careeradvice/wp-content/uploads/Irish-TV-new-blog.jpg"
    }

    @Override
    String getBackgroundImageUrl() {
        return "http://wallpapercave.com/wp/7nG1B5h.jpg"
    }

    @Override
    boolean isUnavailableIn(String region) {
        return false
    }

    @Override
    String getPreferredRegion() {
        return null
    }

    @Override
    int getShieldLevel() {
        return 0
    }

    @Override
    ParentPuppet getParent() {
        return mParent
    }

    @Override
    SearchesPuppet getSearchProvider() {
        return null
    }

    @Override
    SettingsPuppet getSettingsProvider() {
        return null
    }

    @Override
    int getFastlaneBackgroundColor() {
        return 0xFFF08010
    }

    @Override
    int getSearchAffordanceBackgroundColor() {
        return 0xFFDCA300
    }

    @Override
    int getSelectedBackgroundColor() {
        return 0xFFF08010
    }

    @Override
    int getPlayerBackgroundColor() {
        return 0xFFF08010
    }

    @Override
    List<Map<String, String>> getLiveChannelsMetaData() {
        def list = []
        SOURCES.each { k, section ->
            section.each { source ->
                list << [
                        name       : k == "Featured" ? source.name : k + ": " + source.name,
                        description: source.containsKey("description") ? source.description : null,
                        genres     : "NEWS",
                        logo       : source.image,
                        url        : source.urls[0]
                ]
            }
        }
        return list
    }

    @Override
    PuppetIterator getRelated() {
        return null
    }

    @Override
    public String toString() {
        return mParent == null ? getName() : mParent.toString() + " < " + getName()
    }

    @Override
    int getVersionCode() {
        return VERSION_CODE
    }

    def static class IrelandLivePuppetIterator extends PuppetIterator {

        def ArrayList<Puppet> mPuppets = new ArrayList<>()
        def int currentIndex = 0

        @Override
        boolean hasNext() {
            return currentIndex < mPuppets.size()
        }

        @Override
        void add(Puppet puppet) {
            mPuppets.add(puppet)
        }

        @Override
        Puppet next() {
            return mPuppets.get(currentIndex++)
        }

        @Override
        void remove() {
        }
    }

    def static class IrelandLiveSourcesPuppet implements SourcesPuppet {

        def ParentPuppet mParent
        def mUrls = []
        def String mName
        def String mDescription
        def String mImageUrl
        def String mBackgroundImageUrl

        def List<SourcesPuppet.SubtitleDescription> mSubtitles = new ArrayList<SourcesPuppet.SubtitleDescription>()

        IrelandLiveSourcesPuppet(ParentPuppet parent, ArrayList<String> urls, String name, String description, String imageUrl, String backgroundImageUrl) {
            mParent = parent
            mUrls = urls
            mName = name
            mDescription = description
            mImageUrl = imageUrl
            mBackgroundImageUrl = backgroundImageUrl
        }

        @Override
        String getPublicationDate() {
            return null
        }

        @Override
        long getDuration() {
            return -1
        }

        @Override
        SourcesPuppet.SourceIterator getSources() {
            return new IrelandLiveSourceIterator()
        }

        @Override
        boolean isLive() {
            return true
        }

        @Override
        List<SourcesPuppet.SubtitleDescription> getSubtitles() {
            return mSubtitles
        }

        @Override
        String getName() {
            return mName
        }

        @Override
        String getCategory() {
            return null
        }

        @Override
        String getShortDescription() {
            return mDescription
        }

        @Override
        String getImageUrl() {
            return mImageUrl
        }

        @Override
        String getBackgroundImageUrl() {
            return mBackgroundImageUrl
        }

        @Override
        boolean isUnavailableIn(String region) {
            return region != 'ie'
        }

        @Override
        String getPreferredRegion() {
            return 'ie'
        }

        @Override
        int getShieldLevel() {
            return 0
        }

        @Override
        ParentPuppet getParent() {
            return mParent
        }

        @Override
        PuppetIterator getRelated() {
            return mParent.getChildren()
        }

        @Override
        public String toString() {
            return mParent == null ? getName() : mParent.toString() + " < " + getName()
        }

        def class IrelandLiveSourceIterator implements SourcesPuppet.SourceIterator {

            def List<SourceDescription> mSources = null
            def int currentIndex = 0

            @Override
            boolean hasNext() {
                if (mSources == null) {
                    mSources = new ArrayList<SourceDescription>()
                    for (String url : IrelandLiveSourcesPuppet.this.mUrls) {
                        SourceDescription source = new SourceDescription()
                        source.url = url
                        mSources.add(source)
                    }
                }
                return currentIndex < mSources.size()
            }

            @Override
            SourceDescription next() {
                return mSources.get(currentIndex++)
            }

            @Override
            void remove() {
            }
        }
    }
}