CBCPuppet.groovy 28.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
/*
 * Copyright (c) 2017 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  learningit
 */

package tv.puppetmaster.canada

import org.json.JSONArray
import org.json.JSONObject
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import tv.puppetmaster.data.i.*
import tv.puppetmaster.data.i.Puppet.PuppetIterator
import tv.puppetmaster.data.i.SourcesPuppet.SourceDescription
import tv.puppetmaster.data.i.SourcesPuppet.SourceIterator
import tv.puppetmaster.data.i.SourcesPuppet.SubtitleDescription

import java.util.concurrent.TimeUnit
import java.util.regex.Matcher
import java.util.regex.Pattern

public class CBCPuppet implements InstallablePuppet {

    static final int VERSION_CODE = 5;

    transient def Document mDocument;

    def ParentPuppet mParent;

    def String mBaseUrl;
    def String mSearchUrl;

    def String mUrl;
    def String mName;
    def String mCategory;
    def String mImageUrl;
    def String mBackgroundImageUrl;
    def String mRelatedUrl;

    def transient PuppetIterator mChildren;
    def transient PuppetIterator mRelated;

    def transient ScrapeUrlSearchesPuppet mSearchProvider;

    def String mSourcesItemScrapeUrlFormat;
    def String mSourcesItemScrapeUrlPattern;

    def String[] mCategoryItemSelectors;
    def String mSourcesItemScrapeUrlSelector;
    def String mSourcesItemSelector;
    def String mSourcesNameSelector;
    def String mSourcesCategorySelector;
    def String mSourcesDescriptionSelector;
    def String mSourcesDurationSelector;
    def String mSourcesPublicationDateSelector;
    def String mSourcesIsLiveSelector;
    def String mSourcesImageSelector;
    def String mSourcesBackgroundImageSelector;
    def String mSourcesRelatedScrapeUrlSelector;

    public CBCPuppet() {
        this(
                null,
                "http://www.cbc.ca",
                "/player/search?query=",
                "/player",
                "CBC.ca",
                "http://rlv.zcache.ca/cbc_radio_canada_gem_round_sticker-r51dda5f884654d579ee93b01c8f580a5_v9wth_8byvr_324.jpg",
                "/bc/community/blog/photo/Laptop%20Decal_V2.jpg",
                null,
                ["nav ul li:nth-child(4) a,nav ul li:nth-child(5) a", ".longlist-list li a"] as String[],
                "http://tpfeed.cbc.ca/f/ExhSPC/vms_5akSXx4Ng_Zn?q=*&byGuid=#1",
                "(\\d+)",
                ".livenow-item,.allfeatured-item:not(:contains(\\(Live at):contains(ET\\))),.usa-container:has(.usa-title),.medialist-item:has(.medialist-title)",
                "a:first-child",
                ".livenow-title,.allfeatured-title,.usa-title,.medialist-title",
                ".livenow-category,.allfeatured-show,.usa-show,.medialist-show",
                ".livenow-description,.allfeatured-description,.usa-description,.medialist-seasonepisode",
                ".livenow-duration,.allfeatured-duration,.usa-duration,.medialist-duration",
                ".livenow-date,.allfeatured-banner span,.usa-date,.medialist-date",
                ".icon-live",
                ".livenow-thumbnail img,.allfeatured-image img,img.usa-image,.medialist-thumbnail img",
                ".livenow-thumbnail img,.allfeatured-image img,img.usa-image,.medialist-thumbnail img",
                "a"
        );
    }

