

# /usr/bin/php5-cgi /tmp/umsp-plugins/youtube-subscriptions/youtube-subscriptions-helper.php
X-Powered-By: PHP/5.3.2-1
Content-type: text/html
0.1116480827331543#

# php5-cgi youtube-subscriptions-helper.php
X-Powered-By: PHP/5.3.2-1
Content-type: text/html
DBG: result of _getUser()
array(1) {
[0]=>
array(2) {
["youtube_username"]=>
string(14) "my.youtube.username"
["youtube_password"]=>
string(8) "12345678"
}
}
DBG: _get_youtube_feed: about to send to 74.125.39.118:
GET /feeds/api/users/my.youtube.username/subscriptions?v=2&start-index=1&max-results=1 HTTP/1.0
Host: gdata.youtube.com
Connection: Close
DBG: _get_youtube_feed: received:
<errors xmlns='http://schemas.google.com/g/2005'>
<error>
<domain>GData</domain>
<code>InvalidRequestUriException</code>
<internalReason>Missing or invalid username.</internalReason>
</error>
</errors>
DBG: User subscriptions for my.youtube.username
array(0) {
}
0.1109580993652344
#!/usr/bin/php5-cgi
<?php
// Youtube get subcriptions UMSP plugin by Dan
// http://forum.wdlxtv.com/viewtopic.php?f=49&t=713
//to enable debugging (goes to STDOUT) set $want_debug=1
//to disable debugging set $want_debug=0;
$want_debug=1;
define("LOG_FILE",'/tmp/umsp-log.txt');
set_time_limit(0);
$t = microtime(true);
main();
echo microtime(true) - $t;
function main()
{
global $want_debug; //we want to access the global variable $want_debug
$user = _getUser();
if($want_debug){
echo "DBG: result of _getUser()\n";
var_dump($user);
}
$user_data = array();
foreach ($user as $u) {
$us = _getUserSubscriptions($u['youtube_username'], $u['youtube_password']);
if($want_debug){
echo "DBG: User subscriptions for ".$u['youtube_username']."\n";
var_dump($us);
}
$t = _getUserVideos($us,20);
$user_data[ $u['youtube_username'] ]['new_subscription_videos'] = $t['videos'];
$user_data[ $u['youtube_username'] ]['subscriptions_videos'] = $t['user_videos'];
//remove subscriptions without any content
$user_with_videos = array_keys($t['user_videos']);
foreach($us as $k => $v) {
if (!in_array($v, $user_with_videos))
unset($us[ $k ]);
}
$user_data[ $u['youtube_username'] ]['subscriptions'] = $us;
}
file_put_contents('/tmp/youtube-subscriptions.cache',serialize($user_data));
}
function _getUserVideos(array $users, $count)
{
$videos = array();
$user_videos = array();
foreach($users as $user)
{
$t = _get_youtube_feed("74.125.39.118","gdata.youtube.com","/feeds/api/users/{$user}/uploads?orderby=published&max-results={$count}");
$x = simplexml_load_string($t);
if(!$x) {
continue;
}
$x->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$entries = $x->xpath("//a:entry");
foreach($entries as $t) {
$ts = strtotime(substr($t->published, 0, 10).' '.substr($t->published, 11, 8));
$vid = mb_substr($t->id,mb_strrpos($t->id,'/')+1,mb_strlen($t->id));
$d = array(
'user'=>$user,
'title'=>(string)$t->title,
'id'=>$vid,
#'video_link'=>"http://www.youtube.com/watch?v={$vid}&t=&fmt=22",
);
$user_videos[ $user ][] = $d;
$videos[ $ts ] = $d;
}
}
foreach($user_videos as $k=>$v) {
if (count($v) == 0) unset($user_videos[ $k ]);
}
krsort($videos);
$videos = array_slice($videos,0,150,true);
return array('videos'=>$videos, 'user_videos'=>$user_videos);
}
function _getUserSubscriptions($YouTubeUserName, $YouTubePassword)
{
preg_match("/openSearch:totalResults>(\d+)</s",_get_youtube_feed("74.125.39.118","gdata.youtube.com","/feeds/api/users/{$YouTubeUserName}/subscriptions?v=2&start-index=1&max-results=1"),$m);
$total_subscriptions = $m[1];
$subscriptions = array();
$pr = 20;
//get channels
for($i=0;$i<(int)ceil($total_subscriptions/$pr);$i++) {
$start = ($i*$pr) + 1;
$t = _get_youtube_feed("74.125.39.118","gdata.youtube.com","/feeds/api/users/{$YouTubeUserName}/subscriptions?v=2&start-index={$start}&max-results={$pr}");
$x = simplexml_load_string($t);
if(!$x) continue;
$x->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$t = $x->xpath("//a:entry//a:title");
foreach($t as $v) {
preg_match("/:(.+)$/",$v,$s);
$s = (string)trim($s[1]);
if (!empty($s)) $subscriptions[] = $s;
#break(2);
}
}
natcasesort($subscriptions);
return $subscriptions;
}
function _getUser()
{
$xml = simplexml_load_file('/conf/account_list.xml');
$data = $xml->xpath('//service[@name="YOUTUBE"]/account');
$accounts = array();
foreach($data as $account)
{
$accounts[] = array('youtube_username' => (string)$account->username,
'youtube_password' => (string)$account->password);
}
return $accounts;
}
/**
* Debug Logs
* stops if last param is 1
*/
function l()
{
$t = debug_backtrace();
$args = func_get_args();
ob_start();
echo basename($t[0]["file"]).":{$t[0]["line"]} > ";
var_dump($args);
$data = ob_get_contents();
ob_end_clean();
file_put_contents(LOG_FILE,$data,FILE_APPEND);
if(end($args) === 1) die;
}
function _get_youtube_feed($ip,$host,$path)
{
global $want_debug; //we want to access the global variable $want_debug
$fp = fsockopen($ip, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET {$path} HTTP/1.0\r\n";
$out .= "Host: {$host}\r\n";
$out .= "Connection: Close\r\n\r\n";
if($want_debug){
echo "DBG: _get_youtube_feed: about to send to $ip:\n$out";
}
fwrite($fp, $out);
$content = '';
$headerPassed = false;
while (!feof($fp)) {
$l = fgets($fp);
if($l == "\r\n") $headerPassed = true;
if($headerPassed) $content .= $l;
}
fclose($fp);
if($want_debug){
echo "DBG: _get_youtube_feed: received:\n$content\n";
}
preg_match("/(<\?.*>)/s",$content,$m);
return trim($m[1]);
}
}
?>







Users browsing this forum: No registered users and 5 guests