Get YouTube Search List from YouTube API - api

I'm trying to get a search list for a specific keyword from the YouTube API.
I've tried: https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=elanco&key={MY API KEY}
And it only gives me the first 50 Results. So I changed "maxResults" to equal 2000.
But then this error occurs:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid value '2000'. Values must be within the range: [0, 50]",
"locationType": "parameter",
"location": "maxResults"
}
],
"code": 400,
"message": "Invalid value '2000'. Values must be within the range: [0, 50]"
}
}
Does anyone know how I can get more than 50 results? My keyword generates around 1500 results, I want to be able to see all of them.
Thanks!

As per the docs, maxResults param having restriction [Acceptable values are 0 to 50, inclusive. The default value is 5].
So per one request you can get the 50 records at Max, if you see in the result of your first hit , you will get nextPageToken field (starting from first , except the last page,as this is the last page ) and prevPageToken (starting from second page, as there will be no prev page for the first page) field in response to the request.
Now we can use these values (nextPageToken/prevPageToken) to fetch records for the next or prev 50 records by passing the value to the query parameter
pageToken = < value of nextPageToken for the next set of records>
or < for prev use prevPageToken>.
In short, Add the pageToken param to the subsequent requests till the count equals pageInfo.totalResults !!!
Thanks!!!

Use the nextPageToken to get the next resultset. There is some restrictions, the API can only return max of 50 restultet at a time. But using the nexPageToken element you can get the next 50 resultset
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/FSbRmodfOD2HSQzA29X7Tn2Mupc\"",
"
nextPageToken": "CAIQAA"
"pageInfo": {
"totalResults": 199,
"resultsPerPage": 2
},
"items": [

Related

Fetching second maximum date json object(postgres)

I'm trying to fetch second max date json data from an json column..
Here is jsonb column
--------
value
--------
{
"id": "90909",
"records": [
{
"name":"john",
"date": "2016-06-16"
},
{
"name":"kiran",
"date": "2017-06-16"
},
{
"name":"koiy",
"date": "2018-06-16"
}
]
}
How to select the second maximum date json object in the jsonb column..
expected output:-
{
"name":"kiran",
"date": "2017-06-16"
}
and if we have only one object inside the records means that will be the second max date
and any suggestions would also helpful..
one way of doing it could be, get all the dates in an array, then sort the array in DESC and then get the second date. all these steps can be done in a single query

POSTMAN - Test failure false to be truthy

As a beginner I have few questions. I am using the Get request, which would populate json below.
https://reqres.in/api/users
{
"total": 12,
"total_pages": 4,
"data": [{
"id": 1,
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
}]
}
for the 2 tests below while the 1st one passes the 2nd test fails with the message:
AssertionError: expected false to be truthy
//Verify Page number total is 12
var jsonData = JSON.parse(responseBody);
tests["Checking total page number-Manual"] = jsonData.total === 12;
//verify is exists and is 1
var jsonData = JSON.parse(responseBody);
tests["Checking ID exists and is 1"] = jsonData.id === 1;
Question 1:
A github post that I found says there may be an error and suggests to use
the new pm.* equivalent instead. However I do not see any difference between the 1st and the 2nd. So why does the 2nd test fail?
Question 2:
Is it possible to write test to verify that for ID:1 the firstname is George?
Thanks in advance for your time.
The reason that your 2nd test fails is because data is an array and in this case you must access the first element. You would want to do something like this (new syntax):
pm.test("Verify id is equal to 1", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].id).to.equal(1);
});
Similarly for testing first name is George:
pm.test("Verify id is equal to 1", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.data[0].first_name).to.equal("George");
});
If you always expect it to only be a single element in the array then you're safe to use index 0 i.e. data[0]. However if you expect there to be more elements in the data array then you would have to iterate through them to look for the correct element.
Here's a good reference for the API:
https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/

Collapsing a group using Google Sheets API