    public CBCPuppet(ParentPuppet parent, String baseUrl, String searchUrl, String url, String name, String imageUrl, String backgroundImageUrl, String relatedUrl, String[] categoryItemSelectors, String sourcesItemScrapeUrlFormat, String sourcesItemScrapeUrlPattern, String sourcesItemSelector, String sourcesItemScrapeUrlSelector, String sourcesNameSelector, String sourcesCategorySelector, String sourcesDescriptionSelector, String sourcesDurationSelector, String sourcesPublicationDateSelector, String sourcesIsLiveSelector, String sourcesImageSelector, String sourcesBackgroundImageSelector, String sourcesRelatedScrapeUrlSelector) {
        mParent = parent;

        mBaseUrl = baseUrl;
        mSearchUrl = searchUrl != null && searchUrl.startsWith("/") ? baseUrl + searchUrl : searchUrl;

        mUrl = url != null && url.startsWith("/") ? baseUrl + url : url;
        mName = name;
        mCategory = parent != null ? mParent.getName() : null;
        mImageUrl = imageUrl != null && imageUrl.startsWith("/") ? baseUrl + imageUrl : imageUrl;
        mBackgroundImageUrl = backgroundImageUrl != null && backgroundImageUrl.startsWith("/") ? baseUrl + backgroundImageUrl : backgroundImageUrl;
        mRelatedUrl = relatedUrl != null && relatedUrl.startsWith("/") ? baseUrl + relatedUrl : relatedUrl;

        mCategoryItemSelectors = categoryItemSelectors;

        mSourcesItemScrapeUrlFormat = sourcesItemScrapeUrlFormat != null && sourcesItemScrapeUrlFormat.startsWith("/") ? baseUrl + sourcesItemScrapeUrlFormat : sourcesItemScrapeUrlFormat;
        mSourcesItemScrapeUrlPattern = sourcesItemScrapeUrlPattern;

        mSourcesItemSelector = sourcesItemSelector;
        mSourcesItemScrapeUrlSelector = sourcesItemScrapeUrlSelector;
        mSourcesNameSelector = sourcesNameSelector;
        mSourcesCategorySelector = sourcesCategorySelector;
        mSourcesDescriptionSelector = sourcesDescriptionSelector;
        mSourcesDurationSelector = sourcesDurationSelector;
        mSourcesPublicationDateSelector = sourcesPublicationDateSelector;
        mSourcesIsLiveSelector = sourcesIsLiveSelector;
        mSourcesImageSelector = sourcesImageSelector;
        mSourcesBackgroundImageSelector = sourcesBackgroundImageSelector;
        mSourcesRelatedScrapeUrlSelector = sourcesRelatedScrapeUrlSelector;
    }

    @Override
    public String getName() {
        return mName;
    }

    public void setCategory(String category) {
        mCategory = category;
    }

    @Override
    public String getCategory() {
        return mParent == null ? "Public Service" : mCategory;
    }

    @Override
    public String getShortDescription() {
        return "News, sports and television from Canada's national broadcaster";
    }

    @Override
    public String getImageUrl() {
        return mImageUrl;
    }

    @Override
    public String getBackgroundImageUrl() {
        return mBackgroundImageUrl;
    }

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

    @Override
    String getPreferredRegion() {
        return null;
    }

    @Override
    int getShieldLevel() {
        return 0;
    }

    @Override
    public ParentPuppet getParent() {
        return mParent;
    }

    @Override
    public SearchesPuppet getSearchProvider() {
        if (mSearchProvider == null && mSearchUrl != null) {
            mSearchProvider = new ScrapeUrlSearchesPuppet(this, mBaseUrl, mSearchUrl, mSearchUrl, "Search: " + mName, mImageUrl, mBackgroundImageUrl, mRelatedUrl, mCategoryItemSelectors, mSourcesItemScrapeUrlFormat, mSourcesItemScrapeUrlPattern, mSourcesItemSelector, mSourcesItemScrapeUrlSelector, mSourcesNameSelector, mSourcesCategorySelector, mSourcesDescriptionSelector, mSourcesDurationSelector, mSourcesPublicationDateSelector, mSourcesIsLiveSelector, mSourcesImageSelector, mSourcesBackgroundImageSelector, mSourcesRelatedScrapeUrlSelector);
        }
        return mSearchProvider;
    }

    @Override
    SettingsPuppet getSettingsProvider() {
        return null
    }

    @Override
    int getFastlaneBackgroundColor() {
        return 0xFFE21A21
    }

    @Override
    int getSearchAffordanceBackgroundColor() {
        return 0xFF000000
    }

    @Override
    int getSelectedBackgroundColor() {
        return 0xFFE21A21
    }

    @Override
    int getPlayerBackgroundColor() {
        return 0xFFE21A21
    }

    @Override
    List<Map<String, String>> getLiveChannelsMetaData() {
        return null
    }

