Refactoring
This commit is contained in:
parent
e8d5aced72
commit
6393c9e187
106
youtube_api.py
106
youtube_api.py
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
from googleapiclient.discovery import build
|
from googleapiclient.discovery import build
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
@ -29,27 +30,80 @@ def get_playlist_length(youtube, playlist_id):
|
|||||||
|
|
||||||
return playlist_info['items'][0]['contentDetails']['itemCount']
|
return playlist_info['items'][0]['contentDetails']['itemCount']
|
||||||
|
|
||||||
|
def list_songs(youtube, channel_id):
|
||||||
|
max_results = 50
|
||||||
|
request = youtube.playlistItems().list(
|
||||||
|
part="snippet,contentDetails",
|
||||||
|
maxResults=max_results,
|
||||||
|
playlistId=channel_id
|
||||||
|
)
|
||||||
|
response = request.execute()
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Process the first page of results
|
||||||
|
for idx, item in enumerate(response['items'], start=1):
|
||||||
|
title = item['snippet']['title']
|
||||||
|
print(f"{idx:02d} - {title}")
|
||||||
|
|
||||||
|
# Continue making requests for additional pages using pageToken
|
||||||
|
new_starting = 51
|
||||||
|
while 'nextPageToken' in response:
|
||||||
|
request = youtube.playlistItems().list(
|
||||||
|
part='snippet',
|
||||||
|
playlistId=channel_id,
|
||||||
|
maxResults=max_results,
|
||||||
|
pageToken=response['nextPageToken']
|
||||||
|
)
|
||||||
|
response = request.execute()
|
||||||
|
# Process the results for the additional pages
|
||||||
|
for idx, item in enumerate(response['items'], start=new_starting):
|
||||||
|
title = item['snippet']['title']
|
||||||
|
print(f"{idx:02d} - {title}")
|
||||||
|
new_starting += 50
|
||||||
|
|
||||||
|
def add_song(youtube, channel_id, song):
|
||||||
|
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("\nSong added successfully!")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import re
|
parser = argparse.ArgumentParser(description="Manage YouTube playlist songs.")
|
||||||
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("channel_name", help="Name of the YouTube channel")
|
||||||
parser.add_argument("song", help="Name of the song to be added")
|
parser.add_argument(
|
||||||
|
"--list",
|
||||||
|
action="store_true",
|
||||||
|
help="List all songs in the playlist"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--add",
|
||||||
|
nargs=2,
|
||||||
|
metavar=("channel", "song"),
|
||||||
|
help="Add to the playlist a song"
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
channel_name = args.channel_name
|
channel_name = args.channel_name
|
||||||
song = args.song
|
channel_id = channels.get(channel_name)
|
||||||
# this assumes a song is passed as in either of the following ways
|
|
||||||
# https://youtu.be/eEnyi9L6KP4 --> split by "/"
|
|
||||||
# https://youtube.com/watch?v=Ez-gizOF0Wo --> split by "="
|
|
||||||
song = re.split(r"/|=", song)[-1]
|
|
||||||
|
|
||||||
if channel_name not in channels:
|
if not channel_id:
|
||||||
print(f"Error: Channel '{channel_name}' not found.")
|
print(f"Error: Channel '{channel_name}' not found.")
|
||||||
return
|
return
|
||||||
|
|
||||||
channel_id = channels[channel_name]
|
|
||||||
|
|
||||||
# Disable OAuthlib's HTTPS verification when running locally.
|
# Disable OAuthlib's HTTPS verification when running locally.
|
||||||
# *DO NOT* leave this option enabled in production.
|
# *DO NOT* leave this option enabled in production.
|
||||||
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
|
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
|
||||||
@ -66,23 +120,17 @@ def main():
|
|||||||
api_service_name, api_version,
|
api_service_name, api_version,
|
||||||
credentials=credentials)
|
credentials=credentials)
|
||||||
|
|
||||||
pos = get_playlist_length(youtube, channel_id)
|
if args.list:
|
||||||
|
list_songs(youtube, channel_id)
|
||||||
request = youtube.playlistItems().insert(
|
elif args.add:
|
||||||
part="snippet",
|
target_channel, song = args.add
|
||||||
body={
|
# # this assumes a song is passed as in either of the following ways
|
||||||
"snippet": {
|
# # https://youtu.be/eEnyi9L6KP4 --> split by "/"
|
||||||
"playlistId": channel_id,
|
# # https://youtube.com/watch?v=Ez-gizOF0Wo --> split by "="
|
||||||
"position": pos,
|
song = re.split(r"/|=", song)[-1]
|
||||||
"resourceId": {
|
add_song(youtube, channels.get(target_channel), song)
|
||||||
"kind": "youtube#video",
|
else:
|
||||||
"videoId": song
|
print("Invalid command. Use --list or --add.")
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
response = request.execute()
|
|
||||||
print("Song added successfully!")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user