-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_yt_title.py
112 lines (91 loc) · 3.64 KB
/
update_yt_title.py
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
import os
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
def get_authenticated_service():
# Authenticate using the OAuth credentials and refresh token
creds = Credentials.from_authorized_user_info(
{
"client_id": os.environ["YT_CLIENT_ID"],
"client_secret": os.environ["YT_CLIENT_SECRET"],
"refresh_token": os.environ["YT_REFRESH_TOKEN"],
"token_uri": "https://oauth2.googleapis.com/token",
}
)
return build("youtube", "v3", credentials=creds)
def get_view_count(youtube, video_id):
# Get the video statistics
try:
request = youtube.videos().list(part="statistics", id=video_id)
response = request.execute()
if not response["items"]:
print("No video found.")
return None
video = response["items"][0]
view_count = video["statistics"]["viewCount"]
return view_count
except Exception as e:
print(f"Error retrieving view count: {e}")
return None
def get_video_details(youtube, video_id):
# Get the video details (including title and categoryId)
try:
request = youtube.videos().list(part="snippet", id=video_id)
response = request.execute()
if not response["items"]:
print("No video found.")
return None, None, None
video = response["items"][0]
title = video["snippet"]["title"]
category_id = video["snippet"]["categoryId"]
description = video["snippet"]["description"]
return title, category_id, description
except Exception as e:
print(f"Error retrieving video details: {e}")
return None, None, None
def update_video_title():
# Authenticate and get the YouTube service
youtube = get_authenticated_service()
# Get the video ID from the environment variable
video_id = os.environ["YT_VIDEO_ID"]
# Get the current view count of the video
view_count = get_view_count(youtube, video_id)
if view_count is None:
print("Failed to retrieve view count.")
return
# Format the view count with commas
view_count = f"{int(view_count):,}"
# Get the current video title, categoryId, and description
current_title, category_id, description = get_video_details(youtube, video_id)
if category_id is None or description is None:
print("Failed to retrieve video details.")
return
# Construct the new title based on the specified format
new_title = f"Real-Time? Not Quite... Views: {view_count} (Updated Every 10 Minutes to Spare the API!)"
# Ensure the title is within YouTube's title length limit (100 characters)
new_title = new_title[:100]
# Validate that the title is not empty
if not new_title.strip():
print("Error: The new title is empty or invalid.")
return
# Update the video title, keeping categoryId and description unchanged
update_request = youtube.videos().update(
part="snippet",
body={
"id": video_id,
"snippet": {
"title": new_title,
"categoryId": category_id,
"description": description, # Include the original description to prevent it from being removed
},
},
)
# Debug: Print before executing the update request
print(f"Updating video with Title: {new_title}")
# Try executing the update request
try:
update_request.execute()
print(f"Updated title to: {new_title}")
except Exception as e:
print(f"Failed to update title: {e}")
if __name__ == "__main__":
update_video_title()