Youtube Data API is getting called too many times - youtube-api

So I set up the youtube data api and I added following code to fetch 4 videos on my page: Works fine for like 3 hours before the daily quota of 10.000 is reached. I tested it locally and everything worked fine there. Also: I´m using the search resource and its list method, which result in a quota cost of 100 per call, but that normally shouldnt be a problem, because its a small page.Thx in advance.
const youtubeWrapper = document.querySelector(".youtube-playlist");
async function getFloVideoData(){
const response = await fetch("https://youtube.googleapis.com/youtube/v3/search?part=snippet&channelId=UC78M9ne3s4_XpcmPM4r0Xxw&maxResults=4&order=viewCount&key=myAPIkey");
const data = await response.json();
const {items} = data;
items.forEach((item) => {
youtubeWrapper.innerHTML += `<div class="video-container"><img src="${item.snippet.thumbnails.medium.url}"><p>${item.snippet.title}</p><p>${item.snippet.publishTime}</p></div>`;
});
)
}
setTimeout(getFloVideoData(),3600000); //call function every hour too limit API-Calls

Related

Use Functions to get total number of in-progress and ringing calls to number

Am familiar with Twilio Studio and Twilio API (via PHP). Not familiar with Functions/node, so I'm a little lost on how to do this simple requirement...
In Studio I need to call a Function to return an integer of the total number of 'in-progress' and 'ringing' calls to a specific phone number (added together).
For example, if there are 7 'in-progress' and 5 'ringing' the function would return 12.
Any help appreciated. Thanks!
Twilio Functions allow you to run JavaScript in response to an HTTP request (or request from Studio). You can read about how Functions work in the documentation. To fetch the calls and filter them by their current status, you would use the Call Resource. There are examples in the documentation for fetching calls and filtering and all the documentation has examples in Node.js, as well as PHP so you can compare.
Here's a Function I put together quickly (I haven't tested it, but I think it looks good). It fetches the calls that are ringing and in-progress, and returns an object with individual totals and a total. You can then use this in your widget and refer to the values with variables like {{widgets.FUNCTION_WIDGET_NAME.parsed.ringing}}.
exports.handler = async (context, event, callback) => {
const client = context.getTwilioClient();
const ringingPromise = client.calls.list({ status: 'ringing' });
const inProgressPromise = client.calls.list({ status: 'in-progress' });
const [ringing, inProgress] = await Promise.all([ringingPromise, inProgressPromise]);
const response = {
ringing: ringing.length,
inProgress: inProgress.length,
total: ringing.length + inProgress.length
};
callback(null, response);
}

API gives constant subscriber count

I am trying to make a web app which shows real time subscribercount of a youtube channel.But it just shows me a constant number.The number of subscribers are not updated in next call of API.
I have written this function to get subs.I have tried with ID as well still no updates.Help me out.
Javascript
function getsubscribers(username, element) {
let url = `https://www.googleapis.com/youtube/v3/channels?key=${KEY}&forUsername=${username}& part=statistics`;
fetch(url)
.then((res) => res.json())
.then((data) => {
element.innerText = data.items[0].statistics.subscriberCount;
});
}
YouTube analytics has the same issue.
This is because the YouTube API is not a realtime api, the data seams to be updated once a day.

YouTube API "ChannelSections" results don't match with channel?

So for the YouTube Channel Mindless Self Indulgence it has 4 sections on the home tab first section is they're music videos playlist, the 2nd section is albums which is a group of different playlists, then another playlist section and the last section is there uploads.
But when I do a channelSections api call I get like 20 different items and it has me scratching my head why.
Here's the api response https://notepad.pw/raw/w27ot290s
https://www.googleapis.com/youtube/v3/channelSections?key={KEYHERE}&channelId=UChS8bULfMVx10SiyZyeTszw&part=snippet,contentDetails
So I figured this out finally, I neglected to read the documentation on the channelSections api 😅
here: https://developers.google.com/youtube/v3/docs/channelSections
I was getting channel sections for all the regions where channel like music may more often have region specific sections... To filter these you need to also include the targeting object in the part parameter. If the section is region free (or atleast i assume) it won't have the targeting object so something to take into condertation when handling your api response and filter sectoins based on regions.
Here's my code just trying to get the data filtered in react app, not the most practical maybe but I fumbled through it:
const data = response2.data.items;
console.log("response2 data", data);
const filtered = data.filter(item => {
if (item.targeting === undefined) return true;
let test = false;
item.targeting.countries.forEach(i => {
if (i === "US") test = true;
});
return test;
});

GAPI batch request on youtube v3 playlistItems.insert doesnt work as expected

I was playing with v3 Youtube API yesterday and found something unexpected / missing something.
Below code piece is creating a batch request to add multiple playlistItems.insert request and executes them.
const gapiRequest = (playlistId,videoId,i) => gapi.client.request({
path: '/youtube/v3/playlistItems?part=snippet',
method:'POST',
body:JSON.stringify({
snippet: {
playlistId,
position:i,
resourceId:{
videoId,
kind: 'youtube#video'
}
}
})
})
let addItemsToList = gapi.client.newBatch();
songs.map((song,i) => {
addItemsToList.add(
gapiRequest(playlistId,song.get('id'),i)
);
})
addItemsToList.then(result => {
console.log(result)
debugger;
})
I double checked the batch request. As I expected it has 19 (means I'm tryin to add 19 playlist item to playlist) request.
Also it's looks all working fine when I debug result. Batch request returns status 200 (means all playlistItems inserted) for every batch item.
But when it comes to youtube playlist I only see 3(sometimes 5-6) song added.
Does anyone have an idea what's going on / what am I missing ?

Knockout.js and Twitterfeed updating page with intervals

I am currently trying to update my twitter feed every 1 minute for example.
But I have no idea how to.
This is my ViewModel:
var savedLists = ['NielsenRamon', 'Wouter_Willaert', 'AWT_Centric'];
var twitterListModel = function(list) {
this.savedLists = ko.observableArray(list);
this.currentTweets = ko.observableArray([]);
ko.computed(function() {
twitterApi.getTweetsForUsers(savedLists, this.currentTweets);
}, this).extend({ throttle: 1 });
}
ko.applyBindings(new twitterListModel(savedLists));
I'm using a very minified version of this :http://knockoutjs.com/examples/twitter.html
the API i use is also the same.
What I want is that the bindings on my html page get updated every minute for example without refreshing my page. For example: when someone tweets. That tweet get displayed on the page after 1 minute without refreshing the page.
Is this possible the way I am working now?
Thanks in advance!
You could use a setInterval that calls twitterApi.getTweetsForUsers every minute.
Something like: setInterval(function() { twitterApi.getTweetsForUsers(savedLists, twitterListModel.currentTweets); }, 60000);
If the calls can take a while though, then you might want to modify the twitterApi sample code and use a setTimeout in the callback from the actual twitter API calls that reschedules itself with a new setTimeout.

Resources