Created
August 4, 2021 20:11
-
-
Save tim-tx/da169cc2671d62101268afd14f91f191 to your computer and use it in GitHub Desktop.
Get YouTube subscriptions
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/guides/code_samples#python | |
import os | |
import json | |
import google_auth_oauthlib.flow | |
import googleapiclient.discovery | |
import googleapiclient.errors | |
scopes = ["https://www.googleapis.com/auth/youtube.readonly"] | |
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 = "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) | |
nextPageToken = None | |
items = [] | |
while True: | |
request = youtube.subscriptions().list( | |
part="contentDetails,id,snippet,subscriberSnippet", | |
mine=True, | |
maxResults=50, | |
pageToken=nextPageToken | |
) | |
response = request.execute() | |
items += response["items"] | |
if "nextPageToken" in response: | |
nextPageToken = response["nextPageToken"] | |
else: | |
break | |
# print(nextPageToken) | |
with open("subs.json", "w") as fp: | |
json.dump(items, fp, indent=2) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment