How can I get the status of a user's stream? - youtube

I'm building a thing that tells people when a YouTube stream goes live. In order to build this, I need to know when a stream goes live, be it via push notification, polling, or some other method.
With Twitch, it's dead easy. All you have to do is send a GET to https://api.twitch.tv/kraken/streams/[STREAMER_USERNAME_HERE] and you get a JSON object with everything you'd ever hope to know (try it in your browser, it's great). I have combed through the YouTube Streaming and Data API's and nothing even close to this feature seems to exist.
I know that something, somewhere is alerting something when a stream goes live, because my phone gets a notification when the people I am subscribed to start streaming. But if there are no public calls in the API, I'm left with no other option than scraping the HTML off a page every couple seconds, which is bad for both me and YouTube.
Further on this, YouTube's push notification API for regular videos famously doesn't work [1] [2] [3] [4], and every dev is left to either use the deprecated XML feeds (which update sometimes up to an hour slow) or scrape HTML off youtube.com pages (the only current solution for instant updates). So, even if there's something off the API that I missed, it's moot if it doesn't actually work.
That said, am I missing something? Is it possible to get a notification if a stream goes live? It's kind of a deal-breaker for YouTube vs Twitch if there isn't.

It is currently not possible to do what you are asking with the public YouTube API.
You can only retrieve information about your own liveStream or liveBroadcast objects, not anyone else's. Additionally, there is no public API to get a notification when another user goes live.

Related

Live streams created using the YouTube Data API are being marked as private

We are running into an issue where live streams created through the YouTube Data API are being marked private and locked without appeal after the stream, sometimes hours later.
We're using OAuth and the app has gone through the verification process and is verified.
Trying to see if anyone can provide answers to get us over this hurdle. This has been really frustrating as everything looks like it should be working, schedules are created, and the streams are being sent and watched, but are later marked private.
The email from YouTube says spam, deceptive practices, etc. But that seems to just be a generic response.
Thanks in advance!

Live events not showing in YouTube Data API v3

I'm sending a get request to a link like this:
https://www.googleapis.com/youtube/v3/search?part=snippet&channel_id=[MyChannelId]&type=video&eventType=live&key=[MyKey].
The purpose of it is to get the live events of a specific channel for which I provided its ID.
The problem is this does not work all the time.
When I go checking in YouTube, the channel is streaming, but I don't get any data through this link.
What are the possible mistakes that may cause this?
Or is it just a problem from their servers?
Is there an alternative reliable way?

Embed a YouTube channel's current live stream without the video ID

Every time a user starts a live stream on YouTube, a new ID is generated for the stream, along with the corresponding video and URL. Currently, if I want to embed a live stream, I can use YouTube's sharing functions to add the video to my page in an iFrame.
When the user stops streaming, embeds of the live stream automatically switch to showing a recording of that stream. However, if the user starts broadcasting again later on, the embed will continue to show the old recording instead of switching to the new stream. This is because the video ID in the embed is hard-coded and each stream generates a new video ID.
My goal is to create an embed that will automatically display a user's live stream whenever they are streaming, and show an indication of whether they're online or offline. Is there an embed URL that would allow me to do this, or is there something in the API that might help?
I want to embed other streams that aren't just my own, so I need to do this in a way that doesn't require the streamer to log in or authenticate on my site.
If you know the ID of a YouTube channel, and if that channel streams a livestream set to Public, an iframe with this URL will show it:
https://www.youtube.com/embed/live_stream?channel=YOUR_CHANNEL_ID_HERE
See https://stackoverflow.com/a/39582176/470749
Unfortunately I haven't found a similarly simple way to permanently embed the YouTube chat for that livestream.
As far as I can tell, there's nothing built into the YouTube API that would allow you to embed a channel's current live stream automatically without knowing its ID. That said, it's possible to implement this yourself by writing a custom API and hosting it on your own server.
I recognize that this can look like a daunting task, so I've laid out some rough steps below to get you started.
Set up an endpoint on your own server. You could accept a
channelId argument or hard-code one, depending on how extensible
you want this to be.
Query YouTube's search endpoint1 for the specified channelId and eventType=live. An HTTPS request for this will look something like this:https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[CHANNEL_ID]&eventType=live&maxResults=1&order=date&type=video&key=[YOUR_API_KEY]
Check the search JSON response. If it returns any results (data.pageInfo.totalResults > 0), you know that the channel is live.
If the channel is live, redirect the request to your server directly to the live video's embed URL based on the video's ID in the query response (data.items[0].id.videoId).
If the channel isn't live, create a placeholder as you see fit, or make a second request to search for eventType=completed for past broadcasts, eventType=upcoming for scheduled broadcasts, or remove the eventType parameter to get the most recent video.
Once you have a server that can respond and redirect requests, you can embed an iFrame in your page that points directly to your API URL. Your server will handle the logic and, using the redirect, change the iFrame to a YouTube video player automatically, without requiring you to perform client-side logic or expose your API key2.
1 As with all YouTube API requests, search#list queries will count towards your daily quota. If you intend for this to be a high-traffic endpoint, you could either request an increased quota from YouTube, or implement a caching solution on your end to cut down on the number of requests you make.
2 GCP (Google Cloud Platform), which you'll use to manage your access the YouTube Data API, has pretty good protections against API key theft for times when you do have to expose it on the client side. That being said, best practice is to keep your key secret by storing it only on the server whenever possible.

How to know when a user complete viewing a video with youtube api?

I currently working on a project where I send a youtube video id and show it to the user. And I want to know if there are a method to know when the video is completely viewed by the user. In my application's client, I send a response to the server when the view is completed, and I want to prevent receiving falsed data from untrusted user. And that's why I want to know if youtube api make a things like that. I have seen other similar topics, but I haven't found a safe way to secure communication between client and server for this problem.
Thank you.
From this SO post, it was mentioned that you can use onStateChange event to log that a user viewed a video. But the general idea here is about safety which I think is more important, from there I don't think you can do this as per the privacy of a user. You can understand more about YouTube Reporting and Analytics here.

Expected delay for youtube feeds?

We switched over to YouTube from a previously developed internal video system, and right now I've got it going on public upload feeds for users. The problem comes when we first got only about a dozen out of 150 or so videos available from the primary user's account. Over time of the day that number rose to about 50, and its stayed there. I found one post on the YouTube API forums about delay, but no word on how long I can expect that.
Anyone have experience about this to tell me what to expect?
Expected latencies can vary, and are documented here (http://code.google.com/apis/youtube/2.0/reference.html) - ways you can minimize it:
1) Upload the video as public. If you upload the video as private first, this will leave the video out of the fast-track indexing
2) Make an authenticated request to the user feed, this will guarantee the freshest data that the API has.
ie. http://gdata.youtube.com/api/feeds/users/username/uploads (with a dev key and auth token for 'username') instead of http://gdata.youtube.com/api/feeds/videos?author=username

Resources