Created
December 10, 2018 12:33
-
-
Save bisqwit/5ec97cfe5517ab98dcb50ff7cfe43f1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* THIS SCRIPT EDITS *ALL* YOUTUBE VIDEOS BELONGING TO THE AUTHENTICATED USER, | |
* AND REPLACES http:// LINKS WITH https:// LINKS IN THE VIDEO DESCRIPTION. | |
* ONLY LINKS MATCHING A WHITELIST ARE MODIFIED (see preg_replace below). | |
* Copyright © 2018 Joel Yliluoma — https://iki.fi/bisqwit/ | |
* License: MIT | |
*/ | |
/* Note: You will need to create OAuth credentials at: https://console.developers.google.com/apis/credentials | |
* And you will need Google PHP API client library from: https://github.com/googleapis/google-api-php-client/releases | |
*/ | |
require_once 'vendor/autoload.php'; | |
$client = new Google_Client(); | |
$client->setApplicationName("Client_Library_Examples"); | |
$client->setDeveloperKey(file_get_contents('key.txt')); | |
$client->setAuthConfig('client_credentials.json'); | |
$client->setScopes('https://www.googleapis.com/auth/youtube'); | |
$service = new Google_Service_YouTube($client); | |
$client->setRedirectUri('http://127.0.0.1/'); | |
$client->authenticate(urldecode('put your auth code here')); | |
if(!$client->getAccessToken()) | |
{ | |
$url = $client->createAuthUrl(); | |
print "Go to this address. When done, copy the code= parameter from the return URL and paste it on the authenticate() line in this script.\n"; | |
print $url; | |
exit; | |
} | |
function UpdateDesc(&$obj) | |
{ | |
$desc = $obj->getDescription(); | |
$desc = preg_replace( | |
'@http://((?:www\\.)?(?:twitter|patreon|bisqwit\.iki\.fi|iki\.fi/bisqwit|youtube|youtu\.be|twitch|liberapay|fi\.liberapay|github|steamcommunity))@', | |
'https://\1', | |
$desc); | |
$obj->setDescription($desc); | |
} | |
function EditDescription($video_id) | |
{ | |
global $service; | |
$response = $service->videos->listVideos('snippet,localizations', Array('id' => $video_id)); | |
foreach($response->items as &$video) | |
{ | |
UpdateDesc($video->snippet); | |
$localiz = $video->getLocalizations(); | |
foreach($localiz as $lang => &$localization) | |
{ | |
UpdateDesc($localization); | |
} | |
$video->setLocalizations($localiz); | |
$resp = $service->videos->update('snippet,localizations', $video); | |
} | |
} | |
$params = Array('maxResults'=>50, 'forMine'=>true, 'type'=>'video'); | |
for(;;) | |
{ | |
$response = $service->search->listSearch('snippet', $params); | |
foreach($response->items as &$result) | |
{ | |
$id = $result->id->videoId; | |
print "Editing video {$id}\n"; | |
EditDescription($id); | |
} | |
if(!empty($response['nextPageToken'])) | |
{ | |
$params['pageToken'] = $response['nextPageToken']; | |
} | |
else | |
{ | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment