Created
June 28, 2023 18:36
-
-
Save FernandoCelmer/5505c402babf664079d63bbf4aa9bcfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Sample Python code for youtube.subscriptions.list | |
# See instructions for running these code samples locally: | |
# https://developers.google.com/explorer-help/code-samples#python | |
import os | |
import json | |
from apiclient.errors import HttpError | |
import google_auth_oauthlib.flow | |
import googleapiclient.discovery | |
import googleapiclient.errors | |
from oauth2client.tools import argparser, run_flow | |
scopes = ["https://www.googleapis.com/auth/youtube", | |
"https://www.googleapis.com/auth/youtube.force-ssl", | |
"https://www.googleapis.com/auth/youtube.readonly", | |
"https://www.googleapis.com/auth/youtubepartner"] | |
def main(): | |
# Disable OAuthlib's HTTPS verification when running locally. | |
# *DO NOT* leave this option enabled in production. | |
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" | |
api_service_name = "youtube" | |
api_version = "v3" | |
client_secrets_file = "/home/fernandocelmer/Gists/my_youtube/SECRET_FILE.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_local_server() | |
youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials) | |
def add_subscription(channel_id): | |
add_subscription_response = youtube.subscriptions().insert( | |
part='snippet', | |
body=dict( | |
snippet=dict( | |
resourceId=dict( | |
channelId=item | |
) | |
) | |
)).execute() | |
return add_subscription_response["snippet"]["title"] | |
data = open(file="fmix.json", mode="r").read() | |
data = json.loads(data) | |
for item in data: | |
import time | |
time.sleep(5) | |
try: | |
channel_title = add_subscription(channel_id=item) | |
print(channel_title) | |
except HttpError as error: | |
import logging | |
logging.error(str(error)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment