Comment On All YouTube Videos For Specific Query Using Python

In the previous post, I shared a python script to get the list of all videos on any YouTube channel you want. There were many people who liked that. That’s the reason I am sharing another script that you can use to comment on all the YouTube videos for the specific query on that you want.

You can use this script to promote, bulk comment, market, engage on YouTube videos.

The modules using in this python script are apiclient.discovery, googleapiclient.discovery, google_auth_oauthlib.flow and google.oauth2

You will need to replace the API keys of your own. If you need a guide on how to generate YouTube API keys to work on this script, do let me know. You can even find them on their office document: https://developers.google.com/youtube/v3/getting-started or you can just search for it on Google/YouTube

Here’s the Python Code:

from apiclient.discovery import build
import random
import googleapiclient.discovery
import google_auth_oauthlib.flow
from datetime import datetime
from datetime import date
from google.oauth2 import service_account

SERVICE_ACCOUNT_FILE = 'altsskeys.json' #Google Spreadsheets API KEYS file 
scopessheet = ['https://www.googleapis.com/auth/spreadsheets']
scopes = ['https://www.googleapis.com/auth/youtube.force-ssl']
creds = None
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=scopessheet)
SAMPLE_SPREADSHEET_ID = 'ID of the spreadsheet you want your data'
service = build('sheets', 'v4', credentials=creds)
api_key = 'YouTube API Key'
youtubeapi = build('youtube', 'v3', developerKey=api_key)


client_secrets_file = "altytkeys.json" #Client Secret YouiTube API File

# 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("youtube", "v3", credentials=credentials)


comments = ['Comment 1', 'Comment 2', 'Comment 3']
 #Make a list of comments to pick from multiple comments randomly

term = input("What do you want to search on YouTube? ==> ")
req = youtubeapi.search().list(q=term, part='snippet',type='video',maxResults=50).execute()

data = []
ss = []
for item in req['items']:
    commenttext = random.choice(comments)
    title = item['snippet']['title']
    video_id = item['id']['videoId']
    youtubeurl = (f"https://youtube.com/watch?v={video_id}")
    now = datetime.now()
    curTime = now.strftime("%d/%m/%Y %H:%M:%S")
    print(f"Commenting on: {youtubeurl}")
    data.extend([title, youtubeurl, commenttext, curTime, term])
    ss.append(data)

    sheet = service.spreadsheets()
    report = sheet.values().append(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                   range="Sheet1!A2",
                                   valueInputOption="USER_ENTERED",
                                   body={"values": ss}).execute()
    ss = []
    data = []

    #Comment on YouTube Video
    def main():
        request = youtube.commentThreads().insert(part="snippet",body={"snippet":{"videoId": video_id,"topLevelComment": {"snippet": {"textOriginal": commenttext}}}}).execute()
        print("DONE!")
    if __name__ == "__main__":
        main()

That’s it. Make sure you find the keys and keep the folder names which you are using in the python.

Here’s the video demo of this script which you can check out.

Do you want more such python scripts? Do let me know in the comments below. Thank you!

Leave a Reply

Your email address will not be published.