forked from mtakemoto/YTCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold-ytca.php
784 lines (734 loc) · 25.7 KB
/
old-ytca.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<?php
/*
* YouTube Captions Auditor (YTCA)
* version 1.0
*
*/
/*******************
* *
* CONFIGURATION *
* *
*******************/
// See the YouTube API reference for instructions on obtaining an API key
// https://developers.google.com/youtube/v3/docs/
$apiKey = file_get_contents('apikey');
// Title of report
$title = 'YouTube Caption Auditor (YTCA) Report';
// To include only relatively high traffic videos:
// 1. set $includeHighTraffic to true; set to false to skip this analysis
// 2. set $minViews to a positive integer
// If $minViews = 0, YTCA uses the mean # of views per channel as a traffic threshold
// Analyzing high traffic videos separately can help with prioritization if it isn't feasible to caption everything
$includeHighTraffic = true;
$minViews = 0;
// Optionally, the report can highlight channels that are either doing good or bad at captioning
// To use this feature, set the following variables
// if $includeHighlights = false, all other variables are ignored
$includeHighlights = true;
$goodPct = 50; // Percentages >= this value are "good"
$badPct = 0; // Percentages <= this value are "bad"
$goodColor = '#99FF99'; // light green
$badColor = '#FFD7D7'; // light red
// labels are added as a title attribute for the channel name
$goodLabel = 'Exemplary channel';
$badLabel = 'Needs work';
// Set $includeLinks to true to make channel names hyperlinks to the YouTube channel; otherwise set to false
$includeLinks = true;
// Set $includeChannelID to true to include a YouTube Channel ID column in the output; otherwise false
$includeChannelID = true;
//The list of channels is now held in the channels.ini file.
$channels = parse_ini_file('channels.ini', true);
/***********************
* *
* END CONFIGURATION *
* *
***********************/
error_reporting(E_ERROR | E_PARSE);
ini_set(max_execution_time,900); // in seconds; increase if script is timing out on large channels
showTop($title,$goodColor,$badColor);
$timeStart = microtime(true); // for benchmarking
$numChannels = sizeof($channels);
// It can take a long time to collect data for all videos within a channel
// Therefore this script only handles one channel at a time.
if ($numChannels > 0) {
fwrite(STDERR, "Collecting data from Youtube API..." . "\n");
for($c=0; $c< $numChannels; ++$c) {
/*if (!($c = $_POST['channel'])) {
$c = 0;
}*/
$channelId = $channels[$c]['id'];
if (!(ischannelId($channelId))) {
// this is not a valid channel ID; must be a username
$channelId = getChannelId($apiKey,$channelId);
}
$channelQuery = buildYouTubeQuery('search',$channelId,$apiKey);
$channel['name'] = $channels[$c]['name'];
$channel['id'] = $channelId;
$numKeys = sizeof($channels[$c]);
if ($numKeys > 2) {
// there is supplemental meta data in the array
$keys = array_keys($channels[$c]);
$i = 0;
while ($i < $numKeys) {
$key = $keys[$i];
if ($key !== 'name' && $key !== 'id') {
$metaKeys[] = $key;
}
$channel[$key] = $channels[$c][$key];
$i++;
}
}
if ($content = fileGetContents($channelQuery)) {
$json = json_decode($content,true);
$numVideos = $json['pageInfo']['totalResults'];
$channel['videoCount'] = $numVideos;
if ($numVideos > 0) {
$channel['videos'] = getVideos($channelId,$json,$numVideos,$apiKey);
}
else {
echo 'URL Error: '.$channelQuery."<br/>\n";
}
$i++;
}
else {
echo '<p class="error">Unable to retrieve file: ';
echo '<a href="'.$channelQuery.'">'.$channelQuery.'</a></p>'."\n";
}
showResults($c,$channel,$channels,$numChannels,$metaKeys,$includeHighlights,$goodPct,$badPct,$goodLabel,$badLabel,$includeHighTraffic,$minViews,$includeLinks,$includechannelId);
}
}
else {
echo 'There are no channels.<br/>';
}
// end benchmark and report result
$timeEnd = microtime(true);
$time = $timeEnd - $timeStart;
if ($c < ($numChannels-1)) {
// run time is only meaningful per channel.
// Don't show this in the final report.
echo '<p>Total run time: '.$time.' seconds</p>'."\n";
}
echo "</body>\n</html>";
function fileGetContents($url) {
// PHP fileGetContents() is resulting in intermittent errors
// Not sure why but curl seems to be more reliable
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function getChannelId($apiKey,$userName) {
$query = buildYouTubeQuery('channels', $userName, $apiKey);
if ($content = fileGetContents($query)) {
$json = json_decode($content,true);
$channelId = $json['items'][0]['id'];
if (isChannelId($channelId)) {
return $channelId;
}
}
return false;
}
function isChannelId($string) {
if (strlen(trim($string)) == 24) {
if (substr($string,0,2) == 'UC') {
return true;
}
}
return false;
}
function buildYouTubeQuery($which, $id, $apiKey, $nextPageToken=NULL) {
// $which is either 'search', 'channels', or 'videos'
// $id is a channel ID for 'search' queries; or a video ID for 'videos' query
// For 'channels' queries $id is a username
if ($which == 'search') {
// Cost = 100 units
$request = 'https://www.googleapis.com/youtube/v3/search?';
$request .= 'key='.$apiKey;
$request .= '&channelId='.$id;
$request .= '&part=id,snippet';
$request .= '&order=viewcount';
$request .= '&maxResults=50';
if ($nextPageToken) {
$request .= '&pageToken='.$nextPageToken;
}
}
elseif ($which == 'channels') {
// Cost = 5 units
// Cheaper than search, but doesn't include individual video data (not even ids)
// This is currently only used for looking up channel IDs
$request = 'https://www.googleapis.com/youtube/v3/channels?';
$request .= 'key='.$apiKey;
$request .= '&forUsername='.$id;
// $request .= '&id='.$id;
$request .= '&part=id';
$request .= '&maxResults=1';
}
elseif ($which == 'videos') {
// Cost = 5 units
$request = 'https://www.googleapis.com/youtube/v3/videos?';
$request .= 'key='.$apiKey;
$request .= '&id='.$id;
$request .= '&part=contentDetails,statistics';
$request .= '&maxResults=1';
}
return $request;
}
function getVideos($channelId,$json,$numVideos,$apiKey) {
$maxResults = 50; // as defined by YouTube API
if ($numVideos <= $maxResults) {
$numQueries = 1;
$finalBalance = $numVideos;
}
else {
// will need to query Google multiple times to get all data
$numQueries = ceil($numVideos/$maxResults);
$finalBalance = $numVideos % $maxResults;
}
$v=0; // index for videos array
$q=0; // index for queries
while ($q < $numQueries) {
if ($q == ($numQueries-1)) {
// this is the last query
// therefore it has fewer items
$finalIndex = $finalBalance;
}
else {
$finalIndex = $maxResults;
}
// get json data, if needed
if ($q > 0) {
// this is NOT the first query.
// Therefore we need to refresh $json with the next page of data
$nextPageToken = $json['nextPageToken'];
$channelQuery = buildYouTubeQuery('search',$channelId,$apiKey,$nextPageToken);
if ($content = fileGetContents($channelQuery)) {
$json = json_decode($content,true);
}
}
// now step through each item in the search query results, collecting data about each
$i=0;
while ($i < $finalIndex) {
$videoId = $json['items'][$i]['id']['videoId'];
$videos[$v]['id'] = $videoId;
$videos[$v]['title'] = $json['items'][$i]['snippet']['title'];
// now get additional data about this video via a 'videos' query
$videoQuery = buildYouTubeQuery('videos', $videoId, $apiKey);
$videos[$v]['query'] = $videoQuery; // added for debugging purposes
if ($videoContent = fileGetContents($videoQuery)) {
$videos[$v]['status'] = 'Success!'; // added for debugging purposes
$videoJson = json_decode($videoContent,true);
$videos[$v]['duration'] = $videoJson['items'][0]['contentDetails']['duration'];
$videos[$v]['captions'] = $videoJson['items'][0]['contentDetails']['caption']; // 'true' or 'false'
$videos[$v]['views'] = $videoJson['items'][0]['statistics']['viewCount'];
}
$v++;
$i++;
}
$q++;
}
return $videos;
}
function showResults($c,$channel,$channels,$numChannels,$metaKeys,$includeHighlights,$goodPct,$badPct,$goodLabel,$badLabel,$includeHighTraffic,$minViews,$includeLinks,$includechannelId) {
if ($numChannels > 0) {
echo "<table>\n";
echo "<tr>\n";
echo '<th scope="col">ID</th>'."\n";
echo '<th scope="col">YouTube Channel</th>'."\n";
if ($includechannelId) {
echo '<th scope="col">YouTube ID</th>'."\n";
}
$numMeta = sizeof($metaKeys);
if ($numMeta > 0) {
// there is supplemental meta data in the array
// display a column header for each meta data key
$i = 0;
while ($i < $numMeta) {
echo '<th scope="col">'.$metaKeys[$i]."</th>\n";
$i++;
}
}
echo '<th scope="col"># Videos</th>'."\n";
echo '<th scope="col">Duration</th>'."\n";
echo '<th scope="col"># Captioned</th>'."\n";
echo '<th scope="col">% Captioned</th>'."\n";
echo '<th scope="col">Mean # Views per Video</th>'."\n";
if ($includeHighTraffic) {
echo '<th scope="col"># Videos High Traffic</th>'."\n";
echo '<th scope="col"># Captioned High Traffic</th>'."\n";
echo '<th scope="col">% Captioned High Traffic</th>'."\n";
}
echo '<th scope="col">Duration Uncaptioned</th>'."\n";
if ($includeHighTraffic) {
echo '<th scope="col">Duration Uncaptioned High Traffic</th>'."\n";
}
echo "</tr>\n";
if ($resultsField = $_POST['results']) {
// this is not the first channel processed
// $results contains all previous data as comma-delimited text
$results = explode("\n",$resultsField);
$numResults = sizeof($results);
if ($numResults > 0) {
// populate table with previous results
$i=0;
$totalVideos = 0;
$totalDuration = 0;
$totalCaptioned = 0;
$totalDurationUncaptioned = 0;
if ($includeHighTraffic) {
$totalVideosHighTraffic = 0;
$totalCaptionedHighTraffic = 0;
$totalDurationUncaptionedHighTraffic = 0;
}
while ($i < $numResults) {
if (strlen($results[$i])>0) {
$resultsData = str_getcsv($results[$i]);
echo '<tr';
if ($includeHighlights) {
if ($resultsData[6] >= $goodPct) {
echo ' class="goodChannel">'."\n";
$channelTitle = ' title="'.$goodLabel.'"';
}
elseif ($resultsData[6] <= $badPct) {
echo ' class="badChannel">'."\n";
$channelTitle = ' title="'.$badLabel.'"';
}
else {
echo '>'."\n";
$channelTitle = NULL;
}
}
else {
echo '>'."\n";
$channelTitle = NULL;
}
echo '<td>'.$resultsData[0]."</td>\n"; // column number
echo '<td';
if ($channelTitle) {
echo $channelTitle;
}
echo '>';
if ($includeLinks) {
echo '<a href="https://www.youtube.com/channel/'.$resultsData[2].'">'; // channel name
}
echo $resultsData[1];
if ($includeLinks) {
echo '</a>';
}
echo "</td>\n";
if ($includechannelId) {
echo '<td>'.$resultsData[2]."</td>\n"; // channel id
}
// If channel included supplemental meta data, it was stored at end of $resultsData
if ($numMeta) {
if ($includeHighTraffic) {
$metaIndex = 13;
}
else {
$metaIndex = 9;
}
$j = 0;
while ($j < $numMeta) {
echo '<td>'.$resultsData[$metaIndex]."</td>\n";
$metaIndex++;
$j++;
}
}
echo '<td class="data">'.number_format($resultsData[3])."</td>\n"; // number of videos
$totalVideos += $resultsData[3];
echo '<td class="data">'.number_format($resultsData[4])."</td>\n"; // duration
$totalDuration += $resultsData[4];
echo '<td class="data">'.number_format($resultsData[5])."</td>\n"; // number captioned
$totalCaptioned += $resultsData[5];
echo '<td class="data">'.number_format($resultsData[6],1)."%</td>\n"; // percent captioned
echo '<td class="data">'.number_format($resultsData[7])."</td>\n"; // mean # of views
if ($includeHighTraffic) {
echo '<td class="data">'.number_format($resultsData[8])."</td>\n"; // # videos high traffic
$totalVideosHighTraffic += $resultsData[8];
echo '<td class="data">'.number_format($resultsData[9])."</td>\n"; // # captioned high traffic
$totalCaptionedHighTraffic += $resultsData[9];
echo '<td class="data">'.number_format($resultsData[10],1)."%</td>\n"; // % captioned high traffic
echo '<td class="data">'.number_format($resultsData[11])."</td>\n"; // duration uncaptioned
$totalDurationUncaptioned += $resultsData[11];
echo '<td class="data">'.number_format($resultsData[12])."</td>\n"; // duration uncaptioned high traffic
$totalDurationUncaptionedHighTraffic += $resultsData[12];
}
else {
echo '<td class="data">'.number_format($resultsData[8])."</td>\n"; // duration uncaptioned
$totalDurationUncaptioned += $resultsData[8];
}
echo "</tr>\n";
}
$i++;
}
}
}
// perform calculations for current channel
$videos = $channel['videos'];
$numVideos = sizeof($videos);
$totalVideos += $numVideos;
$duration = calcDuration($videos,$numVideos);
$totalDuration += $duration;
$numCaptioned = countCaptioned($videos,$numVideos);
$totalCaptioned += $numCaptioned;
$pctCaptioned = round($numCaptioned/$numVideos * 100,1);
$durationUncaptioned = calcDuration($videos,$numVideos,'false');
$totalDurationUncaptioned += $durationUncaptioned;
if ($includeHighTraffic) {
$totalDurationUncaptionedHighTraffic += $durationUncaptionedHighTraffic;
}
$avgViews = calcAvgViews($videos,$numVideos); // returns an integer (don't need high precision)
if ($includeHighTraffic) {
if ($minViews > 0) {
$highTrafficThreshold = $minViews;
}
else {
$highTrafficThreshold = $avgViews;
}
$numVideosHighTraffic = countHighTraffic($videos,$numVideos,$highTrafficThreshold);
$totalVideosHighTraffic += $numVideosHighTraffic;
$numCaptionedHighTraffic = countCaptioned($videos,$numVideos,$highTrafficThreshold);
$totalCaptionedHighTraffic += $numCaptionedHighTraffic;
$pctCaptionedHighTraffic = round($numCaptionedHighTraffic/$numVideosHighTraffic * 100,1);
$durationUncaptionedHighTraffic = calcDuration($videos,$numVideos,'false',$highTrafficThreshold);
$totalDurationUncaptionedHighTraffic += $durationUncaptionedHighTraffic;
}
// add current channel's data to the table
fwrite(STDERR, "Processing channel: " . $channel['name'] . "\n");
echo '<tr';
if ($includeHighlights) {
if ($pctCaptioned >= $goodPct) {
echo ' class="goodChannel">'."\n";
$channelTitle = ' title="'.$goodLabel.'"';
}
elseif ($pctCaptioned <= $badPct) {
echo ' class="badChannel">'."\n";
$channelTitle = ' title="'.$badLabel.'"';
}
else {
echo '>'."\n";
$channelTitle = NULL;
}
}
else {
echo '>'."\n";
$channelTitle = NULL;
}
if ($numResults) {
echo '<td>'.$numResults."</td>\n";
}
else { // this is the first channel
echo '<td>1</td>'."\n";
}
echo '<td';
if ($channelTitle) {
echo $channelTitle;
}
echo '>';
if ($includeLinks) {
echo '<a href="https://www.youtube.com/channel/'.$channel['id'].'">';
}
echo $channel['name'];
if ($includeLinks) {
echo '</a>';
}
echo "</td>\n";
if ($includechannelId) {
echo '<td>'.$channel['id']."</td>\n";
}
// Display supplemental meta data, if any exists in $channels array
if ($numMeta) {
$i = 0;
while ($i < $numMeta) {
$key = $metaKeys[$i];
echo '<td>'.$channel[$key].'</td>'."\n";
$i++;
}
}
echo '<td class="data">'.number_format($numVideos)."</td>\n";
echo '<td class="data">'.number_format($duration)."</td>\n";
echo '<td class="data">'.number_format($numCaptioned)."</td>\n";
echo '<td class="data">'.$pctCaptioned."%</td>\n";
echo '<td class="data">'.number_format($avgViews)."</td>\n";
if ($includeHighTraffic) {
echo '<td class="data">'.number_format($numVideosHighTraffic)."</td>\n";
echo '<td class="data">'.number_format($numCaptionedHighTraffic)."</td>\n";
echo '<td class="data">'.number_format($pctCaptionedHighTraffic,1)."%</td>\n";
}
echo '<td class="data">'.number_format($durationUncaptioned)."</td>\n";
if ($includeHighTraffic) {
echo '<td class="data">'.number_format($durationUncaptionedHighTraffic)."</td>\n";
}
echo "</tr>\n";
// add a totals row
echo '<tr class="totals">'."\n";
echo '<th scope="row" ';
if ($includechannelId) {
$colSpan = 3 + $numMeta;
}
else {
$colSpan = 2 + $numMeta;
}
echo 'colspan="'.$colSpan.'">TOTALS</th>'."\n";
echo '<td class="data">'.number_format($totalVideos)."</td>\n";
echo '<td class="data">'.number_format($totalDuration)."</td>\n";
echo '<td class="data">'.number_format($totalCaptioned)."</td>\n";
$totalPctCaptioned = round($totalCaptioned/$totalVideos * 100,1);
echo '<td class="data">'.$totalPctCaptioned."%</td>\n";
echo '<td class="data">--</td>'."\n"; // avg is only calculated per channel; no "total avg" needed
if ($includeHighTraffic) {
echo '<td class="data">'.number_format($totalVideosHighTraffic)."</td>\n";
echo '<td class="data">'.number_format($totalCaptionedHighTraffic)."</td>\n";
$totalPctCaptionedHighTraffic = round($totalCaptionedHighTraffic/$totalVideosHighTraffic * 100,1);
echo '<td class="data">'.$totalPctCaptionedHighTraffic."%</td>\n";
}
echo '<td class="data">'.number_format($totalDurationUncaptioned)."</td>\n";
if ($includeHighTraffic) {
echo '<td class="data">'.number_format($totalDurationUncaptionedHighTraffic)."</td>\n";
}
echo "</tr>\n";
echo "</table>\n";
if ($includeHighTraffic) {
echo '<p class="footnote">"<em>High traffic</em>" = greater than ';
if ($minViews > 0) {
echo $minViews.' views</p>'."\n";
}
else {
echo 'the mean number of views for this channel</p>'."\n";
}
}
/*if ($c < ($numChannels - 1)) {
// this is not the last channel
// append result to the cumulative results field
echo '<form action="#" method="POST">'."\n";
echo '<textarea name="results">'."\n";
echo $resultsField; // data from previous post
$rowNum = $c + 1;
echo $rowNum.',';
echo '"'.addslashes($channel['name']).'",';
echo '"'.addslashes($channel['id']).'",';
echo $numVideos.',';
echo $duration.',';
echo $numCaptioned.',';
echo $pctCaptioned.',';
echo $avgViews.',';
if ($includeHighTraffic) {
echo $numVideosHighTraffic.',';
echo $numCaptionedHighTraffic.',';
echo $pctCaptionedHighTraffic.',';
}
echo $durationUncaptioned;
if ($includeHighTraffic) {
echo ','.$durationUncaptionedHighTraffic;
}
// Add supplemental meta data to end, since the number of fields is unknown
// (makes for easier retrieval)
if ($numMeta) {
$i = 0;
while ($i < $numMeta) {
echo ',';
$key = $metaKeys[$i];
echo '"'.$channel[$key].'"';
$i++;
}
}
echo "\n</textarea>\n";
$nextIndex = $c+1;
$nextColNum = $nextIndex+1;
$nextChannel = 'Proceed to Channel '.$nextColNum.' of '.$numChannels.' (';
$nextChannel .= $channels[$nextIndex]['name'].')';
echo '<input type="hidden" name="channel" value="'.$nextIndex.'">'."\n";
echo '<input type="submit" value="'.$nextChannel.'">'."\n";
echo "</form>\n";
}
else {
// this *is* the last channel!
// could display a Success! message
}*/
}
}
function calcDuration($videos,$numVideos,$captioned=NULL,$viewThreshold=NULL) {
// returns total duration of all videos, in seconds
// if $captioned is 'true' or 'false', filter videos based on that value
// if $viewThreshold > 0, count only videos with views > that threshold
$i=0;
$seconds=0;
while ($i < $numVideos) {
$duration = NULL;
if ($captioned === 'true') {
if ($videos[$i]['captions'] === 'true') {
if ($viewThreshold) {
if ($videos[$i]['views'] > $viewThreshold) {
$duration = $videos[$i]['duration'];
}
}
else {
$duration = $videos[$i]['duration'];
}
}
}
elseif ($captioned === 'false') {
if ($videos[$i]['captions'] === 'false') {
if ($viewThreshold) {
if ($videos[$i]['views'] > $viewThreshold) {
$duration = $videos[$i]['duration'];
}
}
else {
$duration = $videos[$i]['duration'];
}
}
}
else { // include all videos, regardless of captions
if ($viewThreshold) {
if ($videos[$i]['views'] > $viewThreshold) {
$duration = $videos[$i]['duration'];
}
}
else {
$duration = $videos[$i]['duration'];
}
}
if ($duration) {
$seconds += convertToSeconds($duration);
}
$i++;
}
return $seconds;
}
function calcAvgViews($videos,$numVideos) {
// returns average number of views per video
$i=0;
$totalViews=0;
while ($i < $numVideos) {
$totalViews += $videos[$i]['views'];
$i++;
}
return round($totalViews/$numVideos);
}
function countHighTraffic ($videos,$numVideos,$threshold) {
// returns average number of views per video
// $threshold is the number of views above which a video is considered "high traffic"
$i=0;
$count=0;
while ($i < $numVideos) {
if ($videos[$i]['views'] > $threshold) {
$count++;
}
$i++;
}
return $count;
}
function convertToSeconds($duration) {
// $duration is in ISO 8601 format
// https://developers.google.com/youtube/v3/docs/videos#contentDetails.duration
// for videos < 1 hour: "PT#M#S"
// for videos >= 1 hour: "PT#H#M#S"
$interval = new DateInterval($duration);
return ($interval->h*3600)+($interval->i*60)+($interval->s);
}
function countCaptioned($videos,$numVideos,$viewThreshold=NULL) {
// if $viewThreshold > 0, count only videos with views > that threshold
$i=0;
$c=0;
while ($i < $numVideos) {
if ($videos[$i]['captions'] === 'true') {
if ($viewThreshold) {
if ($videos[$i]['views'] > $viewThreshold) {
$c++;
}
}
else {
$c++;
}
}
$i++;
}
return $c;
}
function showTop($title,$goodColor,$badColor) {
echo "<!DOCTYPE html>\n";
echo "<head>\n";
echo '<title>'.$title."</title>\n";
echo "<style>\n";
echo <<<END
body {
font-family: Arial, sans-serif;
font-size: 1em;
margin: 1em;
}
.date {
font-weight: bold;
font-size: 1.1em;
}
table {
background-color: black;
}
th, td {
background-color: white;
margin: 1px;
color: black;
padding: 1em;
}
th[scope="col"] {
vertical-align: bottom;
}
td.data {
text-align: right;
}
td a {
color: #474747;
text-decoration: underline;
}
tr.goodChannel th,
tr.goodChannel td {
background-color: $goodColor;
}
tr.badChannel th,
tr.badChannel td {
background-color: $badColor;
}
tr.totals td {
font-weight: bold;
}
input[type="submit"] {
margin: 1em;
font-size: 1em;
border: 1px solid #666;
border-radius: 5px;
padding: 0.15em;
background-color:#9F9
}
input[type="submit"]:hover,
input[type="submit"]:focus,
input[type="submit"]:active {
background-color: black;
border-color: #9F9;
color: white;
}
textarea {
display: none;
}
p.error {
font-weight: bold;
}
p.footnote {
margin-left: 2em;
}
END;
echo "</style>\n";
echo "</head>\n";
echo "<body>\n";
echo '<h1>'.$title."</h1>\n";
echo '<p class="date">'.date('M d, Y')."</p>\n";
}
function showArray($array) {
echo "<pre>\n";
var_dump($array);
echo "</pre>\n";
}
?>