Esempio n. 1
0
 public function testGetChannelByName()
 {
     $response = $this->youtube->getChannelByName('Google');
     $this->assertEquals('youtube#channel', $response->kind);
     //This is not a safe Assertion because the name can change, but include it anyway
     $this->assertEquals('Google', $response->snippet->title);
     //add all these assertions here in case the api is changed,
     //we can detect it instantly
     $this->assertObjectHasAttribute('snippet', $response);
     $this->assertObjectHasAttribute('contentDetails', $response);
     $this->assertObjectHasAttribute('statistics', $response);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Direct access to the Container.
     /** @var Kengrabber $kg */
     $kg = $this->getApplication()->getContainer();
     $output->writeln("Starting with fetching the videos from channel: " . $kg['config']['youtube_channel_username']);
     $yt = new Youtube(array('key' => $kg['config']['youtube_api_key']));
     $channel = $yt->getChannelByName($kg['config']['youtube_channel_username']);
     // Setting channel data...
     $kg['option']->setOption('channel.title', $channel->snippet->title);
     $kg['option']->setOption('channel.description', $channel->snippet->description);
     $params = array('type' => 'video', 'channelId' => $channel->id, 'maxResults' => 15, 'order' => 'date', 'safeSearch' => 'none', 'part' => 'id, snippet');
     foreach ($kg['config']['youtube_queries'] as $query) {
         $params['q'] = $query;
         do {
             $search = $yt->searchAdvanced($params, true);
             if (is_array($search['results'])) {
                 foreach ($search['results'] as $v) {
                     $vid = $yt->getVideoInfo($v->id->videoId);
                     /** @var Video $video */
                     $video = $kg['video']->getVideoById($vid->id);
                     if ($video === null) {
                         $video = new Video();
                         $video->setId($vid->id);
                     }
                     $video->setTitle($vid->snippet->title);
                     $video->setDescription($vid->snippet->description);
                     $video->setPublished(new \DateTime($vid->snippet->publishedAt));
                     $kg['video']->saveVideo($video);
                 }
             }
             if (isset($search['info']['nextPageToken'])) {
                 $params['pageToken'] = $search['info']['nextPageToken'];
                 $output->writeln("Go to next page...");
             } else {
                 break;
             }
         } while (true);
     }
     $output->writeln("Finished crawling...");
 }
