YouTube API Playlist Index Appears Not To Be Zero-Based - youtube-api

I have a YouTube player embedded in my website with the src set to the following URL format:
https://www.youtube.com/embed?listType=playlist&list=[playlistId]&index=0&rel=0&autoplay=1
The index parameter does not appear to be working correctly. The documentation says the index is 0-based. However, values of 0 or 1 will both play the 1st video in my playlist, a value of 2 will play the 2nd video, 3 will play the 3rd video, and so on. It would appear the index is 1-based despite what the documentation says.
Supporting my assumption, go to YouTube and view a playlist (like this one). Hover over one of the videos in the list and observe the URL. The URL for the first video will have an index = 1, the second video will have index = 2, etc.
Obviously I can update the code in my site to use a 1-based index and fix my issue. However, I would like some confirmation that the index is indeed supposed to be 1-based and that YouTube isn't going to come back later and "fix" the index to be 0-based as described in the documentation, thus breaking my site again.

Related

Tracking down URLs of deleted videos in a youtube playlist

I've recently made a Python program using the Youtube api v3 where, given a playlist id, it fetches certain information from every video in the playlist. However, through both the output of this code and this post on Google, it's pretty clear that information on videos that were either privated or deleted is not available through the Youtube api.
Is there an alternative program or resource that I can use to extract information from these unavailable videos, in particular their video ids?
The only solution I can think of now is to access the HTML of the display Youtube and search through it for certain strings (like "[deleted video]") and to then extract the id corresponding to that string. But, I've never dealt with HTML and, if I understand HTML correctly, I'd have to load a new page for every 50 videos in the playlist, which for playlists with thousands of videos, becomes rather inefficient and laborious.
I was hoping to use something like PyTube, but that couldn't handle unavailable videos either.
Edit
Here is the code that extracts the video ids:
from googleapiclient.discovery import build
api_key = "AI~~~" #get from yt (private key)
yt = build("youtube", "v3", developerKey = api_key)
plst_id = "PLorw3mfu-J0ipF4Ss0XgR8IxcwP-JzNKC" #unique yt playlist id
plst_req = yt.playlistItems().list( #request for info from yt api
part = "contentDetails",
playlistId = plst_id,
maxResults = 50
)
plst = plst_req.execute()
vid_ids = [] #available video ids taken from current playlist
for vid in plst['items']:
vid_ids.append(vid['contentDetails']['videoId'])
print(vid_ids)
print(plst['pageInfo']['totalResults'])
The first line printed contains the video ids of every available video in the playlist. The second line printed gives the number of videos in the playlist, including available and unavailable ones.
The playlist used in the code above is given here. It contains 10 total videos, of which one of them is unavailable.
In this case, the output is (with a valid api key)
['bv_cEeDlop0', 'mRKTOZmX2cE', '5ACvKdx1nns', 'wSNhP8b_Avo', 's56cHgokPlE', 'E4IHMWnQiMw', 'sCDkPShADSc', 'EVwgeUVVDYU', 'Z8Mqw0b9ADs']
10
Youtube still treats unavailable videos as an element of the playlist, but does not give out it's video id. In this particular instance, the video id of the unavailable video is "t83zUmjr05I", which is not hard to find manually: copy the link address of the deleted video and extract the part after the "v=".
But, on a larger scale manual extraction becomes tedious.
Here's a permanent fix to that!
You can try tube_dl.
pip install tube_dl
It uses modular approach and is up to date
More about this can be found at : https://github.com/shekharchander/tube_dl/
Maybe, the playlist module can help you with that. It uses regex to grab all video IDs not JSON. Please let me know if the problem is fixed or not, I will update module accordingly.
Edit
Here's the working code
from tube_dl import Playlist
p = Playlist('https://youtube.com/playlist?list=PLorw3mfu-J0ipF4Ss0XgR8IxcwP-JzNKC').videos
print(p)

Programmatically search Youtube videos and return the first available one on a web page

Good day!
Let's say I have a web page dedicated to a specific song by a specific author (eg "Imagine" by "John Lennon"). I would like to programmatically:
Search Youtube for the first n videos with "Imagine John Lennon"
Loop through these results to find a video which is available in the country where the user is located
Display the first video that matches the constraints on point 2. If no video matches them, then I won't display any video.
How can I do this? Is it better to do it with PHP or Ajax calls? I already checked some similar questions (1, 2) but as they are "old" I was wondering if there is a better method now.
Thanks for any help
This can be done with a single call to the Search: list endpoint, setting the 'q' and 'regionCode' parameters. Use any programming language you prefer.

