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.

stvar
  • 6,551
  • 2
  • 13
  • 28
Eric Miller
  • 49
  • 10
  • 3
    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](http://console.developers.google.com/). Upon that, just ask the Web UI to get you a new key. – stvar Jun 07 '19 at 10:37
  • Weird thought for sure I edited it out. Changed it. – Eric Miller Jun 07 '19 at 13:21

1 Answers1

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

stvar
  • 6,551
  • 2
  • 13
  • 28
  • I need the videoId specifically, could you give me an example of how the link should be formatted? – Eric Miller Jun 07 '19 at 11:04
  • 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 – stvar Jun 07 '19 at 11:16
  • Thank you, that works perfectly. 3 is fine, for this, should never really go over 100 a day. – Eric Miller Jun 07 '19 at 11:21
  • 2
    May I warn you that querying the API with an URL as above has the following often overlooked issue: there is a [difference](https://developers.google.com/youtube/v3/docs/videos#snippet.publishedAt) between a video's published time and upload time. – stvar Jun 07 '19 at 11:25
  • 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. – Eric Miller Jun 07 '19 at 11:44
  • May I ask, is there a way to sort by published time? – Eric Miller Jun 07 '19 at 12:12
  • 1
    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). – stvar Jun 07 '19 at 12:48