适用于我的PHP代码的YouTube Data API v3

时间:2015-05-28 03:32:52

标签: php youtube-api

我需要更改以使此代码有效吗? 现在我有错误链接到youtube支持页面。 我想我的YouTube版本已经很老了。 此代码取自SocialEngine插件。

class rc_vidfeeder
{
  var $entries_per_page;
  var $cache_enable;
  var $cache_timeout;
  var $main_keyword;
  var $listing_keywords;

  var $gdata;

  function rc_vidfeeder()
  {
    $this->load_settings();
  }

  function load_settings()
  {
    global $setting;

    $this->entries_per_page = $setting['setting_vidfeeder_entries_per_page'];
    $this->entries_in_tagcloud = $setting['setting_vidfeeder_entries_in_tagcloud'];
    $this->cache_enable = $setting['setting_vidfeeder_xml_cache_enable'];
    $this->cache_timeout = $setting['setting_vidfeeder_xml_cache_timeout'];    
    $this->main_keyword = $setting['setting_vidfeeder_main_keyword'];

    $this->listing_keywords = array_map('trim', explode(',',$setting['setting_vidfeeder_listing_keywords']));   
  }

  function clear_cache()
  {
    $dirname = dirname(__FILE__);
    //rc_toolkit::debug($dirname);

    $dir = "$dirname/../vidfeeder_cache";
    if (!$dh = @opendir($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj=='.' || $obj=='..' || $obj=='index.php') continue;
        @unlink($dir.'/'.$obj);
    }

    closedir($dh);
  }

  function get_start_index($page)
  {
    if ($page < 1) $page = 1;
    $start_index = ($page-1) * $this->entries_per_page + 1;
    return $start_index;
  }

  function get_videos($params=array(), $page = 1)
  {
    global $setting;

    $base_params = array();
    $base_params['v'] = 1;
    $base_params['orderby'] = $setting['setting_vidfeeder_orderby'];
    $base_params['racy'] = 'exclude';
    $base_params['alt'] = 'atom';
    $base_params['format'] = 5;
    $base_params['start-index'] = $this->get_start_index($page);
    $base_params['max-results'] = $this->entries_per_page;

    $params = array_merge($base_params, $params);

    $str_params = array();
    foreach ($params as $k=>$v) {
      if ($k == 'time') continue;
      $str_params[] = "$k=".urlencode($v);
    }

    $url = "http://gdata.youtube.com/feeds/videos?" . join("&", $str_params);
    //rc_toolkit::debug($url,"vidfeeder:get_videos");

    $data = $this->cache_enable ? $this->get_xml_response_cache($url) : file_get_contents($url);

    $xml_parser = new rc_xml_parser();
    $this->gdata = $xml_parser->get_xml_tree($data);       

    $videos = array();

    foreach ($this->gdata['FEED'][0]['ENTRY'] as $k=>$data) {
      $videos[] = $this->yt_fetch_video($data);
    }

    return $videos;
  }


  function get_videos_by_yt_username($username, $page = 1)
  {
    $params = array();
    $params['author'] = $username;

    return $this->get_videos($params, $page);
  }


  function get_yt_username_by_user($user)
  {
    if (!isset($user->usersetting_info['usersetting_vidfeeder_yt_username'])) {
      $user->user_settings();
    }
    return $user->usersetting_info['usersetting_vidfeeder_yt_username'];
  }

  function get_videos_by_keyword($keyword, $page=1)
  {
      global $setting;

    $params = array();
    if ($keyword) {
      $params['vq'] = $keyword;
    }
    if ($setting['setting_vidfeeder_category']) {
        $params['category'] = $setting['setting_vidfeeder_category'];
    }
    if ($setting['setting_vidfeeder_restriction']) {
      $params['restriction'] = $setting['setting_vidfeeder_restriction'];
    }
    if ($setting['setting_vidfeeder_time']) {
      $params['time'] = $setting['setting_vidfeeder_time'];
    }
    if ($setting['setting_vidfeeder_author']) {
      $params['author'] = $setting['setting_vidfeeder_author'];
    }
    //if ($setting['setting_vidfeeder_location']) {
    //  $params['location'] = $setting['setting_vidfeeder_location'];
    //}
    if ($setting['setting_vidfeeder_language']) {
      $params['lr'] = $setting['setting_vidfeeder_language'];
    }
    return $this->get_videos($params,$page);
  }

  function get_xml_response_cache($url)
  {
    $cachefile = "./vidfeeder_cache/".md5($url).".txt";
    if (file_exists($cachefile) && ( (time()-filemtime($cachefile)) < $this->cache_timeout)) {
      $content = file_get_contents($cachefile);
    }
    else {
      $content = file_get_contents($url);
      rc_toolkit::write_to_file($cachefile,$content);
    }
    return $content;
  }


  function get_yt_user_info($username)
  {
    $url = "http://gdata.youtube.com/feeds/api/users/$username";
    $tdata = $this->load_data_url($url);

    $udata = null;
    if (is_array($tdata['ENTRY'][0])) {
      $d =& $tdata['ENTRY'][0];

      $udata['published'] = rc_toolkit::parse_rfc3339($d['PUBLISHED'][0]['VALUE']);
      $udata['updated'] = rc_toolkit::parse_rfc3339($d['UPDATED'][0]['VALUE']);
      $udata['name'] = $d['YT:FIRSTNAME'][0]['VALUE'];
      $udata['username'] = $d['YT:USERNAME'][0]['VALUE'];
      $udata['gender'] = $d['YT:GENDER'][0]['VALUE'];
      $udata['location'] = $d['YT:LOCATION'][0]['VALUE'];
      $udata['occupation'] = $d['YT:OCCUPATION'][0]['VALUE'];
      $udata['thumbnail'] = $d['MEDIA:THUMBNAIL'][0]['ATTRIBUTES']['URL'];

      $udata['viewcount'] = $d['YT:STATISTICS'][0]['ATTRIBUTES']['VIEWCOUNT'] ? $d['YT:STATISTICS'][0]['ATTRIBUTES']['VIEWCOUNT'] : 0;
      $udata['videowatchcount'] = $d['YT:STATISTICS'][0]['ATTRIBUTES']['VIDEOWATCHCOUNT'] ? $d['YT:STATISTICS'][0]['ATTRIBUTES']['VIDEOWATCHCOUNT'] : 0;
      $udata['subscribercount'] = $d['YT:STATISTICS'][0]['ATTRIBUTES']['SUBSCRIBERCOUNT'] ? $d['YT:STATISTICS'][0]['ATTRIBUTES']['SUBSCRIBERCOUNT'] : 0;
      $udata['lastwebaccess'] = rc_toolkit::parse_rfc3339($d['YT:STATISTICS'][0]['ATTRIBUTES']['LASTWEBACCESS']);
    }

    return $udata;
  }

  function load_data_url($url)
  {
    $data = $this->cache_enable ? $this->get_xml_response_cache($url) : file_get_contents($url);
    $xml_parser = new rc_xml_parser();
    $pdata = $xml_parser->get_xml_tree($data);
    return $pdata;
  }


  function get_total_entries()
  {
    return (int) $this->gdata['FEED'][0]['OPENSEARCH:TOTALRESULTS'][0]['VALUE'];
  }

  function get_video($id)
  {
    $url = "http://gdata.youtube.com/feeds/videos/$id";
    $this->gdata = $this->load_data_url($url);
    $video = $this->yt_fetch_video($this->gdata['ENTRY'][0]);

    return $video;
  }

    function yt_fetch_video($vid)
    {
        if( ! $vid )
            return false;

        $guid = explode( "/" , $vid['ID'][0]['VALUE'] );
        $id = $guid[ count($guid) - 1 ];
        $tags = array();
        foreach( $vid['CATEGORY'] as $tag ) {
            if( ereg( "keywords" , $tag['ATTRIBUTES']['SCHEME'] ) )
                array_push( $tags , $tag['ATTRIBUTES']['TERM'] );
        }

        $views = $r_avg = $r_count = 0;

        if( isset($vid['GD:RATING']) ) {
            $r_avg = $vid['GD:RATING'][0]['ATTRIBUTES']['AVERAGE'];
            $r_count = $vid['GD:RATING'][0]['ATTRIBUTES']['NUMRATERS'];
        }

        if( isset($vid['YT:STATISTICS']) )
            $views = $vid['YT:STATISTICS'][0]['ATTRIBUTES']['VIEWCOUNT'];

        return array(
            "id" => $id,
            "author" => $vid['AUTHOR'][0]['NAME'][0]['VALUE'],
            "upload_time" => rc_toolkit::parse_rfc3339($vid['PUBLISHED'][0]['VALUE']),
            "tags" => $tags,
            "title" => $vid['TITLE'][0]['VALUE'],
            "content" => $vid['CONTENT'][0]['VALUE'],
            "description" => $vid['MEDIA:GROUP'][0]['MEDIA:DESCRIPTION'][0]['VALUE'],
            "duration" => $vid['MEDIA:GROUP'][0]['YT:DURATION'][0]['ATTRIBUTES']['SECONDS'],
            "view_count" => $views,
            "rating_avg" => $r_avg,
            "rating_count" => $r_count,
            "thumbnail_url" => $vid['MEDIA:GROUP'][0]['MEDIA:THUMBNAIL'][0]['ATTRIBUTES']['URL']
        );
    }

}

class rc_vidfeeder_tag extends rc_tagcloud
{
  var $table = 'se_vidfeedertags';

}

0 个答案:

没有答案