/** * @return JsonResponse * * @Route("/sync") */ public function syncAction() { $youtube = new Youtube(['key' => $this->getParameter('youtube_api_key')]); $videos = $youtube->getPlaylistItemsByPlaylistId($this->getParameter('youtube_playlist_id')); $em = $this->getDoctrine()->getManager(); foreach ($videos as $video) { $name = $video->snippet->title; $youtubeId = $video->snippet->resourceId->videoId; $description = $video->snippet->description; if (property_exists($video->snippet->thumbnails, 'maxres')) { $thumbnail = $video->snippet->thumbnails->maxres->url; } elseif (property_exists($video->snippet->thumbnails, 'high')) { $thumbnail = $video->snippet->thumbnails->high->url; } elseif (property_exists($video->snippet->thumbnails, 'medium')) { $thumbnail = $video->snippet->thumbnails->medium->url; } else { $thumbnail = $video->snippet->thumbnails->default->url; } $video = $em->getRepository('TGVideoBundle:Video')->findOneByYoutubeId($youtubeId); if ($video === null) { $video = new Video(); $video->setYoutubeId($youtubeId); $em->persist($video); } $video->setName($name); $video->setDescription($description); $video->setThumbnail($thumbnail); $video->setUrl('https://www.youtube.com/embed/' . $youtubeId); } $em->flush(); $videos = $this->getDoctrine()->getRepository('TGVideoBundle:Video')->findAll([], ['createdAt' => 'DESC']); $serializer = new Serializer([$this->get('tg_video.normalizer')]); return new JsonResponse(['videos' => $serializer->normalize($videos)]); }
public function init() { $config = $this->getConfig() + $this->defaults; if (empty($config['api_key'])) { throw new \Exception('Unable to locate google_api_key in bot configuration.'); } // Let's not flood a channel and limit results to 5 if ($config['result_limit'] > 5) { $config['result_limit'] = 5; } $this->bot->onChannel('/^!(?:yt|youtube)?\\s([\\w\\s]+)/i', function (Event $event) use($config) { $keywords = $event->getMatches(); $channel = $event->getRequest()->getSource(); $youtube = new Youtube(array('key' => $config['api_key'])); $results = $youtube->search($keywords[0], $config['result_limit']); if ($results) { $i = 1; foreach ($results as $video) { $message = sprintf('%s. %s - https://www.youtube.com/watch?v=%s', $i++, $video->snippet->title, $video->id->videoId); $event->addResponse(Response::msg($channel, $message)); } } else { $event->addResponse(Response::msg($channel, 'No results found.')); } }); }
/** * Create a Youtube media * * @param string $url * @return array */ public function create(string $url) : array { $config = $this->config; /* @service YoutubeDriver */ $service = new YoutubeDriver(['key' => $config['api_key']]); $videoId = $service->parseVIdFromURL($url); $video = $service->getVideoInfo($videoId); return (array) $video; }
public function youtubecronAction() { include_once 'vendor/autoload.php'; $vidModel = $this->getVideosModel(); $youtube = new Youtube(array('key' => $vidModel->api_key)); $playListArr = $youtube->getPlaylistItemsByPlaylistId($vidModel->playlist_id); $vidMapper = $this->getVideosMapper(); $vidEntArr = $vidModel->formatYoutubeResponse($playListArr); foreach ($vidEntArr as $key => $ent) { $vidMapper->saveVideo($ent); } }
/** * * @return type */ public function store(Request $request) { $this->validate($request, ['youtube_id' => 'required|unique:videos|max:20']); $videoId = Input::get('youtube_id'); // Process Video // Connect to youtube $youtubeKey = array("key" => "AIzaSyAmsWXPu2gP78el5mSftCeLLRCdvWfOHWU"); $youtube = new Youtube($youtubeKey); // Get video Info $videoInfo = $youtube->getVideoInfo($videoId); // dd($videoInfo->snippet); $user = Video::create(['youtube_id' => $videoId]); $data = ['video' => $videoInfo->snippet]; return Redirect::route('video.list', $data); }
/** * Test skipped for now, since the API returns Error 500 */ public function testNotFoundAPICall() { $vID = 'Utn7NBtbHL4'; //an deleted video $response = $this->youtube->getVideoInfo($vID); $this->assertFalse($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..."); }
/** * Because the normal getChannelByName/Id functions appear to make youtube give 500's, * I've got my own wrapper of those functions here. * @param $channel string The channel name/id * @param $isId boolean Is the first parameter a channel id? * */ protected function getChannel($channel, $isId) { $API_URL = $this->youtube->getApi('channels.list'); $params = ['part' => 'id,snippet,contentDetails']; if ($isId) { $params["id"] = $channel; } else { $params["forUsername"] = $channel; } $apiData = $this->youtube->api_get($API_URL, $params); return $this->youtube->decodeSingle($apiData); }
/** * GET /api/code/jwsearch/ * * @param YoutubeRequest $request * @return \StdClass */ public function getYoutubeSearchResults(YoutubeRequest $request) { $youtube = new Youtube(['key' => env('YOUTUBE_API_KEY', null)]); return $youtube->searchVideos($request->get('query'), 10); }
<div style="overflow: hidden"> <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) {
<?php require "vendor/autoload.php"; //require_once('vendor/fennb/phirehose/lib/Phirehose.php'); //require_once('vendor/fennb/phirehose/lib/OauthPhirehose.php'); use Madcoda\Youtube; include 'config.php'; //require_once("phpfastcache.php"); phpFastCache::setup("path", dirname(__FILE__) . '/cache'); // Path For Files // simple Caching with: $cache = phpFastCache(); // Try to get $content from Caching First // product_page is "identity keyword"; $content = $cache->get(PLAYLIST_ID); $content = null; if ($content == null) { $youtube = new Youtube(array('key' => YOUTUBE_API_KEY)); $content = $youtube->getPlaylistItemsByPlaylistId(PLAYLIST_ID); //$content = "DB QUERIES | FUNCTION_GET_PRODUCTS | ARRAY | STRING | OBJECTS"; // Write products to Cache in 10 minutes with same keyword $cache->set(PLAYLIST_ID, $content, CACHE_TIME); //echo "Used API <br><br>"; } else { //echo "Used Cache <br><br>"; } header('Access-Control-Allow-Origin: *'); header('Content-Type: application/json'); echo json_encode($content); //debug( $debug );
<?php require __DIR__ . '/vendor/autoload.php'; use Madcoda\Youtube; // API KEY $apikey = trim(file_get_contents(__DIR__ . '/APIKEY.txt')); $youtube = new Youtube(array('key' => $apikey)); // List: PLmTv5sSj3TpGueGhscXrWMxXNxvTr4iHY //$playlist = $youtube->getPlaylistById('PLmTv5sSj3TpGueGhscXrWMxXNxvTr4iHY'); $videos = $youtube->getPlaylistItemsByPlaylistId('PLmTv5sSj3TpGueGhscXrWMxXNxvTr4iHY'); foreach ($videos as $video) { echo $video->snippet->title . PHP_EOL; echo $video->snippet->thumbnails->medium->url . PHP_EOL; echo $video->contentDetails->videoId . PHP_EOL; } /* array(1) { [0] => class stdClass#5 (6) { public $kind => string(20) "youtube#playlistItem" public $etag => string(57) ""kuL0kDMAqRo3pU7O0pwlO-Lfzp4/V8mAxXVDSZ6VH01tWC6Bm274RMg"" public $id => string(45) "PLFJeS94nReeHSJnvhtU9l6pp8UlS4-BqhbBD08jw-xkQ" public $snippet => class stdClass#6 (9) { public $publishedAt => string(24) "2015-12-21T21:59:28.000Z" public $channelId => string(24) "UCTQ9lOLYm3tyBXzrKn4p1bA"