85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Sample Python code for youtube.playlists.update
|
|
# See instructions for running these code samples locally:
|
|
# https://developers.google.com/explorer-help/code-samples#python
|
|
|
|
import argparse
|
|
import os
|
|
|
|
from googleapiclient.discovery import build
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
import googleapiclient.errors
|
|
|
|
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
|
|
|
|
channels = {
|
|
"breakbeat":"PLZFfy80_qguYVqbpQlX-aHdiYHTwc6-wW",
|
|
"wods": "PLZFfy80_qguYCbTDQD0GSK4zUzEjbQMQT",
|
|
"rap_español": "PLZFfy80_qgub44wWWDcAykdQCJNCXo4jp",
|
|
}
|
|
|
|
def get_playlist_length(youtube, playlist_id):
|
|
items = youtube.playlists().list(
|
|
part="snippet,contentDetails",
|
|
id=playlist_id
|
|
)
|
|
playlist_info = items.execute()
|
|
|
|
return playlist_info['items'][0]['contentDetails']['itemCount']
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Add a song to a YouTube playlist.")
|
|
parser.add_argument("channel_name", help="Name of the YouTube channel")
|
|
parser.add_argument("song", help="Name of the song to be added")
|
|
|
|
args = parser.parse_args()
|
|
|
|
channel_name = args.channel_name
|
|
song = args.song
|
|
# this assumes a song is passed as
|
|
# https://youtu.be/eEnyi9L6KP4
|
|
song = str(song).split("/")[-1]
|
|
|
|
if channel_name not in channels:
|
|
print(f"Error: Channel '{channel_name}' not found.")
|
|
return
|
|
|
|
channel_id = channels[channel_name]
|
|
|
|
# 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_secret = "./client_secrets.json"
|
|
|
|
flow = InstalledAppFlow.from_client_secrets_file(
|
|
client_secret, scopes)
|
|
credentials = flow.run_local_server(port=0)
|
|
|
|
youtube = build(
|
|
api_service_name, api_version,
|
|
credentials=credentials)
|
|
|
|
pos = get_playlist_length(youtube, channel_id)
|
|
|
|
request = youtube.playlistItems().insert(
|
|
part="snippet",
|
|
body={
|
|
"snippet": {
|
|
"playlistId": channel_id,
|
|
"position": pos,
|
|
"resourceId": {
|
|
"kind": "youtube#video",
|
|
"videoId": song
|
|
}
|
|
}
|
|
}
|
|
)
|
|
response = request.execute()
|
|
print("Song added successfully!")
|
|
|
|
if __name__ == "__main__":
|
|
main() |