function getInfo()
{
    global $hash, $apikey;
    $restquery = "https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,status,id,snippet,contentDetails,contentOwnerDetails,statistics,topicDetails,invideoPromotion&id=" . $hash . "&key=" . $apikey;
    // example
    //$hash = "LLP2X3cVGXP0r56gOGhiRDlA";
    //$restquery = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=".$hash."&maxResults=50&key=".$apikey;
    $reply = doAPIRequest($restquery);
    /*
    echo '<pre>';
    print_r($reply->items[0]);
    echo '</pre>';
    */
    echo "<hr /><br />";
    echo '<table class="resulttable">';
    foreach ($reply->items[0] as $key => $var) {
        echo '<tr class="resulttable">';
        echo '<td class="resulttableHi"><b>' . $key . '</b></td>';
        if (gettype($var) != "object" && gettype($var2) != "array") {
            echo '<td class="resulttable">' . $var . '</td>';
        } else {
            echo '<td class="resulttable">';
            echo '<table style="display:inline">';
            foreach ($var as $key2 => $var2) {
                echo '<tr>';
                echo '<td><b>' . $key2 . '</b></td>';
                if (gettype($var2) != "object" && gettype($var2) != "array") {
                    echo '<td>' . $var2 . '</td>';
                } else {
                    echo '<td class="resulttable">';
                    echo '<table style="display:inline">';
                    foreach ($var2 as $key3 => $var3) {
                        echo '<tr>';
                        echo '<td><b>' . $key3 . '</b></td>';
                        if (gettype($var3) != "object" && gettype($var3) != "array") {
                            echo '<td>' . $var3 . '</td>';
                        } else {
                            echo '<td class="resulttable">';
                            echo '<table style="display:inline">';
                            foreach ($var3 as $key4 => $var4) {
                                echo '<tr>';
                                echo '<td><b>' . $key4 . '</b></td>';
                                if (gettype($var4) != "object" && gettype($var) != "array") {
                                    echo '<td>' . $var4 . '</td>';
                                } else {
                                    echo '<td>' . $var4 . '</td>';
                                }
                                echo '</tr>';
                            }
                            echo '</table>';
                            echo '</td>';
                        }
                        echo '</tr>';
                    }
                    echo '</table>';
                    echo '</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
}
function makeStatsFromIds($ids)
{
    global $apikey, $mode;
    $vids = array();
    $lookup = array();
    $categoryIds = array();
    echo "<br />Getting video details (" . count($ids) . "): ";
    for ($i = 0; $i < count($ids); $i++) {
        $vid = $ids[$i];
        $lookup[$vid] = $i;
        // este es el codigo que habrá que combinar. ¿Qué información es la que se pide en el módulo de los comentarios?
        // Específicamente, en la parte del módulo de los comentarios en los que se enumera qué usuarios hicieron qué comentarios...
        $restquery = "https://www.googleapis.com/youtube/v3/videos?part=statistics,contentDetails,snippet&id=" . $vid . "&key=" . $apikey;
        $reply = doAPIRequest($restquery);
        $vid = $reply->items[0];
        //print_r($vid); exit;
        // convert YT duraction format to seconds
        preg_match_all('/(\\d+)/', $vid->contentDetails->duration, $parts);
        $tmptime = array_reverse($parts[0]);
        $smulti = 1;
        $seconds = 0;
        for ($j = 0; $j < count($tmptime); $j++) {
            $seconds += $tmptime[$j] * $smulti;
            $smulti = $smulti * 60;
        }
        // collect categories
        if (!in_array($vid->snippet->categoryId, $categoryIds)) {
            $categoryIds[] = $vid->snippet->categoryId;
        }
        $row = array();
        $row["channelId"] = $vid->snippet->channelId;
        $row["channelTitle"] = $vid->snippet->channelTitle;
        $row["videoId"] = $vid->id;
        $row["publishedAt"] = $vid->snippet->publishedAt;
        $row["videoTitle"] = preg_replace("/\\s+/", " ", $vid->snippet->title);
        $row["videoDescription"] = preg_replace("/\\s+/", " ", $vid->snippet->description);
        $row["videoCategoryId"] = $vid->snippet->categoryId;
        $row["videoCategoryLabel"] = "";
        $row["duration"] = $vid->contentDetails->duration;
        $row["durationSec"] = $seconds;
        $row["dimension"] = $vid->contentDetails->dimension;
        $row["definition"] = $vid->contentDetails->definition;
        $row["caption"] = $vid->contentDetails->caption;
        $row["licensedContent"] = $vid->contentDetails->licensedContent;
        $row["viewCount"] = $vid->statistics->viewCount;
        $row["likeCount"] = $vid->statistics->likeCount;
        $row["dislikeCount"] = $vid->statistics->dislikeCount;
        $row["favoriteCount"] = $vid->statistics->favoriteCount;
        $row["commentCount"] = $vid->statistics->commentCount;
        $vids[] = $row;
        //print_r($row); exit;
        echo $i . " ";
        flush();
        ob_flush();
    }
    // get category labels and assign to videos
    $restquery = "https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=" . urlencode(implode(",", $categoryIds)) . "&key=" . $apikey;
    $reply = doAPIRequest($restquery);
    $categoryTrans = array();
    foreach ($reply->items as $cat) {
        $categoryTrans[$cat->id] = $cat->snippet->title;
    }
    for ($i = 0; $i < count($vids); $i++) {
        $vids[$i]["videoCategoryLabel"] = $categoryTrans[$vids[$i]["videoCategoryId"]];
    }
    // create TSV file
    $content_tsv = "position\t" . implode("\t", array_keys($vids[0])) . "\n";
    for ($i = 0; $i < count($vids); $i++) {
        $content_tsv .= $i + 1 . "\t" . implode("\t", $vids[$i]) . "\n";
    }
    $filename = "videolist_" . $mode . count($vids) . "_" . date("Y_m_d-H_i_s");
    if (isset($_POST["filename"])) {
        $filename = $_POST["filename"] . "_" . $filename;
    }
    file_put_contents("./data/" . $filename . ".tab", $content_tsv);
    echo '<br /><br />The script has created a file with  ' . count($vids) . ' rows.<br /><br />

	your files:<br />
	<a href="./data/' . $filename . '.tab">' . $filename . '.tab</a><br />';
}
function makeNetworkFromIds($depth)
{
    global $apikey, $nodes, $edges, $ids, $crawldepth;
    echo "<br /><br />getting details for " . count($ids) . " videos at depth " . $depth . ": ";
    $newids = array();
    $categoryIds = array();
    for ($i = 0; $i < count($ids); $i++) {
        $vid = $ids[$i];
        $restquery = "https://www.googleapis.com/youtube/v3/videos?part=statistics,contentDetails,snippet&id=" . $vid . "&key=" . $apikey;
        $reply = doAPIRequest($restquery);
        if (isset($reply->items[0])) {
            $video = $reply->items[0];
            // collect categories
            if (!in_array($video->snippet->categoryId, $categoryIds)) {
                $categoryIds[] = $video->snippet->categoryId;
            }
            $row = array();
            $row["channelId"] = $video->snippet->channelId;
            $row["channelTitle"] = preg_replace("/\\s+/", " ", $video->snippet->channelTitle);
            $row["videoId"] = $video->id;
            $row["publishedAt"] = strtotime($video->snippet->publishedAt);
            $row["videoTitle"] = preg_replace("/\\s+/", " ", $video->snippet->title);
            $row["videoDescription"] = preg_replace("/\\s+/", " ", $video->snippet->description);
            $row["videoCategoryId"] = $video->snippet->categoryId;
            $row["videoCategoryLabel"] = "";
            $row["duration"] = $video->contentDetails->duration;
            $row["dimension"] = $video->contentDetails->dimension;
            $row["definition"] = $video->contentDetails->definition;
            $row["caption"] = $video->contentDetails->caption;
            $row["licensedContent"] = $video->contentDetails->licensedContent;
            $row["viewCount"] = $video->statistics->viewCount;
            $row["likeCount"] = $video->statistics->likeCount;
            $row["dislikeCount"] = $video->statistics->dislikeCount;
            $row["favoriteCount"] = $video->statistics->favoriteCount;
            $row["commentCount"] = $video->statistics->commentCount;
            $nodes[$vid] = $row;
            if ($depth == 0) {
                $nodes[$vid]["isSeed"] = "yes";
                $nodes[$vid]["seedRank"] = $i + 1;
            } else {
                $nodes[$vid]["isSeed"] = "no";
                $nodes[$vid]["seedRank"] = "";
            }
        }
        echo $i . " ";
        flush();
        ob_flush();
    }
    // get category labels and assign to videos
    $restquery = "https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=" . urlencode(implode(",", $categoryIds)) . "&key=" . $apikey;
    $reply = doAPIRequest($restquery);
    $categoryTrans = array();
    foreach ($reply->items as $cat) {
        $categoryTrans[$cat->id] = $cat->snippet->title;
    }
    foreach ($nodes as $key => $node) {
        $nodes[$key]["videoCategoryLabel"] = $categoryTrans[$node["videoCategoryId"]];
    }
    echo "<br />getting related videos for " . count($ids) . " videos at depth " . $depth . ": ";
    for ($i = 0; $i < count($ids); $i++) {
        $vid = $ids[$i];
        // get related videos
        $run = true;
        $nextpagetoken = null;
        while ($run == true) {
            $restquery = "https://www.googleapis.com/youtube/v3/search?part=id&maxResults=50&relatedToVideoId=" . $vid . "&type=video&key=" . $apikey;
            if ($nextpagetoken != null) {
                $restquery .= "&pageToken=" . $nextpagetoken;
            }
            $reply = doAPIRequest($restquery);
            foreach ($reply->items as $item) {
                $featid = $item->id->videoId;
                if (!isset($nodes[$featid])) {
                    if (!in_array($featid, $newids)) {
                        $newids[] = $featid;
                    }
                    if ($depth < $crawldepth) {
                        $edgeid = $vid . "_|_|X|_|_" . $featid;
                        $edges[$edgeid] = true;
                    }
                } else {
                    $edgeid = $vid . "_|_|X|_|_" . $featid;
                    $edges[$edgeid] = true;
                }
            }
            if (isset($reply->nextPageToken) && $reply->nextPageToken != "") {
                $nextpagetoken = $reply->nextPageToken;
            } else {
                $run = false;
            }
        }
        echo $i . " ";
        flush();
        ob_flush();
    }
    if ($depth == $crawldepth) {
        //print_r($nodes); exit;
        renderNetwork();
    } else {
        //print_r($newids);
        $ids = $newids;
        $depth++;
        makeNetworkFromIds($depth);
    }
}
function getComments($videohash)
{
    global $apikey, $html, $filename;
    // get toplevel comments first
    $nextpagetoken = null;
    $run = true;
    $comments = array();
    echo "<br />getting comments: ";
    flush();
    ob_flush();
    while ($run == true) {
        $restquery = "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=100&videoId=" . $videohash . "&key=" . $apikey;
        if ($nextpagetoken != null) {
            $restquery .= "&pageToken=" . $nextpagetoken;
        }
        $reply = doAPIRequest($restquery);
        foreach ($reply->items as $item) {
            $comments[] = $item;
        }
        echo " " . count($comments);
        flush();
        ob_flush();
        if (isset($reply->nextPageToken) && $reply->nextPageToken != "") {
            $nextpagetoken = $reply->nextPageToken;
        } else {
            $run = false;
        }
    }
    // work through top level comments and get replies
    $nodecomments = array();
    $counter = 0;
    echo "<br /><br/>digging into thread structure: ";
    flush();
    ob_flush();
    foreach ($comments as $comment) {
        echo " " . $counter;
        $counter++;
        $tmp = array();
        $tmp["id"] = $comment->id;
        $tmp["replyCount"] = $comment->snippet->totalReplyCount;
        $tmp["likeCount"] = $comment->snippet->topLevelComment->snippet->likeCount;
        $tmp["publishedAt"] = date("Y-m-d H:i:s", strtotime($comment->snippet->topLevelComment->snippet->publishedAt));
        $tmp["authorName"] = preg_replace("/\\s+/", " ", $comment->snippet->topLevelComment->snippet->authorDisplayName);
        $tmp["text"] = preg_replace("/\\s+/", " ", $comment->snippet->topLevelComment->snippet->textDisplay);
        $tmp["authorChannelId"] = $comment->snippet->topLevelComment->snippet->authorChannelId->value;
        $tmp["authorChannelUrl"] = $comment->snippet->topLevelComment->snippet->authorChannelUrl;
        $tmp["isReply"] = 0;
        $tmp["isReplyTo"] = "";
        $tmp["isReplyToName"] = "";
        //print_r($tmp);
        $nodecomments[] = $tmp;
        if ($tmp["replyCount"] > 0) {
            $replies = array();
            $nextpagetoken = null;
            $run = true;
            while ($run == true) {
                $restquery = "https://www.googleapis.com/youtube/v3/comments?part=snippet&maxResults=100&parentId=" . $tmp["id"] . "&key=" . $apikey;
                if ($nextpagetoken != null) {
                    $restquery .= "&pageToken=" . $nextpagetoken;
                }
                $reply = doAPIRequest($restquery);
                foreach ($reply->items as $item) {
                    $replies[] = $item;
                }
                if (isset($reply->nextPageToken) && $reply->nextPageToken != "") {
                    $nextpagetoken = $reply->nextPageToken;
                } else {
                    $run = false;
                }
            }
            foreach ($replies as $reply) {
                $tmp2 = array();
                $tmp2["id"] = $reply->id;
                $tmp2["replyCount"] = "";
                $tmp2["likeCount"] = $reply->snippet->likeCount;
                $tmp2["publishedAt"] = date("Y-m-d H:i:s", strtotime($reply->snippet->publishedAt));
                $tmp2["authorName"] = preg_replace("/\\s+/", " ", $reply->snippet->authorDisplayName);
                $tmp2["text"] = preg_replace("/\\s+/", " ", $reply->snippet->textDisplay);
                $tmp2["authorChannelId"] = $reply->snippet->authorChannelId->value;
                $tmp2["authorChannelUrl"] = $reply->snippet->authorChannelUrl;
                $tmp2["isReply"] = 1;
                $tmp2["isReplyToId"] = $tmp["id"];
                $tmp2["isReplyToName"] = $tmp["authorName"];
                $nodecomments[] = $tmp2;
            }
        }
    }
    echo '<br /><br/>The script retrieved ' . count($nodecomments) . ' comments from ' . count($comments) . ' top level comments.';
    $content = implode("\t", array_keys($nodecomments[0])) . "\n";
    foreach ($nodecomments as $comment) {
        $content .= implode("\t", $comment) . "\n";
    }
    file_put_contents("./data/" . $filename . "_comments.tab", $content);
    return $nodecomments;
}
Esempio n. 5
0
    }
}
if ($mode == "daterange") {
    $i = 0;
    $date_start = strtotime($_GET["date_start"] . " 23:59:59");
    $date_end = strtotime($_GET["date_end"] . " 00:00:00");
    $before = $date_start;
    echo "Getting posts between timestamp " . $date_start . " and " . $date_end . ": ";
    flush();
    ob_flush();
    while ($before >= $date_end) {
        echo $i + 1 . " (" . $before . ") ";
        flush();
        ob_flush();
        $i++;
        $data = doAPIRequest($url . $before);
        $before = $data->response[count($data->response) - 1]->timestamp;
        foreach ($data->response as $response) {
            if ($response->timestamp >= $date_end) {
                $results[] = $response;
            }
        }
        sleep(0.5);
    }
}
// create graph and lists
$tags = array();
$edges = array();
$posts = array();
foreach ($results as $item) {
    //print_r($item);
function makeNetworkFromIds($depth)
{
    global $apikey, $nodes, $edges, $ids, $crawldepth, $subscriptions;
    echo "<br /><br />getting details for " . count($ids) . " channels at depth " . $depth . ": ";
    $newids = array();
    for ($i = 0; $i < count($ids); $i++) {
        $chid = $ids[$i];
        //$restquery = "https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,status,id,snippet,contentDetails,contentOwnerDetails,statistics,topicDetails,invideoPromotion&id=".$chid."&key=".$apikey;
        $restquery = "https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,id,snippet,statistics&id=" . $chid . "&key=" . $apikey;
        $reply = doAPIRequest($restquery);
        //print_r($reply);
        if (isset($reply->items[0])) {
            $nodes[$chid] = $reply->items[0];
            $nodes[$chid]->done = false;
            if ($depth == 0) {
                $nodes[$chid]->isSeed = "yes";
                $nodes[$chid]->seedRank = $i + 1;
            } else {
                $nodes[$chid]->isSeed = "no";
                $nodes[$chid]->seedRank = "";
            }
        }
        echo $i . " ";
        flush();
        ob_flush();
    }
    //print_r($nodes);
    if ($subscriptions == "on") {
        echo "<br />getting subscriptions for " . count($ids) . " channels at depth " . $depth . ": ";
        $counter = 0;
    }
    foreach ($nodes as $nodeid => $nodedata) {
        if (isset($nodedata->brandingSettings->channel->featuredChannelsUrls)) {
            foreach ($nodedata->brandingSettings->channel->featuredChannelsUrls as $featid) {
                if (!isset($nodes[$featid])) {
                    if (!in_array($featid, $newids)) {
                        $newids[] = $featid;
                    }
                    if ($depth < $crawldepth) {
                        $edgeid = $nodeid . "_|_|X|_|_" . $featid;
                        $edges[$edgeid] = true;
                    }
                } else {
                    $edgeid = $nodeid . "_|_|X|_|_" . $featid;
                    $edges[$edgeid] = true;
                }
            }
        }
        if ($subscriptions == "on" && $nodedata->done == false) {
            $run = true;
            $nextpagetoken = null;
            echo $counter . " ";
            flush();
            ob_flush();
            $counter++;
            while ($run == true) {
                $restquery = "https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&channelId=" . $nodedata->id . "&maxResults=50&key=" . $apikey;
                if ($nextpagetoken != null) {
                    $restquery .= "&pageToken=" . $nextpagetoken;
                }
                $reply = doAPIRequest($restquery);
                //print_r($reply); exit;
                foreach ($reply->items as $item) {
                    $featid = $item->snippet->resourceId->channelId;
                    //print_r($item);
                    if (!isset($nodes[$featid])) {
                        if (!in_array($featid, $newids)) {
                            $newids[] = $featid;
                        }
                        if ($depth < $crawldepth) {
                            $edgeid = $nodeid . "_|_|X|_|_" . $featid;
                            $edges[$edgeid] = true;
                        }
                    } else {
                        $edgeid = $nodeid . "_|_|X|_|_" . $featid;
                        $edges[$edgeid] = true;
                    }
                }
                if (isset($reply->nextPageToken) && $reply->nextPageToken != "") {
                    $nextpagetoken = $reply->nextPageToken;
                } else {
                    $run = false;
                }
            }
            $nodes[$nodeid]->done = true;
        }
        //print_r($newids);
    }
    if ($depth == $crawldepth) {
        renderNetwork();
    } else {
        $ids = $newids;
        $depth++;
        makeNetworkFromIds($depth);
    }
}