检索整个YouTube播放列表(所有视频)与YouTube Gdata API



我很难让它按照我想要的方式工作。我希望从播放列表中抓取所有视频。目前我可以检索20,但有一些播放列表,其中包含超过100个视频。这就是我的问题所在。我使用下面的代码,我发现从另一个用户在这里,因为我已经用尽了一切我能想到的。

这将启动该过程。请注意,我通过一个特定的URL调用XML提要,因为谷歌开发网站上的信息很少,我正在尝试做什么。

public function saveSpecificVideoFeed($id) {
    $url = 'https://gdata.youtube.com/feeds/api/playlists/' . $id . '?v=2';
    $feed = $this->yt->getPlaylistVideoFeed($url);
    $this->saveEntireFeed($feed, 1);
}

这是我将上面的函数传递给的:

public function saveEntireFeed($videoFeed, $counter) {
        foreach ($videoFeed as $videoEntry) {
            if (self::saveVideoEntry($videoEntry)) {
                $this->success++;
            } else {
                $this->failed++;
            }
            $counter++;
        }
        // See whether we have another set of results
        try {
            $videoFeed = $videoFeed->getNextFeed();
        } catch (Zend_Gdata_App_Exception $e) {
            echo $e->getMessage() . "<br/>";
            echo "Successfuly Pulled: <b>" . $this->success . "</b> Videos.<br/>";
            echo "Failed to Pull: <b>" . $this->failed . "</b> Videos.<br/>";
            echo "You Tryed to Insert: <b>" . $this->duplicate . "</b> Duplicate Videos.";
            return;
        }
        if ($videoFeed) {
            self::saveEntireFeed($videoFeed, $counter);
        }
    }

下面是我如何单独保存视频:

private function saveVideoEntry($videoEntry) {
        if (self::videoExists($videoEntry->getVideoId())) {
            // Do nothing if it exists
        } else {
            $videoThumbnails = $videoEntry->getVideoThumbnails();
            $thumbs = null;
            foreach ($videoThumbnails as $videoThumbnail) {
                $thumbs .= $videoThumbnail['url'] . ',';
            }
            $binds = array(
                'title' => $videoEntry->getVideoTitle(),
                'videoId' => $videoEntry->getVideoId(),
                'updated' => $videoEntry->getUpdated(),
                'description' => $videoEntry->getVideoDescription(),
                'category' => $videoEntry->getVideoCategory(),
                'tags' => implode(", ", $videoEntry->getVideoTags()),
                'watchPage' => $videoEntry->getVideoWatchPageUrl(),
                'flashPlayerUrl' => $videoEntry->getFlashPlayerUrl(),
                'duration' => $videoEntry->getVideoDuration(),
                'viewCount' => $videoEntry->getVideoViewCount(),
                'thumbnail' => $thumbs,
            );
            $sql = "INSERT INTO $this->tblName (title, videoId, updated, description, category, tags, watchPage, flashPlayerUrl, duration, viewCount, thumbnail) 
            VALUES (:title, :videoId, :updated, :description, :category, :tags, :watchPage, :flashPlayerUrl, :duration, :viewCount, :thumbnail)";
            $sth = $this->db->prepare($sql);
            foreach ($binds as $key => $value) {
                if ($value == null) {
                    $value = '';
                }
                $sth->bindValue(":{$key}", $value);
            }
            if ($sth->execute()) {
                return true;
            } else {
                print_r($sth->errorInfo());
                return false;
            }
        }
    }

这是我从浏览器输出中得到的,让我知道在一个容易阅读的格式,我从拉得到了什么:

表已经创建,继续提取。没有链接到下一集结果发现。成功拉出:20个视频。Failed to Pull: 0视频。您试图插入:0个重复的视频。

然而,这是一个播放列表与36个视频,所以我的问题是访问剩余的视频。有没有一种更简单的没有文档记录的方法来做到这一点?如有任何帮助,我将不胜感激。

我已经尝试在请求URL中使用max-results和start-index元素,并在循环时将它们增加到所需的值,但是这对来自YouTube API的XML输出没有影响。

所以我决定走一条不同的路线,并使用以下代码:

<?php
include('HttpCurl.php');
class YouTube {
    public $url;
    private $content;
    private $videoId = array();
    private $Http,$Doc;
    function __construct() {
        $this->Http = new HttpCurl();
        $this->Doc = new DOMDocument();
    }
    /*
     * Sets url to strip the videos from;
     * Insert the full URL for the videos YouTube playlist like:
     * http://www.youtube.com/watch?v=saVE7pMhaxk&list=EC6F914D0CF944737A
     */
    public function setUrl($url) {
        $this->url = $url;
        if ($this->check($this->url)) {
            $this->getPage();
        }
    }
    private function check($item) {
        if (isset($item)) {
            return true;
        } else {
            return false;
        }
    }
    /*
     * Grab the page that is needed
     */
    private function getPage() {
        $this->content = $this->Http->getContent($this->url);
        if($this->check($this->content)) {
            $this->getPlaylistVideos();
        }
    }
    /*
     * Parse page for desired result in our case this will default to the
     * playlist videos.
     */
    private function getPlaylistVideos() {
        $this->Doc->preserveWhiteSpace = false;
        // Load the url's contents into the DOM (the @ supresses any errors from invalid XML)
        if (@$this->Doc->loadHTML($this->content) == false) {
            die("Failed to load the document you specified.: " . $page);
        }
        $xpath = new DOMXPath($this->Doc);
        if ($hrefs = $xpath->query("//ol[@id='watch7-playlist-tray']//li")) {
            //echo "<br/>Grabbing Videos and playlists!";
            //Loop through each <a> and </a> tag in the dom and add it to the link array
            //$count = count($this->data['link']);
            foreach ($hrefs as $link) {
                $this->videoId[] = $link->getAttribute('data-video-id');
            }
            var_dump($this->videoId);
        }
    }
}

不完全是我想要的,但是返回所有视频的id,这样我就可以从YouTube API解析完整的数据

相关内容

  • 没有找到相关文章

最新更新