This question already has answers here:
How to get my entire YouTube watch history?
(6 answers)
Closed 7 years ago.
Can anyone show me the way to get all watched youtube video or check if a video being watched or not by me.
The following request will give you a channel's "special" playlists (the ones that are created automatically, like history and watch later), given that you acquired an access token via OAuth beforehand:
GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&access_token=ACCESS_TOKEN
channels Reference
These steps will walk you through the authentication process, in case you still need that step.
After you got the ID of the user's watch history playlist, you can fetch the items with PlaylistItems or general information with Playlists, just like with any other playlist. But remember that these playlists are private to the user, so you have to provide the access token with every call.
The channels#list method will return a JSON with some information about the channel, including the playlist ID for the "uploads" playlist:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=OneDirectionVEVO&key={YOUR_API_KEY}
With the playlist ID you can get the videos with the playlistItems#list method:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUbW18JZRgko_mOGm5er8Yzg&key={YOUR_API_KEY}
You can test those at the end of the documentation pages.
Related
I want to get the first video from youtube which is listed when I enter a certain catchphrase, e.g. when I enter 'Kill You Eminem' it should give me the URL for the first video, in this case 'https://www.youtube.com/watch?v=D1I1x2pYMK0'.
I want to achieve this using javascript/node.js
You can use the YouTube API to obtain data. Just use axios or the fetch api to create a get request to their endpoint and get the first item of the return array. Here is the complete documentation of YouTube's API. If you need help with the code, HMU in messages.
Here is sample for getting the most viewed videos:
GET {base_URL}/search?part=snippet
&forMine=true
&order=viewCount
&type=video
Here is the whole documentation of YouTube's API:
https://developers.google.com/youtube/v3/sample_requests
This question already has answers here:
Firebase Custom Claims doesn't propagate
(2 answers)
Closed 3 years ago.
When assigned a custom claim, my users in the client have to log out and log in before the claims show on their account. I spent all today trying to figure out why my claims were not setting and it turns out the user has to relog in order for the claim to show LOL rip 5 hours of my day for no reason. Is there anything I can add to make it refresh the users info without them having to relog?
Custom claims may be force refreshed by calling currentUser.getIdToken(true).
From the docs...
Propagate custom claims to the client
After new claims are modified on a user via the Admin SDK, they are propagated to an authenticated user on the client side via the ID token in the following ways:
A user signs in or re-authenticates after the custom claims are modified. The ID token issued as a result will contain the latest claims.
An existing user session gets its ID token refreshed after an older token expires.
An ID token is force refreshed by calling currentUser.getIdToken(true).
Recently , the youtube API has been updated to V3 , and I have a question:
How can I get the video details (views, name , description etc..) through Javascript using the url of the video ?
Example :
I have this url https://www.youtube.com/watch?v=DQ5w8LBI0kI&ab_channel=YT
and I want to get via Javascript the name and description of it.
How can I do this ?
You can find a detailed information about how to retrieve any kind of data you want in the official documentation of youtube api.Here
The API documentation is quite involved. Two good points to start from are the following:
YouTube Data API Overview, and
JavaScript Quickstart.
Upon reading them -- and surely more for that matter -- do come back with concrete (programming!) questions.
One more hint: the API endpoint that provides you with the information attached to a given video is Videos.list. But that doc page becomes meaningful only after you familiarize yourself with the surrounding (programming) environment.
Steps to achieve:
See the answer here to see how to get/parse video Id from a youtube video url.
After you get a videoId e.g. DQ5w8LBI0kI, you have to use that in the below API to get views, name, description etc.
https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=DQ5w8LBI0kI&key=[YOUR_API_KEY]
See more about this Youtube API here
Is there any way that I can get the channel ID of a Youtube Channel by having its custom url (using the Youtube API) ?
Example:
A custom url is like:
https://www.youtube.com/onepiece
I want to get its channelID, so that I have the link like:
www.youtube.com/user/OnePieceUK
Elaborating on this answer of mine, I'll note here the following facts:
In YouTube URLs of form https://www.youtube.com/c/NAME or https://www.youtube.com/NAME, NAME is a channel's custom URL. (See this official account from Google support.)
In YouTube URLs of form https://www.youtube.com/user/NAME, NAME is a channel's user name. User names are a legacy feature of the API v3; not every channel has one attached; no channel is required to have one attached. (See this official statement from Google staff from 2013-07-11.)
The two API concepts -- custom URLs and user names -- encompass two different categories.
The public (MIT licensed) Python 3 script youtube-search.py referred by my answer quoted above is able to search the API for custom URLs and respectively query the API for user names:
$ python3 youtube-search.py --custom-url onepiece
UC6LPb3zSebrzU_0Yclpwb4Q
$ python3 youtube-search.py --user-name OnePieceUK
UC6LPb3zSebrzU_0Yclpwb4Q
Note that youtube-search.py requires a valid API key to be passed to it as argument of the command line option --app-key or, otherwise, passed on as the environment variable YOUTUBE_DATA_APP_KEY. (Use the command line option --help for brief helping info.)
try this
import requests
from bs4 import BeautifulSoup
resp = requests.get('https://www.youtube.com/onepiece')
soup = BeautifulSoup(resp.text, 'html.parser')
channel_id = soup.select_one('meta[property="og:url"]')['content'].strip('/').split('/')[-1]
I recently had a similar requirement where I knew the custom URL of a youtube channel and I want to get the channel id. So I went to the YouTube channel and clicked on a random video to get the Video ID from the web browser url. Once I get the Video Id, then I used the "Videos: list" API to get the video details:
https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id=[VEDIO_ID]&key=[YOUR_API_KEY]
The retuned data contained all the metadata information about the video, including ChannelId :-).
It may not be the most smartest and elegant way of doing things, but it solved my purpose.
I'm trying to get a full list of watched videos for a given user in my YouTube API application. I want to add up total duration of all videos.
When I get the list of videos from history playlist, the API caps it at 50 items. There's pagination but total amount of items is 50 (not just per page); I can't access more data with the API it appears.
Is there any way I can get this playlist without the data cap? I'm hoping for another method (of using the API) or a way to do it without the API. I know YouTube stores this data because I can view my entire history (far more that 50 videos).
I'm using this code:
var requestOptions = {
playlistId: playlistId,
part: 'snippet',
maxResults: 50
};
gapi.client.youtube.playlistItems.list(requestOptions);
where playlistId is the id of the history playlist I got from a gapi.client.youtube.channels.list request.
Edit (2017): I want to clarify that it was always my intention to download my own history, just out of interest to see how much time I have spent watching videos. I still have not been able to do this.
The API currently only retrieves the last two weeks of Watch History. For more information refer to the Bug Issue reported: https://code.google.com/p/gdata-issues/issues/detail?id=4642
Note:
There is a similar question on SO asked here: YouTube API v3 returns truncated watch history
I wrote a scraper(in Python 2.7(updated for 3.5) and Scrapy) for this task a while ago.
Sans official API, it uses a logged in session cookie and html parsing. Dumps to SQLite by default.
https://github.com/zvodd/Youtube-Watch-History-Scraper
How it's done: essentially it opens the url
https://www.youtube.com/feed/history'
with a valid(logged in) session cookie taken from Chrome. Scrapes all video entries for name, vid(url), channel/user, description, length. Then it finds the button at the bottom of the page with the attribute data-uix-load-more-href which contains the link to the next page, something like:
"/browse_ajax?action_continuation=1&continuation=98h32hfoasau0fu928hf2hf908h98hr%253D%253D&target_id=item-section-552363&direct_render=1"
... re-scrapes the video entries from there and dumps them all into an sqlite database; which you can search entries by any of the fields (name, length, user, description, etc).
So until they change their feed/history page, it's doable and done.
I might even update it.
While this isn't currently possible using just the YouTube API, there is an (albeit slightly involved) method to calculate your watch time):
download a list of your watch history as a JSON file using Google Takeout.
Unfortunately the JSON file doesn't include the video durations, so the next step is to extract the video IDs (the part after "watch?v=" in the "titleURL" object
Now take your list of video IDs, and send a request to the youtube API that looks something like this:
function execute() {
return gapi.client.youtube.videos.list({
"part": [
"contentDetails"
],
"id": [
"VIDEO IDs"
],
"fields": "items(contentDetails(duration))"
})
(Code created using YouTube API Explorer)
Note: You may need to break the list of video IDs into smaller lists (I had to) or the API may reject the request. As [pointed out by stvar in the comments] the ID list maximum length is 50, so this is the maximum length your lists can be. (full disclosure: I was using Python to send the requests)
Finally, just extract the duration values and add them up (though this might not be quite as easy as it sounds)
The best part of this is I don't believe this actually violates any ToS.
It seems like this is a known bug originally reported in 2013. The exact same behavior is explained on a Google Code thread: https://code.google.com/p/gdata-issues/issues/detail?id=4642
Brainstorming, never tried: Have you tried not using the API and instead parsing the https://www.youtube.com/feed/history URL?
Theoretically, the user browsing could be emulated, including the pagination. I am not aware of how hard though (probably very), since you need to deal with authentication and YouTube probably tries to verify that a human is browsing.
I was looking for some way to get the list of YouTube history.
I just found out that Google has a tool for this. In Google Takeout you have a option taht you can get the entire list of watched videos. My list went back util 2011.
To get explanation short there are two videos explaining how to do this:
https://www.youtube.com/watch?v=zlzzO1e6dws
https://www.youtube.com/watch?v=dto8jGMxHxY