-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_script.py
78 lines (64 loc) · 2.58 KB
/
comment_script.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
import os
import google_auth_oauthlib.flow #pip install google-auth-oauthlib
import googleapiclient.discovery #pip install google-api-python-client
import googleapiclient.errors
import pprint
from time import sleep
#initialize permissions
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
#to displau data
pp = pprint.PrettyPrinter(indent=1)
def main():
#setup
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "comments_secret.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
while(True):
# Request (This asks Youtube API for the comment data)
request = youtube.commentThreads().list(
part="snippet",
videoId="E9IhFQynJVc"
)
response = request.execute()
comment_data = response["items"][0]
comment_snippet = comment_data["snippet"]["topLevelComment"]["snippet"]
comment_text = comment_snippet["textOriginal"]
comment_username = comment_snippet["authorDisplayName"]
#Make sure it's under 300 characters and format
buffer = 25; #length of the hard-coded text i.e. "Most Recent Comment"
comment_username = comment_username[:min(len(comment_username), 30)]
max_length = 100 - (buffer + len(comment_username))
comment_text = comment_text[:min(len(comment_text), max_length)]
#Update title
title_upd = "\""+ comment_text + "\" -" + comment_username+ "(Your Comment Is This Video's Title)"
# Request (This is what asks Youtube API for the video data)
request = youtube.videos().list(
part="snippet,statistics",
id="E9IhFQynJVc"
)
response = request.execute()
data = response["items"][0]
vid_snippet = data["snippet"]
curr_title = vid_snippet["title"]
if(curr_title != title_upd):
vid_snippet["title"] = title_upd; #Max 300 Characters
request = youtube.videos().update(
part="snippet",
body={
"id": "E9IhFQynJVc",
"snippet": vid_snippet
}
)
response = request.execute()
print("Worked!")
sleep(180) #every 3 minutes
#run program
if __name__ == "__main__":
main()