Пример #1
0
 /**
  * Creates the new series.
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  */
 private function syncSeries(InputInterface $input, OutputInterface $output)
 {
     //lists() method is deprecated, so we use pluck() instead.
     $allSeries = Watchlist::all()->pluck('tvmaze_id')->toArray();
     $currentSeries = Serie::all()->pluck('external_id')->toArray();
     $seriesToCreate = array_values(array_diff($allSeries, $currentSeries));
     //Loop through all series to create
     foreach ($seriesToCreate as $serieID) {
         //First let's fetch from TVMaze:
         $request = new Request('GET', 'http://api.tvmaze.com/shows/' . $serieID);
         $response = $this->client->send($request);
         if ($response->getStatusCode() != 200) {
             $output->writeln('An error has been occurred while fetching series with ID ' . $serieID);
             $output->writeln('Error code: ' . $response->getStatusCode());
             $output->writeln(self::separator);
             continue;
         }
         $data = json_decode($response->getBody()->getContents(), true);
         //Now let's create the TV show!
         $serie = new Serie();
         $serie->title = $data['name'];
         $serie->external_id = $data['id'];
         if (isset($data['image']['original'])) {
             $serie->image = $data['image']['original'];
         }
         $serie->premiered = date('Y-m-d H:i:s', strtotime($data['premiered']));
         $serie->save();
         $output->writeln($serie->title . ' created successfully!');
         $output->writeln(self::separator);
     }
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $group = $input->getOption('group');
     $show = $input->getOption('show');
     $link = $input->getOption('link');
     if (!$show && !$link) {
         $output->writeln('<fg=red>You must provide a show ID or link</>');
         return;
     }
     //Let's fetch the ID
     if ($show && !$link) {
         if (!is_numeric($show) || intval($show) != $show || intval($show) <= 0) {
             $output->writeln('<fg=red>Error: Invalid ID</>');
             return;
         } else {
             $showId = intval($show);
         }
     } else {
         $link = $input->getOption('link');
         $url = parse_url($link);
         if (!$url || $url['host'] != 'www.tvmaze.com') {
             $output->writeln('<fg=red>Error: invalid URL</>');
             return;
         }
         preg_match("/\\/shows\\/([0-9]+)\\//i", $url['path'], $matches);
         $showId = $matches[1];
     }
     $check = Watchlist::with('group')->whereHas('group', function ($q) use($group) {
         $q->where('title', $group);
     })->where('tvmaze_id', $showId)->first();
     if ($check) {
         $output->writeln('<fg=red>Error: The show you have provided already exists in the watchlist "' . $check->group->title . '"</>');
         return;
     }
     $groupData = WatchlistGroup::where('title', $group)->first();
     if (!$groupData) {
         $output->writeln('<fg=red>Error: Watchlist group not found</>');
         return;
     }
     $watchlist = new Watchlist();
     $watchlist->watchlist_group_id = $groupData->id;
     $watchlist->tvmaze_id = $showId;
     $watchlist->save();
     $output->writeln('<fg=green>Success: The show is successfully added to watchlist</>');
 }