    @Override
    public PuppetIterator getRelated() {
        if (mRelated == null && mRelatedUrl != null) {
            mRelated = new CBCPuppet(this, mBaseUrl, mSearchUrl, mRelatedUrl, "Related to: " + mName, mImageUrl, mBackgroundImageUrl, null, mCategoryItemSelectors, mSourcesItemScrapeUrlFormat, mSourcesItemScrapeUrlPattern, mSourcesItemSelector, mSourcesItemScrapeUrlSelector, mSourcesNameSelector, mSourcesCategorySelector, mSourcesDescriptionSelector, mSourcesDurationSelector, mSourcesPublicationDateSelector, mSourcesIsLiveSelector, mSourcesImageSelector, mSourcesBackgroundImageSelector, mSourcesRelatedScrapeUrlSelector).getChildren();
        }
        return mRelated;
    }

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

    @Override
    public PuppetIterator getChildren() {
        if (mChildren == null || mParent == null) {
            init();
        }
        return mChildren;
    }

    @Override
    boolean isTopLevel() {
        return mParent == null || mParent.getParent() == null
    }

    @Override
    int getVersionCode() {
        return VERSION_CODE
    }

    def void init() {
        try {
            mDocument = Jsoup.connect(mUrl).get();
            mChildren = new CBCPuppetIterator(mBaseUrl);
        } catch (Exception ex) {
            // Error scraping mLocation
        }
    }

    def void setUrl(String url) {
        mUrl = url;
        mChildren = null;
    }

