Skip to content

Instantly share code, notes, and snippets.

@bisqwit
Created December 10, 2018 12:33
Show Gist options
  • Save bisqwit/5ec97cfe5517ab98dcb50ff7cfe43f1b to your computer and use it in GitHub Desktop.
Save bisqwit/5ec97cfe5517ab98dcb50ff7cfe43f1b to your computer and use it in GitHub Desktop.
<?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