/**
  * Gets all shows and store them in json.
  */
 public function updateShows()
 {
     $url = $this->builder->getAddictedShowsUrl();
     printf("Trying to get shows from [%s].\n", $url);
     $crawler = $this->client->request('GET', $url);
     $showsLinkAndName = $crawler->filter('table.tabel90 > tr > td > h3 > a')->extract(['_text', 'href']);
     $showsSeasons = $crawler->filter('table.tabel90 > tr > td.newsDate')->extract(['_text']);
     if (count($showsLinkAndName) != count($showsSeasons)) {
         throw new \Exception("Inconsistencies detected while updating shows.");
     }
     printf("Found [%s] shows.\n", count($showsLinkAndName));
     $shows = [];
     foreach ($showsLinkAndName as $n => $show) {
         $id = $this->extractShowId($show[1]);
         if ($this->nonEmptyShow($showsSeasons[$n])) {
             $name = Episode::sanitizeShowName($show[0]);
             // if multiple shows name reference the same id
             if (isset($this->mappedShowsNames()[$name])) {
                 foreach ($this->mappedShowsNames()[$name] as $name) {
                     $shows[$name] = $id;
                 }
             } else {
                 $shows[$name] = $id;
             }
         }
     }
     $this->io->saveShows($shows);
 }
 function testTagsFiltering()
 {
     $tags = "Hdtv-x264_randomTag.proper";
     $filtered = Episode::filterTags($tags);
     $this->assertContains('hdtv', $filtered);
     $this->assertContains('x264', $filtered);
     $this->assertContains('proper', $filtered);
     $this->assertNotContains('randomtag', $filtered);
 }
 /**
  * @param $episodeFilename string show file name
  * @param $download boolean download the file?
  *
  * @return null|string
  */
 public function findSubtitle($episodeFilename, $download)
 {
     $language = $this->config->getSubtitleLanguage();
     if (!isset($this->languages[$language])) {
         printf("Missing language [%s].\n", $language);
         return null;
     }
     $episode = new Episode($episodeFilename);
     if (!isset($this->shows[$episode->sanitizedShowName])) {
         printf("Missing show [%s].\n", $episode->showName);
         return null;
     }
     $languageId = $this->languages[$language];
     $showId = $this->shows[$episode->sanitizedShowName];
     $url = $this->builder->getAddictedShowAjaxUrl($showId, $episode->season, $languageId);
     printf("Trying to get subtitles from [%s].\n", $url);
     $crawler = $this->client->request('GET', $url);
     $matchingSubtitles = $crawler->filter('div#season > table > tbody > tr.epeven')->reduce(function (Crawler $node) use($episode) {
         $children = $node->children();
         $ep = $children->getNode(1)->nodeValue;
         $group = strtolower($children->getNode(4)->nodeValue);
         $status = strtolower($children->getNode(5)->nodeValue);
         return (int) $ep === (int) $episode->ep && $episode->inGroups($group) && strpos($status, '%') === false;
     });
     if ($matchingSubtitles->count() == 0) {
         printf("Missing subtitles for show [%s] season [%s] episode [%s] \n  and groups [%s].\n", $episode->showName, $episode->season, $episode->ep, implode(', ', $episode->groups));
         return null;
     }
     $chosenSubtitle = $matchingSubtitles->first();
     $downloadUri = $chosenSubtitle->children()->getNode(9)->firstChild->getAttribute('href');
     $url = $this->builder->getSubtitleUrl($downloadUri);
     if ($download === false) {
         printf("Chosen subtitle [%s].\n", $url);
         return null;
     }
     printf("Downloading subtitle [%s].\n", $url);
     $headers = $this->builder->getRequestHeaders($showId);
     return $this->client->getClient()->get($url, ['headers' => $headers])->getBody()->getContents();
 }