How to get video url with youtube api(v3) - javascript

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.

Related

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

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"]

Is there was way to limit the number of queries from youtube api?

I am creating a tampermonkey script that will read a line of text that contains an artist and song title from a webpage and will create a link that will open a new tab to the first result that is returned from the youtube api. My issue is that when the page is loaded and say there are 5 songs on this particular page, according to my youtube api dashboard, I am making about 1200 api requests for that single page. Im guessing that its because when I query a single artist/song, its getting every result on its server. I tried limiting my maxResults to 1, but this doesn't help. Since youtube decreased the number of queries a single API gets, I would like to know if there is a way to reduce the number of queries it makes. Realistically, I just need the first result from the GET request, since chances are that will be the correct video.
here is a snippet of my code that parses through the json data:
function getLink(artist, song){
// API Key
var key = "MY_KEY";
// Setup url for api
var url = 'https://www.googleapis.com/youtube/v3/search?part=id&q=' + artist + "-" + song + '&maxResults=1&key=' + key;
// call api and get videoId
var xhReq = new XMLHttpRequest();
xhReq.open("GET", url, false);
xhReq.send(null);
var id = JSON.parse(xhReq.responseText);
return id.items[0].id.videoId;
}
I'm not sure if I'm looking at the same Google doc, but it says the part parameter should be snippet for a Search:list call. I know different calls take up different quota amounts. Using the query below I get only one song returned. It seems to use only the normal 100 units of quota required for Search:list:
https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Adele+Hello&fields=items(id(videoId))&key=API_key
It returns:
{
"items": [
{
"id": {
"videoId": "YQHsXMglC9A"
}
}
]
}
Be sure to escape artist and title with something like encodeURIComponent. It could be that the strings in those variables are breaking the intended URL you want to process.
Even more, if you do a test with the Youtube API sandbox, you could check it by yourself. In my case I've tried "Radiohead" and "Paranoid Android" with 3 max results and it's working fine. See here

Soundcloud API get tracks force update

Hey I would like to know please how can I retrieve tracks from the Soundcloud API and force it to be up to date and not from a cache please?
The issue I got is that I added new tags to the tracks on Soundcloud, and I want to get these using the API, but the API returns old results like the new tags that I added aren't exist, which means there is probably a cache and I don't see any kind of "update" parameter in the documentations so I can't update the cache and it isn't reading the new tags.
Here's an example of my code:
SC.get('/users/[user-ID#-goes-here]/tracks', {
tags: 'test'
}, function(tracks) {
console.log(tracks);
});
or simply by visiting the next url:
http://api.soundcloud.com/users/[user-ID#-goes-here]/tracks?client_id=[client-ID-goes-here]&limit=15&tags=test
Is there any parameter to force an update to the cache please?

Given the video ID, how to get it's title using YouTube Data API v.3

In my project I generate custom wrappers for youtube video embeds on the client side. I used to get video titles by parsing xml responses from http://gdata.youtube.com/feeds/api/videos/VIDEO_ID. Recently Youtube changed the title field for every video with "https://youtube.com/devicesupport" indicating deprecation of Data API v.2. I went to v.3 documentation and got confused. It was talking about authorization credentials, registering a project in the developers console, etc. Is it really necessary to just get a video's title?
Only need a apikey of you application and videoID:
GET https://www.googleapis.com/youtube/v3/videos?part=id%2Csnippet&id={video_id}&key={YOUR_API_KEY}
https://developers.google.com/youtube/v3/docs/videos/list
Set the part parameter to snippet and id to the ID of the video. For example, with the ID jofNR_WkoCE, you will get:
{
...
"items": [
{
...
"snippet": {
...
"title": "Ylvis - The Fox (What Does The Fox Say?) [Official music video HD]",
...
}
}
]
}

Deezer Radio: Track id not in radio

How to achieve that all tracks in Deezer Radio API remain unchanged and therefore playable?
Explanation:
I have a Deezer App (HTML+JavaScript, no external libraries) on a Set-Top-Box.
App grabs list of radio trakcs from above mentioned API. It then plays the tracks, fetching the streaming URL for each track from streaming API.
Sometimes the list of radio tracks remains unchanged for many API calls and sometimes the list changes with every call. Why is this so? When a radio track is no longer in radio's list, streaming API returns an error:
{
error: {
type: "DataException",
message: "Track id not in radio",
code: 800
}
}
For example, I get a list of 40 tracks for a particular radio, streaming API normally returns full streaming URLs but every 5th or so track fails to play (a.k.a. Deezer API throws above error) it is not because of erroneus API call but because that particular track is not in the original list of 40 tracks anymore... or this is my own assumption at least.
Therefore I am searching for a way to have a fixed list of 40 tracks that would not change until I play all 40 of them and then get the new list. Is that possible or am I missing something?
Thank you!
There is two ways to achieve this with the JavaScript SDK.
1) You can directly play the radio with DZ.player.playRadio(radio_id)
2) If your really want a fixed list, you can fetch the track list on http://api.deezer.com/radio/radio_id/tracks then play all the tracks with DZ.player.playTracks(tracks)
Example:
DZ.api('/radio/30771/tracks', function(result) {
var tracks = result.data.map(function(elem) {
return elem.id;
});
DZ.player.playTracks(tracks);
});
EDIT: After discussion in the comment, the solution is just do play directly the track without mentioning the radio_id in the streaming url.

Categories