How do I get a result from this specific API request? - javascript

I am currently trying to make a command in my discord bot that will track your stats in Rainbow six Siege. I am using an API for this, but I can't get any results out of my API request. The API gives a player ID between "players" and "profile", but I have no idea how to get around it. I also tried to get the response via an array, but that also gave no results. The thing I want to achieve is getting the player name for example, but I don't know how to get a value out of there. I thought r.body.players.profile.p_name, but that doesn't work because the API gives a player ID in between it. Maybe that someone knows how to work around it?
The result of r.body
The code I want to use to simply get the player name, but "profile" will be undefined
This is what the API responds when just pasting the link in my browser

You can use
Object.entries(...)
Object.entries requires an object to send and
it returns new array with key and value.
For eg:
let data={
"4cc06f42-86f6-11ea-bc55-0242ac130003": {
name: "Stack Overflow",
type: "website"
}
}
let newArr=Object.entries(data);
/*["4cc06f42-86f6-11ea-bc55-0242ac130003",{name:
"Stack Overflow",
type: "website"}]*/
You can access the key by:
let key=newArr.map(value=>value[0])
// ["4cc06f42-86f6-11ea-bc55-0242ac130003"]
You can also use array destructing
let key=newArr.map(([key,value])=>key)
// ["4cc06f42-86f6-11ea-bc55-0242ac130003"]

Related

How to get views and comment count when using Youtube data api -v3 playlistItems

I'm trying to get some data using Youtube data api -v3 playlistItems. I'm able to get all the data that comes with playlistItems perfectly. However, I need more data than what playlistItems offers. For example, I would like to get the view counts, comment count, and all the statistics
I know I could use /youtube/v3/videos to get the statistics but I have been trying this and it is not working for me. Please help. Thanks.
export function buildVideosRequest(amount = 12, loadDescription = false, nextPageToken) {
let fields = 'nextPageToken,prevPageToken,items(contentDetails/videoId,id,snippet(channelId,channelTitle,publishedAt,thumbnails/medium,title)),pageInfo(totalResults)';
if (loadDescription) {
fields += ',items/snippet/description';
}
return buildApiRequest('GET',
'/youtube/v3/playlistItems',
{
part: 'snippet,contentDetails',
maxResults: amount,
playlistId: 'PLvahqwMqN4M0zIUkkXUW1JOgBARhbIz2e',
pageToken: nextPageToken,
fields,
}, null);
}
Upon invoking the PlaylistItems.list endpoint, you obtain a result set of which each item is a playlistItems resource JSON object.
That JSON object doesn't contain the info you're interested in (view count, comment count, etc). This kind of info -- as you alluded yourself -- is obtainable through the Videos.list API endpoint.
That is that you have to collect all video IDs that you're interested in into an array, then invoke repeatedly the Videos.list endpoint, passing to it an id parameter assigned properly.
Note that this endpoint's id property allows you to reduce the number of endpoint calls, since the id may be specified as a comma-separated list of video IDs (at most 50). Hence, if you have for example an array of 114 video ID's, then you may issue only 3 calls to Videos.list.

How to use 'find' in Google Sheets API (Cocos Creator)

I am using Cocos Creator to build a web app that will communicate with google sheets.
I didn't manage to find a suitable client library (as highlighted in below link)
https://discuss.cocos2d-x.org/t/integrating-google-sheets-api/47920
And decided to go with the REST api using http requests.
However, I am unable to find documents that show exactly how to perform the requests i need.
UPDATE:
Updated the title as I realized my previous approach of condition check is meant for filter views, which i misunderstood it's use case.
However, i saw this video showing that you can use the 'find' function that will return the cell grid, which is exactly what i'm looking for.
https://youtu.be/yPQ2Gk33b1U?t=348
I would like to know how to construct the request string to perform this call on the REST api using http request.
Previously:
As the title says, I need a condition check to for searching my sheet and return the cell that has the exact match for a given string.
This link documents that such conditions exist but does not show how to execute it with http request
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other
I've found this post that is performing something very similar to what I wish to request, using FilterCriteria and Condition.
Looking for examples how to use the Google Sheets API FilterCriteria object
However, he is using the C# client library but is there any that I can use for Cocos Creator? As such, I have to perform this with REST using Http Request, and I have no leads on how to construct the request string.
var http = require('http');
var obj = {
'key' : 'MY_API_KEY'
}
var filters = {
'dataFilters': [
{
'Condition' : {
'type' : 'TEXT_EQ',
'values' : [{ 'userEnteredValue' : 'string_to_match' }]
},
}
]
}
var filtersStr = JSON.stringify(filters);
http.Get('MY_GOOGLE_SHEET_ID:getByDataFilter' + filtersStr, obj, function(responseJson)
{
console.log(responseJson);
});
Above code is something i attempted, but you bet it is not working.

