-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutubeplylst.py
More file actions
116 lines (96 loc) · 3.6 KB
/
Copy pathyoutubeplylst.py
File metadata and controls
116 lines (96 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import argparse
import google_auth_oauthlib.flow
import googleapiclient.discovery
# Define your client secrets file path
CLIENT_SECRETS_FILE = "client_secret.json"
# Define the correct scope for accessing YouTube data
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def authenticate():
# 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"
# 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(port=0)
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
return youtube
def create_playlist(youtube, title, description):
request = youtube.playlists().insert(
part="snippet,status",
body={
"snippet": {
"title": title,
"description": description
},
"status": {
"privacyStatus": "public"
}
}
)
response = request.execute()
return response["id"]
def search_video_id(youtube, query):
request = youtube.search().list(
part="id",
maxResults=1, # Assuming you want the first search result
q=query
)
response = request.execute()
if "items" in response:
video_id = response["items"][0]["id"]["videoId"]
return video_id
else:
return None
def add_songs_to_playlist(youtube, playlist_id, song_file):
added_songs = []
not_found_songs = []
with open(song_file, "r", encoding="utf-8") as file:
songs = [line.strip() for line in file if line.strip()]
for song in songs:
video_id = search_video_id(youtube, song)
if video_id:
request = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": playlist_id,
"resourceId": {
"kind": "youtube#video",
"videoId": video_id
}
}
}
)
request.execute()
added_songs.append(song)
else:
not_found_songs.append(song)
return added_songs, not_found_songs
def main():
# Create an ArgumentParser to handle command-line arguments
parser = argparse.ArgumentParser(description="YouTube Playlist Creator")
parser.add_argument("title", help="Playlist title")
parser.add_argument("description", help="Playlist description")
args = parser.parse_args()
youtube = authenticate()
# Get the playlist title and description from the command-line arguments
playlist_title = args.title
playlist_description = args.description
# Replace with the path to your song file
song_file_path = "playlist_tracks.txt"
# Create a new playlist and get its ID
playlist_id = create_playlist(youtube, playlist_title, playlist_description)
# Add songs to the playlist and get lists of added and not found songs
added_songs, not_found_songs = add_songs_to_playlist(youtube, playlist_id, song_file_path)
print(f"Playlist '{playlist_title}' created with {len(added_songs)} songs.")
if not_found_songs:
print("Songs not found on YouTube:")
for song in not_found_songs:
print(song)
if __name__ == "__main__":
main()