Skip to content

Instantly share code, notes, and snippets.

@vasilvestre
Created June 3, 2020 17:00
Show Gist options
  • Save vasilvestre/eb20ee5fe058903209960dd4565e9859 to your computer and use it in GitHub Desktop.
Save vasilvestre/eb20ee5fe058903209960dd4565e9859 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use Elasticsearch\ClientBuilder;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
(new SingleCommandApplication())
->setCode(function (InputInterface $input, OutputInterface $output) {
$response = buildClientAndRequestStatistics();
$statisticOutput = getStatisticOutput($response);
$serializer = new Serializer([new ObjectNormalizer()],[]);
$client = ClientBuilder::create()->build();
$params = [
'index' => 'grp5',
'id' => md5(random_bytes(64)),
'body' => $serializer->normalize($statisticOutput)
];
$response = $client->index($params);
print_r($serializer->normalize($statisticOutput));
print_r($response);
})
->run();
function getStatisticOutput(Google_Service_YouTube_ChannelListResponse $response): StatisticOutput
{
$items = $response['items'][0]; // we only call hitoski channel so we take the first one
$statistics = $items['statistics'];
return (new StatisticOutput())
->setChannelId($items['id'])
->setKind($items['kind'])
->setCommentCount($statistics['commentCount'])
->setHiddenSubscriberCount($statistics['hiddenSubscriberCount'])
->setSubscriberCount($statistics['subscriberCount'])
->setVideoCount($statistics['videoCount'])
->setViewCount($statistics['viewCount']);
}
class StatisticOutput
{
private $channelId;
private $kind;
private $commentCount;
private $hiddenSubscriberCount;
private $subscriberCount;
private $videoCount;
private $viewCount;
public function getChannelId(): string
{
return $this->channelId;
}
public function setChannelId(string $channelId): StatisticOutput
{
$this->channelId = $channelId;
return $this;
}
public function getKind(): string
{
return $this->kind;
}
public function setKind(string $kind): StatisticOutput
{
$this->kind = $kind;
return $this;
}
public function getCommentCount(): int
{
return (int) $this->commentCount;
}
public function setCommentCount(string $commentCount): StatisticOutput
{
$this->commentCount = $commentCount;
return $this;
}
public function isHiddenSubscriberCount(): int
{
return (int) $this->hiddenSubscriberCount;
}
public function setHiddenSubscriberCount(bool $hiddenSubscriberCount): StatisticOutput
{
$this->hiddenSubscriberCount = $hiddenSubscriberCount;
return $this;
}
public function getSubscriberCount(): int
{
return (int) $this->subscriberCount;
}
public function setSubscriberCount(string $subscriberCount): StatisticOutput
{
$this->subscriberCount = $subscriberCount;
return $this;
}
public function getVideoCount(): int
{
return (int) $this->videoCount;
}
public function setVideoCount(string $videoCount): StatisticOutput
{
$this->videoCount = $videoCount;
return $this;
}
public function getViewCount(): int
{
return (int) $this->viewCount;
}
public function setViewCount(string $viewCount): StatisticOutput
{
$this->viewCount = $viewCount;
return $this;
}
}
function buildClientAndRequestStatistics(): Google_Service_YouTube_ChannelListResponse
{
$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setScopes([
'https://www.googleapis.com/auth/youtube.readonly',
]);
$client->setAuthConfig('YOUR_CLIENT_SECRET_FILE.json');
$client->setAccessType('offline');
$service = new Google_Service_YouTube($client);
return $service->channels->listChannels('statistics', ['id' => 'UCD4LxPA-ZR_0VXpPl_O-h0w']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment