Formating the api url to return only the videoId. (And use minimal quota)
Asked Answered
F

1

0

So I'm using Home Assistant to launch an automation that retrieves the newest videoId that a channel has uploaded, so I can use my google home to play it(on a Roku TV), works fine, I am working on creating an automation that also does a GET request but for now, I am using the home assistant rest sensor that updates by performing a GET after a set number of seconds, for some reason though there are only 3 sensors polled every minute or so it seems they use around 100-500 quota(hitting my quota of 10,000 after only a few hours or less), I'm not sure if this is a home assistant problem or if I am not using the api correctly(I only need the videoId), ill link my url below:

https://www.googleapis.com/youtube/v3/search?key=API_KEY&part=id&order=date&maxResults=1

Expected 1-3 quota usage per GET, getting 100+ quota usage per GET.

Florous answered 7/6, 2019 at 8:21 Comment(2)
I edited out your API key! Please note that the API key is an user's private information. Therefore nobody should include his/her API keys in API url's himself/herself makes public! In such cases, as yours is, that key must be quickly deleted from Google's developers console. Upon that, just ask the Web UI to get you a new key.Coaster
Weird thought for sure I edited it out. Changed it.Florous
C
4

Querying the Search Endpoint is more expensive than querying the PlaylistItems endpoint for the given user's uploads playlist. Depending on usage patterns, the default quotas may put rather tight limits on the number of calls an user is allowed to make on various endpoints of the API.

Adapting my answer to a different question, I suggest you to do the following instead: call PlaylistItems endpoint, passing to it as playlistId parameter the given channel's uploads playlist ID.

A given channel's uploads playlist ID is obtained upon querying the channel's own endpoint. The needed ID is to be found at .items.contentDetails.relatedPlaylists.uploads. Usually, an channel ID and its corresponding uploads playlist ID are related by s/^UC([0-9a-zA-Z_-]{22})$/UU\1/.

Note that you should query the Channels endpoint only once, then use the returned uploads playlist ID as many times as you need.

Also note that you may experiment using the fields parameter applied to your queries, as to get from the API partial resources only. However, I'm predicting that (I may well be wrong, since did not tested it) the cost of 3 points for querying PlaylistItems for its contentDetails object cannot be improved.


Here is a prototype URL:

https://www.googleapis.com/youtube/v3/playlistItems?key=APP_KEY&part=contentDetails&fields=items/contentDetails/videoId&maxResults=1&playlistId=PLAYLIST_ID

Coaster answered 7/6, 2019 at 10:55 Comment(7)
I need the videoId specifically, could you give me an example of how the link should be formatted?Florous
Here is a prototype URL: googleapis.com/youtube/v3/…Coaster
Thank you, that works perfectly. 3 is fine, for this, should never really go over 100 a day.Florous
May I warn you that querying the API with an URL as above has the following often overlooked issue: there is a difference between a video's published time and upload time.Coaster
Should be okay as I only need the newest video ID, mostly will be used for linus tech tips lol, so they only post once a day or so, so I'm guessing it'll probably be okay. thanks again.Florous
May I ask, is there a way to sort by published time?Florous
As far as I know, unfortunately there is none -- of course, excepting the possibility of doing that yourself, locally (implying extra work and extra quota costs).Coaster

© 2022 - 2024 — McMap. All rights reserved.