Youtube api 获取最新上传缩略图 [英] Youtube api get latest upload thumbnail

查看:34
本文介绍了Youtube api 获取最新上传缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望返回从我的频道上传的最新视频的视频缩略图,并将其显示在我的网站上.

有人知道我如何通过 api 进行最小连接并仅获取缩略图吗?

谢谢!-汤姆

已修改!!

使用 Cakephp,我就是这样做的(感谢 dave 使用 zend 的建议);

控制器:

App::import('Xml');$channel = '空白电视';$url = 'https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads?v=2&max-results=1&orderby=published';$parsed_xml =&新的 XML($url);$parsed_xml = Set::reverse($parsed_xml);//调试($parsed_xml);$this->set('parsed_xml',$parsed_xml);

查看;

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

现在唯一剩下的就是以某种方式缓存提要调用..有什么建议吗???

-汤姆

这里有一种快速、肮脏的方法,无需真正接触 api.

我并不是建议这是最佳实践或任何其他方法,而且我确信有更聪明的方法,但它绝对适用于当前的 Youtube Feed 服务.

我的解决方案是使用 Zend Framework 中的 Zend_Feed_Reader 组件的 PHP,如果您需要手动设置,如果您不熟悉它,请告诉我.

基本上,您可以从 Zend.com 此处下载 1.11 版,然后确保框架文件可在您的 PHP 包含路径中访问.

如果您已经在 MVC 模式中使用 Zend Framework,则可以在您选择的控制器操作中执行此操作:

$channel = 'Blanktv';//将此更改为您的频道名称$url = 'https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads';$feed = Zend_Feed_Reader::import($url);$this->view->feed = $feed;

然后您可以在您的视图中执行此操作:

最新视频

<div><?php$i=0;foreach ($this->feed as $entry){$urlChop = 爆炸 ('http://gdata.youtube.com/feeds/api/videos/',$entry->getId());$videoId = 结束($urlChop);echo '<h3><a href="http://www.youtube.com/watch?v='. $videoId .'" target="_blank">'.$entry->getTitle() .'</a></h3>';echo '<p>上传于:'.$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) 中断;}?>

否则你可以这样做:

<h1>最新视频</h1><div><?php$i=0;foreach ($feed 作为 $entry){$urlChop = 爆炸 ('http://gdata.youtube.com/feeds/api/videos/',$entry->getId());$videoId = 结束($urlChop);echo '<h3><a href="http://www.youtube.com/watch?v='. $videoId .'" target="_blank">'.$entry->getTitle() .'</a></h3>';echo '<p>上传于:'.$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) 中断;}?>

使用后一种方法,您可能需要对 Zend_Feed_Reader 文件等使用 php require 语句......

希望这会有所帮助,就像我说的,如果您需要帮助,请告诉我.

一切顺利,

戴夫

更新:回应您对缓存的评论

Tom,这是另一个快速而肮脏的解决方案,它不使用缓存,但实施起来可能非常快.

我没有使用缓存组件的原因是因为我认为在这种情况下一个简单的数据库解决方案就足够了.我还认为必须拉取提要来比较它是否是新的对您来说不是最经济的.

您可以自动执行此过程以在指定时间自动运行,但如果您不想自动执行此过程并且不介意点击链接手动更新视频,则可以通过这种方式触发它.

我的解决方案再次基于 ZF,但是由于您可以使用 cakephp 将其破解为有用的东西,因此在这里做同样的事情应该没有问题.

首先建立一个新表(假设是一个 MySQL db):

CREATE TABLE `yourdbname`.`latestvid` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '唯一标识符',`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 '视频上传日期') ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;INSERT INTO `yourdbname`.`latestvid`(`id`, `videoId`, `videoTitle`, `uploadDate`) VALUES (NULL, '--', '--', '--');

这将为您的模板中使用的最新视频信息创建一个表格,但是由于显而易见的原因,我设置的默认值不适用于您的模板.

然后你可以做类似的事情:

公共函数updateAction(){$this->_helper->viewRenderer->setNoRender();//禁用视图$this->_helper->layout()->disableLayout();//禁用布局$user = '空白电视';//插入您的频道名称$url = 'https://gdata.youtube.com/feeds/api/users/'.$user.'/uploads';$feed = Zend_Feed_Reader::import($url);如果(!$饲料){die("无法访问提要");//注意:如果提要不可用,Zend 组件将显示错误,因此 ZF 不需要这样做}别的{$i=0;foreach ($feed 作为 $entry){$urlChop = 爆炸 ('http://gdata.youtube.com/feeds/api/videos/',$entry->getId());$videoId = 结束($urlChop);$videoTitle = $entry->getTitle();$uploadDate = $entry->getDateCreated();//使用您喜欢的方法更新 id = 1 的数据库记录$i++;if($i==1) 中断;}}}

也许试一试,让我知道你过得怎么样?

您只需要调整模板,以便从数据库而不是 Youtube 中获取变量,缩略图除外.

我想您总是可以采取进一步的方法并实际存储图像等,因为仍在从 Youtube 中提取缩略图并且可能会减慢速度.

您可以设置一个脚本来将缩略图复制到您自己的服务器并将路径存储在数据库中,或者如果您正在运行一系列需要标准品牌的视频,则使用标准缩略图 - 无论如何希望它有所帮助.

:-D

戴夫

I am looking to returning the video-thumbnail of the latest uploaded video from my channel, and display it on my website.

Anyone know how I can do a minimal connection trough api and get only the thumbnail?

Thanks! -Tom

REVISED!!

Using Cakephp, this is how I did it (thanks dave for 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 the only thing remaining is to cache the feed call someway.. Any suggestions???

-Tom

解决方案

here is a quick dirty way of doing it without really touching the api at all.

I'm not suggesting it's best practice or anything and I'm sure there are smarter ways but it definitely works with the current Youtube feed service.

My solution is PHP using the Zend_Feed_Reader component from Zend Framework, if you need a hand setting this up if you're not familiar with it let me know.

Essentially you can download version 1.11 from Zend.com here and then make sure the framework files are accessible on your PHP include path.

If you are already using Zend Framework in an MVC pattern you can do this in your chosen controller action:

$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;

Then you can 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'll likely need to use a php require statement for the Zend_Feed_Reader files etc....

Hope this helps, like I say 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 which doesn't use cache but may be very quick to implement.

The reason I didn't go with a caching component is because I figured a simple db solution would suffice under the circumstances. I also thought having to pull the feed to compare whether it was new or not wouldn't be the most economical for you.

You could automate this process to be run automatically at specified times but if you don't want to automate the process and don't mind clicking a link to update the video manually you could trigger it that way.

My solution is again based on ZF but since you were ok hacking it into something useful with cakephp you should have no problem doing the same here.

First set up a new table (assuming a 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 info for use in your template however the default values I've set up will not work with your template for obvious reasons.

You could then 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 have a go and let me know how you get on?

You'd just need to tweak the template so you'd get the variables from the database instead of Youtube with the exception of the thumbnail.

I suppose you could always take that approach further and actually store images etc since the thumbnail is still being pulled from Youtube and may slow things down.

You could set up a script to copy the thumbnail to your own server and store the path in the db or use a standard thumbnail if you are running a series of videos for which you require standard branding - anyway hope it helps.

:-D

Dave

这篇关于Youtube api 获取最新上传缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