Reputation: 11
I am trying to put together three individual codes I found as examples in https://developers.google.com/youtube/v3/docs:
Create a playlist in my channel (https://developers.google.com/youtube/v3/docs/playlists/insert)
Search for video(s) with a given keyword in their title (among the videos I have in the channel) (https://developers.google.com/youtube/v3/docs/search/list)
Add the videos to the playlist (https://developers.google.com/youtube/v3/docs/playlistItems/insert)
I am able to sucessfully run the codes separately, but fail to combine them. Specifically: after the playlist is created, the code needs to get the playlist ID. Likewise, when a video is found that meets the search criteria, the video ID is needed for it to be added to the playlist. I have used Python. Please, could anyone help?
This is the code I have:
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
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 = "client_secrets.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)
request = youtube.search().list(
part="snippet",
forMine=True,
maxResults=50,
order="date",
q="searchcriteria",
type="video"
)
response = request.execute()
print(response)
vid=response['videoId']
request2 = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": "myplaylistIdcomeshere",
"resourceId": {
"videoId": "vid",
"kind": "youtube#video",
"playlistId": "myplaylistIdcomeshereagain"
},
"position": 0
}
}
)
respons2 = request2.execute()
print(response2)
if __name__ == "__main__":
main()
From the response from the search part I have the following (edited to remove unnnecesary details):
{'kind': 'youtube#searchListResponse', 'etag': '"longetag"', 'nextPageToken': 'anotherlongtext=', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 50}, 'items': [{'kind': 'youtube#searchResult', 'etag': '"theetagofthevideodfound"', 'id': {'kind': 'youtube#video', 'videoId': 'THISISWHATIWANT'}, 'snippet': {'publishedAt': '2020-02-22T19:33:03.000Z', 'channelId': 'MyChannelTitle', 'title': 'TitleOfTheVideo', 'description': '', 'thumbnails': {VideoThumbnails}}, 'channelTitle': 'MyChannelTitle', 'liveBroadcastContent': 'none'}}]}
I have tried with vid=response['videoId'] vid=response['id']
But none of them worked
Upvotes: 0
Views: 1019
Reputation: 11
Just figured out a way that works. Placing this piece of code in between requests does it. Posting in here in case someone else runs into the same question:
for search_result in response.get('items', []):
videos.append('%s (%s)' % (search_result['snippet']['title'],
search_result['id']['videoId']))
vid=search_result['id']['videoId']
Upvotes: 0
Reputation: 2291
The response of inserting an item to the playlist should return a playlist resource which contains the playlist ID
https://developers.google.com/youtube/v3/docs/playlists#resource
Similarly, when you search for a video you get a search_resource
in response which contains a list of videos with IDs
https://developers.google.com/youtube/v3/docs/search#resource
Upvotes: 0