youtube v3 api : get all live streaming playlist ids of a channel

I have two "Live streaming/live broadcast saved playlists" in following youtube channel --> https://www.youtube.com/user/swaminarayanlive.
I am trying to retrieve all the "live streaming / live broadcast playlists" of a channel using new youtube v3 api by using the below link-->
https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCBkNpeyvBO2TdPGVC_PsPUA&key={YOUR_API_KEY}
here i can get the info of playlists which are not live streams or live broadcast. and not able to get the same for the live one.
Please help me how can i get that for live one using youtube v3 api
I was researching another issue with the API when finding this, and when I saw that this was never resolved, I decided to look into it. It turns out that this is related to that other issue.
The YouTube API v3 lacks support for saved playlists. The channel swaminarayanlive did not create the playlists, only saved them from the channel BhujMandir.
The workaround in your case would be to retrieve the playlists of BhujMandir and extract the response snippets with "title" parameters containing the word "Live".
You would need to go through the pages and search each one for this, since there is a limit to how many results an API response can show at once. Currently it's 50.
So, you would use the following to get the first page of playlistItems:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCVItNtUctAknegvmYcMhUQg&maxResults=50&key={YOUR_API_KEY}
This will return an API response containing the properties "kind" (which will have the value "youtube#playlistListResponse") and "etag". If there are more than 50 results, there will also be a "nextPageToken" property. (On a page that is not the first, there would also be a "prevPageToken" property.)
After these properties, there are two blocks. One called "pageInfo", containing info about how many results (playlists) there are and how many are shown per page, and one block called "items", containing the resulting playlistItems.
You would look through the items block for any playlistItem with a title property (which is a string) containing the substring "Live" and get the id properties of those. You would then look on the next page by using the nextPageToken's value in a new HTTP request, like so:
GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCVItNtUctAknegvmYcMhUQg&maxResults=50&pageToken=[nextPageToken_value_here]&key={YOUR_API_KEY}
As of now, the two playlists you're looking for are on the pages with tokens CJYBEAA and CMgBEAA.
I think that where is the {YOUR_API_KEY} you should change this code and insert the name of your application program interface

Tags (keywords) are missing from the Youtube API video feed when using category search

When I make an authorized call for Youtube videos, I get all the tags/keywords in the feed.
http://gdata.youtube.com/feeds/api/users/default/uploads?alt=jsonc&v=2
However, if I want to filter this result by a single keyword/category, the tags are not returned by the API. Example, I want to reduce the number of videos in the feed by tag "English"/"French".
http://gdata.youtube.com/feeds/api/users/default/uploads/-/English?alt=jsonc&v=2
Is this expected behavior or a bug?
It's expected behavior; this blog post explains how your second request is going against the search index, which will never return keywords/tags in the response.

how to get number of views: Youtube analytics Report query

_analyticService = new YoutubeAnalyticsService(_authenticator = CreateAuthenticator()); ... var result = _analyticService.Reports.Query("channel==myChannelCode", "2013-01-01", "2013-02-02", "views").Fetch();
I have created a new Youtube analytic Service and I have sent the server a query.
My goal is to get the number of views the user has on his videos.
How do I use result to do this?
In your request
_analyticService.Reports.Query("channel==myChannelCode", "2013-01-01", "2013-02-02", "views").Fetch();
you only fetch the views between 1 January to 2 February. To get the full viewcount you would need to get the lifetime view count, try to specify a start and end date that is far away, like start-date=2005-01-01 and end-date=2100-01-01.
You also probably want to specify a filter with the video id of the video you are looking for, like filter=video==_iwmv6644dA Note also that the view count returned by the api don't seem to get updated as quick as the one on the watch page, so may therefore not be exactly the same.
However, this request works well to get the view count for one of my videos:
https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DUCYHMS8hN8s49F93iJuEgG6w&start-date=2005-01-01&end-date=2100-01-01&metrics=views&filters=video%3D%3D_iwmv6644dA&key={YOUR_API_KEY}

Resources