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

最后都变了- 提交于 2019-11-27 08:58:12

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/56490584/formating-the-api-url-to-return-only-the-videoid-and-use-minimal-quota

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!