Esempio n. 3
0
 /**
  * Reads and processes the PMs
  */
 protected function readPMs()
 {
     $messages = $this->reddit->getComments("message/unread", 100);
     foreach ($messages as $message) {
         /* @var $message \RedditApiClient\Comment */
         try {
             if ($message->offsetGet("was_comment")) {
                 $this->debug("Ignored a comment reply from {$message->getAuthorName()}");
                 //mark as read
                 $this->reddit->sendRequest("POST", "http://www.reddit.com/api/read_message", array("id" => $message->getThingId(), "uh" => $this->reddit->modHash));
                 break;
             }
             $this->debug("Received a message!\n------------\n{$message->getBody()}\n------------");
             $this->debug("Processing message...");
             $this->debug("- Action: {$message->offsetGet("subject")}");
             $this->debug("- Parsing message...");
             $config = (object) Yaml::parse($message->getBody(), true, false);
             switch ($subject = strtolower($message->offsetGet("subject"))) {
                 case "list":
                     //validate existence of required fields
                     $this->debug("- Checking required fields...");
                     if (!isset($config->subreddit)) {
                         throw new \InvalidArgumentException("Required field 'subreddit' missing");
                     }
                     //validate subreddit
                     $this->debug("- Validating fields...");
                     $this->validate($config->subreddit, "subreddit");
                     //do mod check
                     $this->debug("- Checking if {$message->getAuthorName()} is mod of {$config->subreddit}...");
                     if (!$this->isMod($message->getAuthorName(), $config->subreddit)) {
                         throw new NotAModException("You're either not a mod, or your account doesn't have all permissions on this subreddit.");
                     }
                     //all checks done, let the magic begin
                     $reply = "Channel|Channel ID|Time added to CB|User who added it to CB|Last check by CB\n:---|:---|:---|:---|:---|\n";
                     $hits = 0;
                     foreach ($this->channels->getItems() as $channel) {
                         $channel = (object) $channel;
                         if (strtolower($channel->subreddit) == strtolower($config->subreddit)) {
                             $hits++;
                             $added = (new \TimeAgo())->inWords(date("Y-m-d H:i:s", $channel->register_date));
                             $lastcheck = round(time() - $channel->last_check);
                             $reply .= "{$channel->channel}|{$channel->channel_id}|{$added} ago|{$channel->user}|{$lastcheck} seconds ago\n";
                         }
                     }
                     if ($hits == 0) {
                         $reply = "Sorry, nothing found for that subreddit in particular. Perhaps [add some channels to it](http://www.reddit.com/r/ChannelBot/wiki/api)?";
                     }
                     $this->debug($reply);
                     $this->reddit->sendRequest("POST", "http://www.reddit.com/api/compose", array("api_type" => "json", "subject" => "Results", "text" => $reply, "to" => $message->getAuthorName(), "uh" => $this->reddit->modHash));
                     break;
                 case "add":
                     //validate existence of required fields
                     $this->debug("- Checking required fields...");
                     if (!isset($config->subreddit)) {
                         throw new \InvalidArgumentException("Required field 'subreddit' missing");
                     }
                     if (!isset($config->channel) && !isset($config->channel_id)) {
                         throw new \InvalidArgumentException("Please specify either 'channel' or 'channel_id'");
                     }
                     $this->debug("- Validating fields...");
                     //validate subreddit
                     $this->validate($config->subreddit, "subreddit");
                     if (isset($config->channel)) {
                         //validate channel
                         $this->validate($config->channel, "channel");
                     } else {
                         //validate channel_id
                         $this->validate($config->channel_id, "channel_id");
                     }
                     //do exist check
                     $this->debug("- Checking if channel/subredit combination already exists...");
                     if ($this->existsAlready($config->subreddit, !isset($config->channel) ? null : $config->channel, !isset($config->channel_id) ? null : $config->channel_id)) {
                         throw new ExistenceException("This channel/subreddit combination already exists.");
                     }
                     //do mod check
                     $this->debug("- Checking if {$message->getAuthorName()} is mod of {$config->subreddit}...");
                     if (!$this->isMod($message->getAuthorName(), $config->subreddit)) {
                         throw new NotAModException("You're either not a mod, or your account doesn't have all permissions on this subreddit.");
                     }
                     //do yt check
                     $this->debug("- Retrieving and processing channel data from YT API...");
                     $data = $this->getChannel(!isset($config->channel) ? $config->channel_id : $config->channel, isset($config->channel_id));
                     if (!$data) {
                         throw new InvalidChannelException("This channel doesn't exist.");
                     }
                     $config->channel_id = $data->id;
                     $config->channel = $data->snippet->title;
                     if (!isset($data->contentDetails->relatedPlaylists->uploads)) {
                         throw new InvalidChannelException("Channel was found, but unable to access the uploaded videos of the channel.");
                     }
                     $config->upload_playlist = $data->contentDetails->relatedPlaylists->uploads;
                     $reply = "Successfully added channel {$data->snippet->title}. It will now be monitored and new entries will be posted to /r/{$config->subreddit}.";
                     $this->channels->push(["channel" => isset($config->channel) ? $config->channel : null, "channel_id" => isset($config->channel_id) ? $config->channel_id : null, "subreddit" => $config->subreddit, "user" => $message->getAuthorName(), "register_date" => time(), "upload_playlist" => $config->upload_playlist]);
                     //tell console & user
                     $this->info($reply);
                     $this->reddit->sendRequest("POST", "http://www.reddit.com/api/compose", array("api_type" => "json", "subject" => "Success!", "text" => $reply, "to" => $message->getAuthorName(), "uh" => $this->reddit->modHash));
                     break;
                 case "remove":
                     //validate existence of required fields
                     $this->debug("- Checking required fields...");
                     if (!isset($config->subreddit)) {
                         throw new \InvalidArgumentException("Required field 'subreddit' missing");
                     }
                     if (!isset($config->channel) && !isset($config->channel_id)) {
                         throw new \InvalidArgumentException("Please specify either 'channel' or 'channel_id'");
                     }
                     $this->debug("- Validating fields...");
                     //validate subreddit
                     $this->validate($config->subreddit, "subreddit");
                     //validate channel (id)
                     if (!isset($config->channel_id)) {
                         $this->validate($config->channel, "channel");
                         $youtube = $this->youtube->getChannelByName($config->channel);
                         if (!$youtube) {
                             throw new InvalidChannelException("This channel doesn't exist");
                         }
                         $config->channel_id = $youtube->id;
                         $config->channel = $youtube->snippet->title;
                     } else {
                         $this->validate($config->channel_id, "channel_id");
                     }
                     //do exist check
                     $this->debug("- Checking if channel/subredit combination already exists...");
                     if (!$this->existsAlready($config->subreddit, null, $config->channel_id)) {
                         throw new ExistenceException("This channel/subreddit combination doesn't exist.");
                     }
                     //do mod check
                     $this->debug("- Checking if {$message->getAuthorName()} is mod of {$config->subreddit}...");
                     if (!$this->isMod($message->getAuthorName(), $config->subreddit)) {
                         throw new NotAModException("You're either not a mod, or your account doesn't have all permissions on this subreddit.");
                     }
                     //ok, all checks done, remove it
                     $this->channels->removeItem($this->getChannelIndex($config->subreddit, null, $config->channel_id));
                     $reply = "The channel with id '{$config->channel_id}', subreddit: {$config->subreddit} has been removed from the database.";
                     //tell console & user
                     $this->info($reply);
                     $this->reddit->sendRequest("POST", "http://www.reddit.com/api/compose", array("api_type" => "json", "subject" => "Success!", "text" => $reply, "to" => $message->getAuthorName(), "uh" => $this->reddit->modHash));
                     break;
                 default:
                     if (strpos($subject, "approved submitter") === false) {
                         throw new ActionNotSupportedException("Invalid subject. Subject needs to be one of the following:\n'add' OR 'remove'.");
                     }
             }
         } catch (\Exception $e) {
             $msg = method_exists($e, "getFullMessage") ? $e->getFullMessage() : $e->getMessage();
             $x = explode("\n", $msg);
             $emsg = "";
             foreach ($x as $m) {
                 $emsg .= "```\r\n{$m}\r\n```\r\n\r\n";
             }
             //throw error to console
             if (method_exists($e, "getFullMessage")) {
                 $this->error(get_class($e) . ": " . $e->getFullMessage());
             } else {
                 $this->error(get_class($e) . ": " . $e->getMessage());
             }
             //message user the bad news
             $this->reddit->sendRequest("POST", "http://www.reddit.com/api/compose", array("api_type" => "json", "subject" => get_class($e), "text" => $emsg, "to" => $message->getAuthorName(), "uh" => $this->reddit->modHash));
         }
         //mark as read
         $this->reddit->sendRequest("POST", "http://www.reddit.com/api/read_message", array("id" => $message->getThingId(), "uh" => $this->reddit->modHash));
     }
 }
Esempio n. 4
0
            <div class="pull-right">
                <a href="<?php 
echo Url::toRoute(['/news']);
?>
">Читать все материалы</a>
            </div>
        </div>
    </div>
    <div class="video-tv">
        <h3>Канал-ТВ</h3>
        <div class="well">
            <div class="row">
                <?php 
$key = 'AIzaSyAvDmtfH6P73IJzaV4bN0JyoJl--3Z4tc8';
$youtube = new Youtube(array('key' => $key));
$channel = $youtube->getChannelByName('fcbaltika');
//                var_dump($channel);
$playlist = $channel->contentDetails->relatedPlaylists->uploads;
$playlistItems = $youtube->getPlaylistItemsByPlaylistId($playlist);
$i = 0;
$playList = '';
$video = '';
foreach ($playlistItems as $item) {
    //                    echo $item->thumbnails->medium;
    //                    echo Html::img($item->snippet->thumbnails->medium->url, [
    //                        'width' => $item->snippet->thumbnails->default->width,
    //                        'height' => $item->snippet->thumbnails->default->height
    //                    ]);
    //                    var_dump($item->snippet->thumbnails);
    if ($i == 0) {
        $video = $item->snippet->resourceId->videoId;