Youtube api get latest thumbnail downloads

I want to return the thumbnail video of the last downloaded video from my channel and display it on my website.

Does anyone know how I can make a minimal connection via api and get only thumbnail?

Thanks! -Tom

REVISED !!

Using CakeFp, I did this (thanks dave for the suggestions using zend);

controller:

App::import('Xml'); $channel = 'Blanktv'; $url = 'https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads?v=2&max-results=1&orderby=published'; $parsed_xml =& new XML($url); $parsed_xml = Set::reverse($parsed_xml); //debug($parsed_xml); $this->set('parsed_xml',$parsed_xml); 

View

 $i=0; foreach ($parsed_xml as $entry) { echo '<a href="/videokanalen" target="_self"> <img width="220px" src="'.$entry['Entry']['Group']['Thumbnail'][1]['url'] .'"> </a>'; } 

Now it remains only to cache the channel call in some way .. Any suggestions ???

-Tom

+3
source share
1 answer

here is a quick dirty way to do this without even touching the api at all.

I do not suggest that this be best practice or something else, and I am sure there are more reasonable ways, but it definitely works with the current Youtube submission service.

My solution is PHP using the Zend_Feed_Reader component from the Zend Framework, if you need manual configuration, if you are not familiar with this, let me know.

Essentially, you can download version 1.11 from Zend.com here , and then make sure that the frame files are available in your path of including PHP.

If you are already using the Zend Framework in the MVC template, you can do this in the controller action of your choice:

 $channel = 'Blanktv'; //change this to your channel name $url = 'https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads'; $feed = Zend_Feed_Reader::import($url); $this->view->feed = $feed; 

You can then do this in your view:

 <h1>Latest Video</h1> <div> <?php $i=0; foreach ($this->feed as $entry) { $urlChop = explode ('http://gdata.youtube.com/feeds/api/videos/',$entry->getId()); $videoId = end($urlChop); echo '<h3><a href="http://www.youtube.com/watch?v=' . $videoId .'" target="_blank">' . $entry->getTitle() . '</a></h3>'; echo '<p>Uploaded on: '. $entry->getDateCreated() .'</p>'; echo '<a href="http://www.youtube.com/watch?v=' . $videoId .'" target="_blank"> <img src="http://img.youtube.com/vi/' . $videoId .'/hqdefault.jpg"> </a>'; $i++; if($i==1) break; } ?> </div> 

otherwise you can do:

 <?php $channel = 'Blanktv'; //change this to your channel $url = 'https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads'; $feed = Zend_Feed_Reader::import($url); ?> <h1>Latest Video</h1> <div> <?php $i=0; foreach ($feed as $entry) { $urlChop = explode ('http://gdata.youtube.com/feeds/api/videos/',$entry->getId()); $videoId = end($urlChop); echo '<h3><a href="http://www.youtube.com/watch?v=' . $videoId .'" target="_blank">' . $entry->getTitle() . '</a></h3>'; echo '<p>Uploaded on: '. $entry->getDateCreated() .'</p>'; echo '<a href="http://www.youtube.com/watch?v=' . $videoId .'" target="_blank"> <img src="http://img.youtube.com/vi/' . $videoId .'/hqdefault.jpg"> </a>'; $i++; if($i==1) break; } ?> </div> 

With the latter method, you will most likely need the php require statement for Zend_Feed_Reader files, etc.

Hope this helps, as I said, let me know if you need a hand.

All the best

Dave

UPDATE: In response to your comments about caching

Hi Tom, here is another quick and dirty solution that doesn't use cache, but can be implemented very quickly.

The reason I didn't deal with the caching component was because I decided that in these circumstances a simple solution for db is enough. I also thought that you need to pull the feed to compare whether it was new or not, would not be the most economical for you.

You can automate this process, which will start automatically at the specified time, but if you do not want to automate this process and are not averse to clicking the link to manually update the video, you can run it this way.

My solution is again based on ZF, but since you normally cracked it into something useful with cakephp, you shouldn't have a problem with that.

First set up a new table (assuming MySQL db):

 CREATE TABLE `yourdbname`.`latestvid` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique identifier', `videoId` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Video id', `videoTitle` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Video title', `uploadDate` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Video upload date' ) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci; INSERT INTO `yourdbname`.`latestvid` (`id`, `videoId`, `videoTitle`, `uploadDate`) VALUES (NULL, '--', '--', '--'); 

This will create a table for your latest video information for use in your template, however the default values ​​that I set will not work with your template for obvious reasons.

Then you can do something similar to this:

 public function updateAction() { $this->_helper->viewRenderer->setNoRender(); // disable view $this->_helper->layout()->disableLayout(); // disable layout $user = 'Blanktv'; // insert your channel name $url = 'https://gdata.youtube.com/feeds/api/users/'.$user.'/uploads'; $feed = Zend_Feed_Reader::import($url); if(!$feed) { die("couldn't access the feed"); // Note: the Zend component will display an error if the feed is not available so this wouldn't really be necessary for ZF } else { $i=0; foreach ($feed as $entry) { $urlChop = explode ('http://gdata.youtube.com/feeds/api/videos/',$entry->getId()); $videoId = end($urlChop); $videoTitle = $entry->getTitle(); $uploadDate = $entry->getDateCreated(); // use your preferred method to update the db record where the id = 1 $i++; if($i==1) break; } } } 

Maybe go and let me know how you are doing?

You just need to configure the template to get the variables from the database instead of Youtube, except for the thumbnail.

I assume that you can always use this approach and actually store images, etc., since the thumbnail is still pulled from Youtube and may slow down.

You can configure the script to copy the sketch to your own server and save the path in db or use the standard thumbnail if you use a series of videos that require standard branding - anyway, hope this helps.

: - D

Dave

+2
source

Source: https://habr.com/ru/post/1403333/


All Articles