Skip to content

Commit b245af1

Browse files
committed
Update YouTube title
1 parent 09900bb commit b245af1

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Code
22

3-
[Google Apps Script](https://www.labnol.org/topic/google-apps-script/) - Code Snippets
3+
[Google Apps Script](https://www.labnol.org/topic/google-apps-script) - Code Snippets
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Update YouTube Title
2+
3+
This Google Script will automatically update the title of your YouTube video based on number of views and comments on the video.
4+
5+
It uses the YouTube API with Google Apps Script to setup a time trigger that runs every 5 minutes and updates the video title.
6+
7+
[Automatically Update YouTube Title](https://www.labnol.org/update-youtube-title-200818)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"timeZone": "Asia/Kolkata",
3+
"dependencies": {
4+
"enabledAdvancedServices": [
5+
{
6+
"userSymbol": "YouTube",
7+
"serviceId": "youtube",
8+
"version": "v3"
9+
}
10+
]
11+
},
12+
"exceptionLogging": "STACKDRIVER",
13+
"runtimeVersion": "V8"
14+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
This Google Script will auto-update the title of your
4+
YouTube video based on the number of views and comments
5+
6+
Tutorial: www.labnol.org/update-youtube-title-200818
7+
8+
Author: Amit Agarwal https://digitalinspiration.com/
9+
10+
Original Idea by Tom Scott youtu.be/BxV14h0kFs0
11+
12+
*/
13+
14+
const updateYouTubeVideo = (e = null) => {
15+
const id = '<<Video Id>>';
16+
const template = 'This video has VIEWCOUNT views and COMMENTCOUNT comments';
17+
18+
// The cron job is created only when the script is run manually
19+
if (e === null) {
20+
const triggerName = 'updateYouTubeVideo';
21+
const triggers = ScriptApp.getProjectTriggers().filter((trigger) => {
22+
return trigger.getHandlerFunction() === triggerName;
23+
});
24+
25+
// If time based trigger doesn't exist, create one that runs every 5 minutes
26+
if (triggers.length === 0) {
27+
ScriptApp.newTrigger(triggerName).timeBased().everyMinutes(5).create();
28+
}
29+
}
30+
31+
// Get the watch statistics of the video
32+
const { items: [video = {}] = [] } = YouTube.Videos.list('snippet,statistics', {
33+
id,
34+
});
35+
36+
// Parse the YouTube API response to get views and comment count
37+
const { snippet: { title: oldTitle, categoryId } = {}, statistics: { viewCount, commentCount } = {} } = video;
38+
39+
if (viewCount && commentCount) {
40+
const newTitle = template.replace('VIEWCOUNT', viewCount).replace('COMMENTCOUNT', commentCount);
41+
42+
// If the video title has not changed, skip this step
43+
if (oldTitle !== newTitle) {
44+
YouTube.Videos.update({ id: id, snippet: { title: newTitle, categoryId } }, 'snippet');
45+
}
46+
}
47+
};

0 commit comments

Comments
 (0)