    def class CBCPuppetIterator extends PuppetIterator {

        def int currentCategoryIteration = 0;
        def int currentSourcesIteration = 0;
        def List<Puppet> mPuppets = new ArrayList<Puppet>();

        def String mBaseUrl;

        transient def Elements categoryItems;
        transient def Elements sourcesItems;

        public CBCPuppetIterator(String baseUrl) {
            if (categoryItems == null || sourcesItems == null) {
                init();
            }
            mBaseUrl = baseUrl;
        }

        def void init() {
            if (mDocument == null) {
                CBCPuppet.this.init();
            }
            categoryItems = mDocument.select(mCategoryItemSelectors[0]);
            sourcesItems = mDocument.select(mSourcesItemSelector);
        }

        @Override
        public void add(Puppet puppet) {
            // pass
        }

        @Override
        public boolean hasNext() {
            if (categoryItems == null || sourcesItems == null) {
                init();
            }
            if ((currentCategoryIteration + currentSourcesIteration) < (categoryItems.size() + sourcesItems.size())) {
                return true;
            } else {
                // Reset for reuse
                currentCategoryIteration = 0;
                currentSourcesIteration = 0;
            }
            return false;
        }

        @Override
        public Puppet next() {
            boolean withinSources = currentSourcesIteration < sourcesItems.size();
            if ((currentCategoryIteration + currentSourcesIteration) >= mPuppets.size()) {
                if (withinSources) {
                    Element item = sourcesItems.get(currentSourcesIteration);

                    String scrapeUrl = item.select(mSourcesItemScrapeUrlSelector).toString();
                    if (mSourcesItemScrapeUrlFormat != null && mSourcesItemScrapeUrlPattern != null) {
                        try {
                            Pattern p = Pattern.compile(mSourcesItemScrapeUrlPattern);
                            Matcher m = p.matcher(scrapeUrl);
                            if (m.find()) {
                                scrapeUrl = mSourcesItemScrapeUrlFormat.replace("#1", m.group());
                            }
                        } catch (Exception ex) {
                            // Error scraping items from scrapeUrl
                        }
                    }

                    SourcesPuppet sourcesPuppet = new JsonContainedSMILSourcesPuppet();

                    sourcesPuppet.setParent(CBCPuppet.this);
                    sourcesPuppet.setUrl(scrapeUrl.startsWith("/") ? mBaseUrl + scrapeUrl : scrapeUrl);
                    String name = item.select(mSourcesNameSelector).text().trim();
                    sourcesPuppet.setName(name);
                    if (mSourcesCategorySelector != null) {
                        String category = item.select(mSourcesCategorySelector).text().trim();
                        if (category != "") {
                            sourcesPuppet.setCategory(category);
                            if (CBCPuppet.this.isTopLevel() && category != CBCPuppet.this.getName()) {
                                sourcesPuppet.setName(name + " - " + category);
                            }
                        }
                    }
                    sourcesPuppet.setShortDescription(item.select(mSourcesDescriptionSelector).text().trim());
                    sourcesPuppet.setDuration(convertDuration(item.select(mSourcesDurationSelector).text().trim()));
                    sourcesPuppet.setPublicationDate(item.select(mSourcesPublicationDateSelector).text().trim());
                    if (mSourcesIsLiveSelector != null) {
                        sourcesPuppet.setIsLive(item.select(mSourcesIsLiveSelector).size() > 0);
                    }
                    String imageUrl = item.select(mSourcesImageSelector).first().absUrl("src");
                    sourcesPuppet.setImageUrl(imageUrl);
                    String backgroundImageUrl = item.select(mSourcesBackgroundImageSelector).first().absUrl("src");
                    sourcesPuppet.setBackgroundImageUrl(backgroundImageUrl);
                    String relatedScrapeUrl = mSourcesRelatedScrapeUrlSelector != null ? item.select(mSourcesRelatedScrapeUrlSelector).attr("href") : null;
                    sourcesPuppet.setRelatedParent(new CBCPuppet(CBCPuppet.this, mBaseUrl, mSearchUrl, relatedScrapeUrl, mName, mImageUrl, mBackgroundImageUrl, null, mCategoryItemSelectors, mSourcesItemScrapeUrlFormat, mSourcesItemScrapeUrlPattern, mSourcesItemSelector, mSourcesItemScrapeUrlSelector, mSourcesNameSelector, mSourcesCategorySelector, mSourcesDescriptionSelector, mSourcesDurationSelector, mSourcesPublicationDateSelector, mSourcesIsLiveSelector, mSourcesImageSelector, mSourcesBackgroundImageSelector, mSourcesRelatedScrapeUrlSelector));
                    mPuppets.add(sourcesPuppet);
                } else {
                    Element item = categoryItems.get(currentCategoryIteration);
                    String url = item.attr("href");
                    String name = item.text().trim();

                    String[] categoryItemSelectors = mCategoryItemSelectors.length > 1 ? Arrays.copyOfRange(mCategoryItemSelectors, 1, mCategoryItemSelectors.length) : mCategoryItemSelectors;
                    mPuppets.add(new CBCPuppet(CBCPuppet.this, mBaseUrl, mSearchUrl, url, name, mImageUrl, mBackgroundImageUrl, null, categoryItemSelectors, mSourcesItemScrapeUrlFormat, mSourcesItemScrapeUrlPattern, mSourcesItemSelector, mSourcesItemScrapeUrlSelector, mSourcesNameSelector, mSourcesCategorySelector, mSourcesDescriptionSelector, mSourcesDurationSelector, mSourcesPublicationDateSelector, mSourcesIsLiveSelector, mSourcesImageSelector, mSourcesBackgroundImageSelector, mSourcesRelatedScrapeUrlSelector));
                }
            }
            Puppet curr = mPuppets.get(currentCategoryIteration + currentSourcesIteration);
            if (withinSources) {
                currentSourcesIteration++;
            } else {
                currentCategoryIteration++;
            }
            return curr;
        }

        @Override
        public void remove() {
            // pass
        }

        private static long convertDuration(String str) {  // HH:MM[:SS] to milliseconds
            if (str.equals("")) {
                return -1;
            }

            String[] data = str.split(":");

            int time;
            if (data.length > 2) {
                int hours  = Integer.parseInt(data[0]);
                int minutes = Integer.parseInt(data[1]);
                int seconds = Integer.parseInt(data[2]);
                time = seconds + 60 * minutes + 3600 * hours;
            } else if (data.length > 1) {
                int minutes = Integer.parseInt(data[0]);
                int seconds = Integer.parseInt(data[1]);
                time = seconds + 60 * minutes;
            } else {
                int seconds = Integer.parseInt(data[0]);
                time = seconds;
            }

            return TimeUnit.MILLISECONDS.convert(time, TimeUnit.SECONDS);
        }
    }

    public static class ScrapeUrlSearchesPuppet extends CBCPuppet implements SearchesPuppet {

        def String mSearchUrl;

        public ScrapeUrlSearchesPuppet(ParentPuppet parent, String baseUrl, String searchUrl, String url, String name, String imageUrl, String backgroundImageUrl, String relatedUrl, String[] categoryItemSelectors, String sourcesItemScrapeUrlFormat, String sourcesItemScrapeUrlPattern, String sourcesItemSelector, String sourcesItemScrapeUrlSelector, String sourcesNameSelector, String sourcesCategorySelector, String sourcesDescriptionSelector, String sourcesDurationSelector, String sourcesPublicationDateSelector, String sourcesIsLiveSelector, String sourcesImageSelector, String sourcesBackgroundImageSelector, String sourcesRelatedScrapeUrlSelector) {
            super(parent, baseUrl, searchUrl, url, name, imageUrl, backgroundImageUrl, relatedUrl, categoryItemSelectors, sourcesItemScrapeUrlFormat, sourcesItemScrapeUrlPattern, sourcesItemSelector, sourcesItemScrapeUrlSelector, sourcesNameSelector, sourcesCategorySelector, sourcesDescriptionSelector, sourcesDurationSelector, sourcesPublicationDateSelector, sourcesIsLiveSelector, sourcesImageSelector, sourcesBackgroundImageSelector, sourcesRelatedScrapeUrlSelector);
            mSearchUrl = searchUrl;
        }
        @Override
        public void setSearchQuery(String searchQuery) {
            setCategory(searchQuery);
            setUrl(mSearchUrl + searchQuery);
        }

        @Override
        boolean isTopLevel() {
            return false
        }
    }

    public static class JsonContainedSMILSourcesPuppet implements SourcesPuppet {

        def ParentPuppet mParent;
        def String mUrl;
        def String mName;
        def String mCategory;
        def String mShortDescription;
        def String mImageUrl;
        def String mBackgroundImageUrl;
        def String mPublicationDate;
        def long mDuration;
        def boolean mIsLive = false;
        def ParentPuppet mRelatedParent;

        def List<SubtitleDescription> mSubtitles = new ArrayList<SubtitleDescription>();

        public void setParent(ParentPuppet parent) {
            mParent = parent;
        }

        public void setUrl(String url) {
            mUrl = url;
        }

        public void setPublicationDate(String publicationDate) {
            mPublicationDate = publicationDate;
        }

        @Override
        public String getPublicationDate() {
            return mPublicationDate;
        }

        public void setDuration(long duration) {
            mDuration = duration;
        }

        @Override
        public long getDuration() {
            return mDuration;
        }

        @Override
        public SourceIterator getSources() {
            return new JsonContainedSMILSourceIterator();
        }

        public void setIsLive(boolean isLive) {
            mIsLive = isLive;
        }

        @Override
        public boolean isLive() {
            return mIsLive;
        }

        @Override
        public List<SubtitleDescription> getSubtitles() {
            return mSubtitles;
        }

        public void setName(String name) {
            mName = name;
        }

        @Override
        public String getName() {
            return mName;
        }

        public void setCategory(String category) {
            mCategory = category;
        }

        @Override
        public String getCategory() {
            return mCategory;
        }

        public void setShortDescription(String shortDescription) {
            mShortDescription = shortDescription;
        }

        @Override
        public String getShortDescription() {
            return mShortDescription;
        }

        public void setImageUrl(String imageUrl) {
            mImageUrl = imageUrl;
        }

        @Override
        public String getImageUrl() {
            return mImageUrl;
        }

        public void setBackgroundImageUrl(String backgroundImageUrl) {
            mBackgroundImageUrl = backgroundImageUrl;
        }

        @Override
        public String getBackgroundImageUrl() {
            return mBackgroundImageUrl;
        }

        @Override
        boolean isUnavailableIn(String region) {
            if (region == 'ca') {
                return false;
            }
            try {
                JSONObject entries = fetchJSON(mUrl).getJSONArray("entries").getJSONObject(0)
                JSONArray countries = entries.getJSONArray("countries");
                for (int i = 0; i < countries.length(); i++) {
                    if (region == countries.getString(i)) {
                        return entries.getBoolean("excludeCountries");
                    }
                }
                return !entries.getBoolean("excludeCountries");
            } catch (Exception ignore) {

            }
            return true;
        }

        @Override
        String getPreferredRegion() {
            return 'ca';
        }

        @Override
        int getShieldLevel() {
            return 0;
        }

