Selaa lähdekoodia

Fix for YouTube daily query quota is too low (#1297)

CauseFX 6 vuotta sitten
vanhempi
commit
6fcf955ccc

+ 2 - 1
api/config/default.php

@@ -254,5 +254,6 @@ return array(
 	'unifiCookie' => '',
 	'homepageUnifiEnabled' => 'false',
 	'homepageUnifiAuth' => '1',
-	'homepageUnifiRefresh' => '600000'
+	'homepageUnifiRefresh' => '600000',
+	'youtubeAPI' => ''
 );

+ 17 - 0
api/functions/api-functions.php

@@ -1281,3 +1281,20 @@ function getSchema()
 		return 'DB not set yet...';
 	}
 }
+
+function youtubeSearch($query){
+	if(!$query){
+		return 'no query provided!';
+	}
+	$keys = array('AIzaSyBsdt8nLJRMTwOq5PY5A5GLZ2q7scgn01w','AIzaSyD-8SHutB60GCcSM8q_Fle38rJUV7ujd8k','AIzaSyBzOpVBT6VII-b-8gWD0MOEosGg4hyhCsQ');
+	$randomKeyIndex = array_rand($keys);
+	$key = $keys[$randomKeyIndex];
+	$apikey = ($GLOBALS['youtubeAPI'] !== '') ? $GLOBALS['youtubeAPI'] : $key;
+	$results = false;
+	$url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=$query+official+trailer&part=snippet&maxResults=1&type=video&videoDuration=short&key=$apikey";
+	$response = Requests::get($url);
+	if ($response->success) {
+		$results = json_decode($response->body, true);
+	}
+	return ($results) ? $results : false;
+}

+ 23 - 0
api/functions/homepage-functions.php

@@ -2455,6 +2455,29 @@ function getHomepageList()
 					),
 				)
 			)
+		),
+		array(
+			'name' => 'Misc',
+			'enabled' => true,
+			'image' => 'plugins/images/organizr/logo-no-border.png',
+			'category' => 'Custom',
+			'settings' => array(
+				'YouTube' => array(
+					array(
+						'type' => 'input',
+						'name' => 'youtubeAPI',
+						'label' => 'Youtube API Key',
+						'value' => $GLOBALS['youtubeAPI'],
+						'help' => 'Please make sure to input this API key as the organizr one gets limited'
+					),
+					array(
+						'type' => 'html',
+						'override' => 6,
+						'label' => 'Instructions',
+						'html' => '<a href="https://www.slickremix.com/docs/get-api-key-for-youtube/" target="_blank">Click here for instructions</a>'
+					),
+				)
+			)
 		)
 	);
 }

+ 14 - 0
api/index.php

@@ -1376,6 +1376,20 @@ switch ($function) {
 				break;
 		}
 		break;
+	case 'v1_youtube_search':
+		switch ($method) {
+			case 'GET':
+				$query = isset($_GET['q']) ? $_GET['q'] : false;
+				$result['status'] = isset($_GET['q']) ? 'success' : 'error';
+				$result['statusText'] = isset($_GET['q']) ? 'success' : 'missing query';
+				$result['data'] = youtubeSearch($query);
+				break;
+			default:
+				$result['status'] = 'error';
+				$result['statusText'] = 'The function requested is not defined for method: ' . $method;
+				break;
+		}
+		break;
 	default:
 		//No Function Available
 		$result['status'] = 'error';

+ 3 - 2
js/functions.js

@@ -6428,13 +6428,14 @@ function humanFileSize(bytes, si) {
 //youtube search
 function youtubeSearch(searchQuery) {
 	return $.ajax({
-		url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q="+searchQuery+"+official+trailer&part=snippet&maxResults=1&type=video&videoDuration=short&key=AIzaSyD-8SHutB60GCcSM8q_Fle38rJUV7ujd8k",
+		url: "api/?v1/youtube/search&q="+searchQuery,
 	});
 }
 function youtubeCheck(title,link){
 	youtubeSearch(title).success(function(data) {
+        var response = JSON.parse(data);
 		inlineLoad();
-		var id = data.items["0"].id.videoId;
+		var id = response.data.items["0"].id.videoId;
 		var div = `
 		<div id="player-`+link+`" data-plyr-provider="youtube" data-plyr-embed-id="`+id+`"></div>
 		<div class="clearfix"></div>