So as a workaround to difficulties creating a new sheet with groups I am trying to create and collapse these groups in a separate call to batchUpdate. I can call request an addDimensionGroup successfully, but when I request updateDimensionGroup to collapse the group I just created, either in the same API call or in a separate one, I get this error:
{
"error": {
"code": 400,
"message": "Invalid requests[1].updateDimensionGroup: dimensionGroup.depth must be \u003e 0",
"status": "INVALID_ARGUMENT"
}
}
But I'm passing depth as 0 as seen by the following JSON which I send in my request:
{
"requests":[{
"addDimensionGroup":{
"range":{
"dimension":"ROWS",
"sheetId":0,
"startIndex":2,
"endIndex":5}
}
},{
"updateDimensionGroup":{
"dimensionGroup":{
"range": {
"dimension":"ROWS",
"sheetId":0,
"startIndex":2,
"endIndex":5
},
"depth":0,
"collapsed":true
},
"fields":"*"
}
}],
"includeSpreadsheetInResponse":true}',
...
I'm not entirely sure what I am supposed to provide for "fields", the documentation for UpdateDimensionGroupRequest says it is supposed to be a string ("string ( FieldMask format)"), but the FieldMask definition itself shows the possibility of multiple paths, and doesn't tell me how they are supposed to be separated in a single string.
What am I doing wrong here?
The error message is actually instructing you that the dimensionGroup.depth value must be > 0:
If you call spreadsheets.get() on your sheet, and request only the DimensionGroup data, you'll note that your created group is actually at depth 1:
GET https://sheets.googleapis.com/v4/spreadsheets/{SSID}?fields=sheets(rowGroups)&key={API_KEY}
This makes sense, since the depth is (per API spec):
depth numberThe depth of the group, representing how many groups have a range that wholly contains the range of this group.
Note that any given particular DimensionGroup "wholly contains its own range" by definition.
If your goal is to change the status of the DimensionGroup, then you need to set its collapsed property:
{
"requests":
[
{
"updateDimensionGroup":
{
"dimensionGroup":
{
"range":
{
"sheetId": <your sheet id>,
"dimension": "ROWS",
"startIndex": 2,
"endIndex": 5
},
"collapsed": true,
"depth": 1
},
"fields": "collapsed"
}
}
]
}
For this particular Request, the only attribute you can set is collapsed - the other properties are used to identify the desired DimensionGroup to manipulate. Thus, specifying fields: "*" is equivalent to fields: "collapsed". This is not true for the majority of requests, so specifying fields: "*" and then omitting a non-required request parameter is interpreted as "Delete that missing parameter from the server's representation".
To change a DimensionGroup's depth, you must add or remove other DimensionGroups that encompass it.

mongoDB: aggregation which adds a field of counting field in the collection

I am new to MongoDb and have a query which I am struggling with..
I have a collection of reported users which looks like this:
{
"_id": 1,
"userId": 1,
"reason": "some reason",
"date": "2017-07-22"
}
I need a query which will add to each report the number of reports for that userId.
meaning if the collection has 3 records with userId=1. the query will return three records and each of them will also include a field
count=3 meaning the record above will now look like this:
{
"_id": 1
"userId": 1,
"reason": "some reason",
"date": "2017-07-22",
"count": 3
}
I tried using $project and $addFields aggregations but was not able to add a field which is a result of a query over the whole collection.
any ideas?
The answer provided by Veeram is correct, just that if you are running mongodb version less than 3.6, in the last stage of aggregate you might want to replace the $replaceRoot operator with a $project
aggregate([
{$group:{_id:"$userId", "data":{"$push":"$$ROOT"}, count:{$sum:1} } },
{"$unwind":"$data"},
{$project:{_id:"$data._id", userId:"$data.userId", reason:"$data.reason", date:"$data.date", count:1}}
])

YouTube Search API gives me less videos than requested per page, but also gives me a nextPageToken

The query below (First Request) says there are 24 results, but only returns me 3 items (when I asked for 20 per page)... fine... but why is it also giving me a nextPageToken. I thought, well, maybe if I get less than the 20 I'm asking for, I'm at the end...
But... in another case below (Second and Third Requests) it said there were 1717 videos and it gave me the first 20, but when I got the next page it only gave me 5... also with a nextPageToken.
Note: this does include a location and radius parameter... maybe this is not quite baked yet.
The question, then, is how can I tell when I have reached the end of my results?
(Edit: I found the reason for getting only 5 was that you still DO need to include maxResults along with nextPageToken, so maybe the answer is simply you're at the end if the nextPageToken is null or the number of items is less than resultsPerPage.)
First Request
https://www.googleapis.com/youtube/v3/search?maxResults=20&type=video&order=rating&q=Stanford%20University&location=37.86854355%2C-122.502038433&locationRadius=6000m&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
{
"nextPageToken": "CBQQAA",
"pageInfo": {
"totalResults": 24,
"resultsPerPage": 20
},
"items": [
{
"id": {
"kind": "youtube#video",
"videoId": "WMfwyjJz9hs"
},
},
{
"id": {
"kind": "youtube#video",
"videoId": "aGnYxoGHYHQ"
},
},
{
"id": {
"kind": "youtube#video",
"videoId": "s50ZSCKA1zY"
},
}
]
}
Second and Third Requests
https://www.googleapis.com/youtube/v3/search?maxResults=20&type=video&order=rating&q=Sausalito&location=37.8590937%2C-122.4852507&locationRadius=6000m&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
https://www.googleapis.com/youtube/v3/search?pageToken=CBQQAA&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
I have similar trouble. I fixed bug by deleting field &order=rating in request. I think this parameter exclude videos with Zero(0) rating / this url return more results https://www.googleapis.com/youtube/v3/search?maxResults=20&type=video&order=rating&q=Sausalito&location=37.8590937%2C-122.4852507&locationRadius=6000m&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
Default order parameter value is 'relevance' returns similar result or you can try &order=viewCount parameter return similar result to