        @Override
        public ParentPuppet getParent() {
            return mParent;
        }

        public void setRelatedParent(ParentPuppet relatedParent) {
            mRelatedParent = relatedParent;
        }

        @Override
        public PuppetIterator getRelated() {
            return mRelatedParent != null ? mRelatedParent.getChildren() : null;
        }

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

        def class JsonContainedSMILSourceIterator implements SourceIterator {

            def List<SourceDescription> mSources = new ArrayList<SourceDescription>();
            def transient JSONObject mJson;
            def transient JSONArray mContent;
            def int mNextSourceIndex = -1;

            @Override
            public boolean hasNext() {

                if (mUrl == null || mUrl.trim() == "") {
                    return false;
                } else if (mNextSourceIndex < 0) {
                    mJson = fetchJSON(mUrl);
                    try {
                        JSONObject entries = (JSONObject) mJson.getJSONArray("entries").get(0);
                        mContent = entries.getJSONArray("content");

                        for (int i = 0; mNextSourceIndex < 0; i++) {
                            SourceDescription currentSource = new SourceDescription();
                            try {
                                JSONObject entry = mContent.getJSONObject(i);

                                currentSource.url = entry.getString("url").split("/meta.smil")[0] + "?mbr=true&manifest=m3u";
                                try {
                                    currentSource.duration = (long) Float.parseFloat(entry.get("duration").toString()) * 1000;
                                    currentSource.height = entry.get("height").toString();
                                    currentSource.width = entry.get("width").toString();

                                    String previewUrl = currentSource.url.replace("?mbr=true&manifest=m3u", "?mbr=true&format=preview");
                                    JSONObject previewJson = fetchJSON(previewUrl);
                                    for (int j = 0; j < previewJson.getJSONArray("captions").length(); j++) {
                                        SubtitleDescription subtitle = new SubtitleDescription();
                                        subtitle.url =((JSONObject) previewJson.getJSONArray("captions").get(j)).getString("src").toString();
                                        mSubtitles.add(subtitle);
                                    }
                                } catch (Exception ex) {
                                    // Error parsing extra source values
                                }

                                Document document = Jsoup.connect(currentSource.url).ignoreContentType(true).get();

                                try {
                                    currentSource.url = document.select("video").first().attr("src");

                                    mSources.add(currentSource);
                                    mNextSourceIndex = 0;

                                    Collections.sort(mSources, new Comparator<SourceDescription>() {
                                        @Override
                                        public int compare(SourceDescription lhs, SourceDescription rhs) {
                                            // Reverse sort so higher quality is tried first
                                            return Long.compare(Long.parseLong(rhs.width), Long.parseLong(lhs.width));
                                        }
                                    });
                                } catch (Exception ex) {
                                    // No video, let's try audio
                                    currentSource.url = document.select("audio").first().attr("src");
                                    currentSource.isAudioOnly = true;
                                    mSources.add(currentSource);
                                    mNextSourceIndex = 0;
                                }
                            } catch (Exception ex) {
                                // Must be out of sources
                                Collections.sort(mSources, new Comparator<SourceDescription>() {
                                    @Override
                                    public int compare(SourceDescription lhs, SourceDescription rhs) {
                                        // Reverse sort so higher quality is tried first
                                        return Long.compare(Long.parseLong(rhs.width), Long.parseLong(lhs.width));
                                    }
                                });
                                mNextSourceIndex = 0;
                            }
                        }
                    } catch (Exception ex) {
                        // Oops, something went wrong, go with what sources we have
                        mNextSourceIndex = 0;
                    }
                }

                return mNextSourceIndex < mSources.size();
            }

            @Override
            public SourceDescription next() {
                mSources.get(mNextSourceIndex++);
            }

            @Override
            public void remove() {

            }
        }

        def static JSONObject fetchJSON(String link) {
            // Parse JSON from URL link
            BufferedReader reader = null;

            try {
                URL url = new URL(link);
                URLConnection urlConnection = url.openConnection();
                reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "iso-8859-1"));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                String json = sb.toString();
                return new JSONObject(json);
            } catch (Exception e) {
                // Failed to parse the JSON for media list
                return null;
            } finally {
                if (null != reader) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        // JSON feed closed
                    }
                }
            }
        }
    }
}