I am building an extension and it makes a lot of requests. The feature I'm working on is to display the total length of time it would take to watch a playlist. Given a playlist of size 1000, I have to make 40 requests just to find this information (50 videos at a time limit, 1st call to /v3/playlistItems for an array of videoID's, 2nd call to /v3/videos for duration information). As far as I can tell, just for that one playlist, I lose 600 quota. Per load of page. I know, nothing to get worked up about because I have 50,000,000 quota per day allowed, but I want to optimize early. This is also a speed issue. It takes a solid minute and a half just to get the playlist length.
Now, ETags. For some reason, every time I make a request to youtube's data API for videos or playlistItems I get a completely new Etag (Most of the time, I have had cases where it returns the same ETag), regardless of playlist (I haven't tried private playlists, did not do OAuth yet). I'm assuming that the reason is that something is changing somewhere in a playlist, causing a new Etag very fast. Views? PlaylistItems doesn't even return views!
Here are example API calls to a macaroni playlist. The ETags are always different! How am I supposed to use them if they don't work? They are specific, there is no way that the length of videos changes in between requests.
The api key is omitted because you can make your own api key.
Playlist Items, give me video id's, page tokens, and Etag for playlist for items 100-150
Videos, give me durations and Etag for these video ids
I figured out how to cache the data a while ago, sorry!
You can make a call to /playlists to get both the total count of items in a playlist, plus the etag changes if and only if the playlist itself changed, which is what I want. I only want to make new requests if the base playlist changed.
A call to /playlistItems always generates a new etag, regardless of changes. I think this endpoint is meant for temporary querying to figure out metadata of a video as it relates to a playlist, not for static data lookup. Playlists are very felxible and I think YouTube decided against caching this data since calls to /playlistItems are often on a case-by-case basis. It's likely their backend automatically generates an etag, but doesn't actually save anything for this endpoint.
So, these are the steps to get the total length of time of a playlist, plus caching:
If-None-Matchheader (should work even if etag is empty)
videoId:videoLengthpair, then add length to a
lengthsarray into a moment.js duration object