Skip to content

Commit

Permalink
feat(import): add new command to remove unneccesary request to move o…
Browse files Browse the repository at this point in the history
…bjets to series
  • Loading branch information
Yurujai committed Mar 22, 2024
1 parent 3ac7e9d commit f6673b2
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 20 deletions.
36 changes: 18 additions & 18 deletions Command/ImportVideosFromYouTubeChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,24 +325,24 @@ private function downloadThumbnail(Video $video, MultimediaObject $multimediaObj

private function obtainSeriesToSave(\Google_Service_YouTube $service, string $videoId): Series
{
$playlists = $this->documentManager->getRepository(Series::class)->findBy([
'properties.youtube_import_type' => 'playlist',
]);

foreach ($playlists as $playlist) {
$response = $service->playlistItems->listPlaylistItems('snippet', [
'playlistId' => $playlist->getProperty('youtube_import_id'),
'videoId' => $videoId,
]);

if (0 === count($response->getItems())) {
continue;
}

if (1 === count($response->getItems())) {
return $playlist;
}
}
// $playlists = $this->documentManager->getRepository(Series::class)->findBy([
// 'properties.youtube_import_type' => 'playlist',
// ]);
//
// foreach ($playlists as $playlist) {
// $response = $service->playlistItems->listPlaylistItems('snippet', [
// 'playlistId' => $playlist->getProperty('youtube_import_id'),
// 'videoId' => $videoId,
// ]);
//
// if (0 === count($response->getItems())) {
// continue;
// }
//
// if (1 === count($response->getItems())) {
// return $playlist;
// }
// }

return $this->defaultSeries();
}
Expand Down
155 changes: 155 additions & 0 deletions Command/ImportVideosOnPlaylistCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

declare(strict_types=1);

namespace Pumukit\YoutubeBundle\Command;

use Doctrine\ODM\MongoDB\DocumentManager;
use Google\Service\YouTube\ChannelListResponse;
use Pumukit\SchemaBundle\Document\MultimediaObject;
use Pumukit\SchemaBundle\Document\Series;
use Pumukit\SchemaBundle\Document\Tag;
use Pumukit\YoutubeBundle\Services\GoogleAccountService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class ImportVideosOnPlaylistCommand extends Command
{
private DocumentManager $documentManager;
private GoogleAccountService $googleAccountService;

private string $channelId;

public function __construct(
DocumentManager $documentManager,
GoogleAccountService $googleAccountService,
) {
$this->documentManager = $documentManager;
$this->googleAccountService = $googleAccountService;
parent::__construct();
}

protected function configure(): void
{
$this
->setName('pumukit:youtube:import:videos:on:playlist')
->addOption('account', null, InputOption::VALUE_REQUIRED, 'Account')
->addOption('channel', null, InputOption::VALUE_REQUIRED, 'Channel ID')
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'limit')
->setDescription('Import all videos on playlist')
->setHelp(
<<<'EOT'
Import all videos from Youtube channel
Limit is optional to test the command.
Usage: php bin/console pumukit:youtube:import:videos:on:playlist --account={ACCOUNT} --channel={CHANNEL_ID} --limit={LIMIT}

EOT
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$channel = $input->getOption('channel');

$youtubeAccount = $this->ensureYouTubeAccountExists($input);

$service = $this->googleAccountService->googleServiceFromAccount($youtubeAccount);
$this->channelId = $this->channelId($channel, $service);

$nextPageToken = null;
$count = 0;
$queryParams = [
'maxResults' => 50,
];

$seriesPlaylist = $this->documentManager->getRepository(Series::class)->findBy([
'properties.youtube_import_type' => ['$exists' => true],
'properties.youtube_import_playlist_channel' => $this->channelId,
]);

$progressBar = new ProgressBar($output, count($seriesPlaylist));
$progressBar->start();

foreach ($seriesPlaylist as $playlist) {
$progressBar->advance();

$queryParams['playlistId'] = $playlist->getProperty('youtube_import_id');

$response = $service->playlistItems->listPlaylistItems('snippet', $queryParams);
$nextPageToken = $response->getNextPageToken();

if (0 === count($response->getItems())) {
continue;
}

do {
if (null !== $input->getOption('limit') && $count >= $input->getOption('limit')) {
break;
}

if (null !== $nextPageToken) {
$queryParams['pageToken'] = $nextPageToken;
}

foreach ($response->getItems() as $item) {
$videoId = $item->getSnippet()->getResourceId()->getVideoId();

$multimediaObject = $this->documentManager->getRepository(MultimediaObject::class)->findOneBy([
'properties.youtube_import_id' => $videoId,
]);

if (!$multimediaObject instanceof MultimediaObject) {
continue;
}

$multimediaObject->setSeries($playlist);
if (0 == $count % 50) {
$this->documentManager->flush();
$this->documentManager->clear();
}
}
} while (null !== $nextPageToken);
}

$progressBar->finish();

$this->documentManager->flush();
$this->documentManager->clear();

return 0;
}

private function ensureYouTubeAccountExists(InputInterface $input): Tag
{
$youtubeAccount = $this->documentManager->getRepository(Tag::class)->findOneBy([
'properties.login' => $input->getOption('account'),
]);

if (!$youtubeAccount) {
throw new \Exception('Account not found');
}

return $youtubeAccount;
}

private function channelId(string $channel, \Google_Service_YouTube $service): string
{
$channels = $this->channelInfo($channel, $service);

return $channels->getItems()[0]->getId();
}

private function channelInfo(string $channel, \Google_Service_YouTube $service): ChannelListResponse
{
$queryParams = ['id' => $channel];

return $service->channels->listChannels('snippet', $queryParams);
}
}
15 changes: 13 additions & 2 deletions Resources/doc/ImportFromYoutube.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ where ACCOUNT is the name added for YouTube tag created on PuMuKIT and CHANNEL_I

Import videos metadata from YouTube

The command creates a new video on PuMuKIT for each video on YouTube and move it to the series created on step 1.
The command creates a new video on PuMuKIT for each video on YouTube on default series.

```
php bin/console pumukit:youtube:import:videos:from:channel --account={ACCOUNT} --channel={CHANNEL_ID}
Expand All @@ -39,7 +39,18 @@ where ACCOUNT is the name added for YouTube tag created on PuMuKIT and CHANNEL_I

You can use limit to test the download using optional parameter --limit={NUMBER_OF_VIDEOS}.

### 4. Generate jobs for downloaded videos

### 4. Move imported videos from imported series

This process will move imported videos from default series to imported series.

```
php bin/console pumukit:youtube:import:videos:on:playlist --account={ACCOUNT} --channel={CHANNEL_ID}
```

where ACCOUNT is the name added for YouTube tag created on PuMuKIT and CHANNEL_ID is the channel id of the YouTube channel.

### 5. Generate jobs for downloaded videos

This process will generate jobs for downloaded videos.

Expand Down

0 comments on commit f6673b2

Please sign in to comment.