1

これを希望どおりに機能させるのに非常に苦労しています。プレイリストからすべてのビデオを取得しようとしています。現在は 20 件取得できますが、中には 100 件を超える動画を含むプレイリストもあります。これは私が問題を抱えているところです。考えられるすべてを使い果たしたので、ここで別のユーザーから見つけた次のコードを使用しています。

これにより、プロセスが開始されます。私がやろうとしていることについてGoogleの開発サイトには最小限の情報があるため、特定の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 本の動画。プルに失敗しました: 0 件の動画。挿入しようとしました: 0 件の重複動画。

ただし、これは 36 本の動画を含むプレイリストであるため、残りの動画にアクセスすることが問題です。これを行うための文書化されていない簡単な方法はありますか? どんな助けでも大歓迎です。

リクエスト URL で max-results 要素と start-index 要素を使用して、ループスルー時に必要な値まで増やしてみましたが、これは YouTube API からの XML 出力には影響しません。

どんな助けでも大歓迎です。

4

1 に答える 1

1

そこで、別のルートに進み、次のコードを使用することにしました。

<?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);
        }
    }

}

私が望むものではありませんが、YouTube API からの完全なデータを解析できるように、ビデオのすべての ID を返します。

于 2013-01-09T09:02:26.567 に答える