How to get video url with youtube api(v3)

I'm working in node on a server and I'm trying to use the YouTube Api to get a video url. The particular url isn't important, this is more of just an exercise. As far as I can tell with the docs, the best way to find a particular video is if it is associated with a channel or user. IE,
query the api with a username(GoogleDevelopers for example)
get it's channel id
query again to get that channels playlists
search a particular playlist to get a playlist item
given a playlistid, search with that to get items(each should represent a video)
item ids have a videoId field, search with that
get video info, but no url?
However, when I get to that point I feel like there should be a url to that video. The though process would be to send this url back to a front end to allow it to use some library to render the video.
QUESTION: Is there a better way to do this? If not, where am I missing the video URL,
In your return you should get a videoId:
"id":
{
"kind": "youtube#video",
"videoId": "BsLvIF5c7NU"
}
edit: the return is actually just id actually:
"id": "BsLvIF5c7NU"
All you need to do is just append that to the standard url no?:
var url = `https://www.youtube.com/watch?v=${result.id.videoId}`;
Does that make sense?
edit: You could also use part=contentDetails in which case the id is under:
result.items.id
Depending on what you use in the part param will change up the layout of what's returned.
Hope that helps you, dude.

Pushing Elements to a JSON Array with Braces

I am having a hard time trying to push elements to a JSON file located in an external location; here's a backstory on what I am trying to achieve:
I am programming a private Discord bot and am currently working on the message-system portion of it. The whole idea is that a user's message is deleted if they aren't fully authorized onto the server; that user's message and UserID will be logged into the JSON file . The thing is, I can only log the message if a UserID is manually added to the array (using the push function, I can add the message). But, if I try to push a UserID to the file array, it acts as if the push function does not exist for this; I think the JSON is nested as well. I would appreciate any help I can get, thanks!
JSON
{"users":[{}]}
I want to put the UserID within the braces inside the brackets
Code to push to the JSON
function removeMessage(content, authorid) {
if (!messagedata.users[0][authorid]) {
messagedata.users[0].push(authorid);
}
}
Current I'm Getting:
TypeError: messagedata.users[0].push is not a function
Expected Output
{"users":[{"287423028147781654":["Hi"]}]}
The numerical value is the UserID, while 'Hi' is clearly the message
users[0] is an object, not an array (and hence has no push method). It appears you actually want to add a key to the object, so try this instead:
messagedata.users[0][authorid] = ["Hi"];

How to know if value changed in Parse Cloud Code afterSave hook?

I want to send push notifications every time the value of a single key of my object changes in a parse cloud code afterSave hook.
Parse.Cloud.afterSave("Channel", function(request) {
var channel = request.object
// TODO: check if value of channel key "state" was changed
});
How can I check if the value of the key state was updated?
This is all data I can get from the request object: http://parseplatform.org/Parse-SDK-JS/api/v1.11.0/Parse.Cloud.html#.TriggerRequest
The solution suggested in this thread feels wrong: Parse Javascript API Cloud Code afterSave with access to beforeSave values
I know I can do this via the dirty method in the beforeSave hook. However this does not work for me. Why? If I do send push notifications to many users this takes some time. The clients receiving the push notifications start requesting the updated channel object from the server. However they might receive an old version of the object because as long as beforeSave has not finished sending all pushes the channel object is not persisted in the database.
You can use request.original. For example:
Parse.Cloud.afterSave("Channel", function(request) {
var channel = request.object;
var channel_orig = request.original;
if (channel.get("status") != channel_orig.get("status")) {
// Send push notification.
}
});
The documentation states about request.original, that: "If set, the object, as currently stored." I'm not sure in what cases it would be set, though. In my use cases it works as provided in the code snippet above.

Categories