YouTube video API not working with IDs beginning with a dash - youtube

I am accessing data from YouTube's API, I have everything working fine but the problem I'm having is that when there's a dash (-) at the beginning of the videoID that it's not returning the json data.
$videoID = -FIHqoTcZog;
$json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$videoID}&alt=json"));
I am however able to return the thumbnail as always with it using this:
$thumbnail = "http://i4.ytimg.com/vi/".$videoID."/mqdefault.jpg";
This is the code that I use to pull the information from the above json that I want.
$title = $json->{'feed'}->{'entry'}[0]->{'title'}->{'$t'};
$description = $json->{'feed'}->{'entry'}[0]->{'media$group'}->{'media$description'}->{'$t'};
$thumbnail = "http://i4.ytimg.com/vi/".$videoID."/mqdefault.jpg";
$ratings = ((round($json->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'average'}, 1)/$json->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'max'})*100)."%";
$views = number_format($json->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'});
$duration = $json->{'feed'}->{'entry'}[0]->{'media$group'}->{'yt$duration'}->{'seconds'};

Are you sure you're only getting a problem with IDs that have a dash in front of it? The code you pasted shouldn't be working with any Youtube ID, because the gdata feed returns, as part of the JSON, some text with the '$' character in it. That character is a PHP identifier, so you'll get 500 errors trying to run the json_decode function on whatever the feed returns.
One way to solve the problem is to use json_decode's 2nd parameter to give you an associative array rather than an object, like this:
$json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$videoID}&alt=json"),true);
Of course, that requires you to work with an array, too, but the subsequent code changes should be minimal.
If you aren't getting errors with other videos using the exact same code, perhaps you could post it here?

Related

YouTube API - retrieve more than 5k items

I just want to fetch all my liked videos ~25k items. as far as my research goes this is not possible via the YouTube v3 API.
I have already found multiple issues (issue, issue) on the same problem, though some claim to have fixed it, but it only works for them as they don't have < 5000 items in their liked video list.
playlistItems list API endpoint with playlist id set to "liked videos" (LL) has a limit of 5000.
videos list API endpoint has a limit of 1000.
Unfortunately those endpoints don't provide me with parameters that I could use to paginate the requests myself (e.g. give me all the liked videos between date x and y), so I'm forced to take the provided order (which I can't get past 5k entries).
Is there any possibility I can fetch all my likes via the API?
more thoughts to the reply from #Yarin_007
if there are deleted videos in the timeline they appear as "Liked https://...url" , the script doesnt like that format and fails as the underlying elements dont have the same structure as existing videos
can be easily fixed with a try catch
function collector(all_cards) {
var liked_videos = {};
all_cards.forEach(card => {
try {
// ignore Dislikes
if (card.innerText.split("\n")[1].startsWith("Liked")) {
....
}
}
catch {
console.log("error, prolly deleted video")
}
})
return liked_videos;
}
to scroll down to the bottom of the page ive used this simple script, no need to spin up something big
var millisecondsToWait = 1000;
setInterval(function() {
window.scrollTo(0, document.body.scrollHeight);
console.log("scrolling")
}, millisecondsToWait);
when more ppl want to retrive this kind of data, one could think about building a proper script that is more convenient to use. If you check the network requests you can find the desired data in the response of requests called batchexecute. One could copy the authentification of one of them provide them to a script that queries those endpoints and prepares the data like the other script i currently manually inject.
Hmm. perhaps Google Takeout?
I have verified the youtube data contains a csv called "liked videos.csv". The header is Video Id,Time Added, and the rows are
dQw4w9WgXcQ,2022-12-18 23:42:19 UTC
prvXCuEA1lw,2022-12-24 13:22:13 UTC
for example.
So you would need to retrieve video metadata per video ID. Not too bad though.
Note: the export could take a while, especially with 25k videos. (select only YouTube data)
I also had an idea that involves scraping the actual liked videos page (which would save you 25k HTTP Requests). But I'm unsure if it breaks with more than 5000 songs. (also, emulating the POST requests on that page may prove quite difficult, albeit not impossible. (they fetch /browse?key=..., and have some kind of obfuscated / encrypted base64 strings in the request-body, among other parameters)
EDIT:
Look. There's probably a normal way to get a complete dump of all you google data. (i mean, other than takeout. Email them? idk.)
anyway, the following is the other idea...
Follow this deep link to your liked videos history.
Scroll to the bottom... maybe with selenium, maybe with autoit, maybe put something on the "end" key of your keyboard until you reach your first liked video.
Hit f12 and run this in the developer console
// https://www.youtube.com/watch?v=eZPXmCIQW5M
// https://myactivity.google.com/page?utm_source=my-activity&hl=en&page=youtube_likes
// go over all "cards" in the activity webpage. (after scrolling down to the absolute bottom of it)
// create a dictionary - the key is the Video ID, the value is a list of the video's properties
function collector(all_cards) {
var liked_videos = {};
all_cards.forEach(card => {
// ignore Dislikes
if (card.innerText.split("\n")[1].startsWith("Liked")) {
// horrible parsing. your mileage may vary. I Tried to avoid using any gibberish class names.
let a_links = card.querySelectorAll("a")
let details = a_links[0];
let url = details.href.split("?v=")[1]
let video_length = a_links[3].innerText;
let time = a_links[2].parentElement.innerText.split(" • ")[0];
let title = details.innerText;
let date = card.closest("[data-date]").getAttribute("data-date")
liked_videos[url] = [title,video_length, date, time];
// console.log(title, video_length, date, time, url);
}
})
return liked_videos;
}
// https://stackoverflow.com/questions/57709550/how-to-download-text-from-javascript-variable-on-all-browsers
function download(filename, text, type = "text/plain") {
// Create an invisible A element
const a = document.createElement("a");
a.style.display = "none";
document.body.appendChild(a);
// Set the HREF to a Blob representation of the data to be downloaded
a.href = window.URL.createObjectURL(
new Blob([text], { type })
);
// Use download attribute to set set desired file name
a.setAttribute("download", filename);
// Trigger the download by simulating click
a.click();
// Cleanup
window.URL.revokeObjectURL(a.href);
document.body.removeChild(a);
}
function main() {
// gather relevant elements
var all_cards = document.querySelectorAll("div[aria-label='Card showing an activity from YouTube']")
var liked_videos = collector(all_cards)
// download json
download("liked_videos.json", JSON.stringify(liked_videos))
}
main()
Basically it gathers all the liked videos' details and creates a key: video_ID - Value: [title,video_length, date, time] object for each liked video.
It then automatically downloads the json as a file.

YouTube API: differentiating between Premiered and Livestream

I am using YouTube data API and trying to differentiate prior livestreams vs premiered content. The liveStreamingDetails in the video list is populated for both livestreams and premiered content. Is there a way I can differentiate between the two?
Below is my python code for getting live stream start time. If its not populated, then I know that video is not live stream. But the problem is that this value is getting populated for premiered content as well.
vid_request = youtube.videos().list(part = 'contentDetails, statistics, snippet, liveStreamingDetails, status',id = ','.join(vid_ids))
vid_response = vid_request.execute()
for videoitem in vid_response['items']:
try:
livestreamStartTime = videoitem['liveStreamingDetails']['actualStartTime']
except:
livestreamStartTime = ''
Any pointers on what could work would really help?

how to Add titles and descriptions to videos on YouTube in other languages using api v3?

I had studied hardly the documentation on https://developers.google.com/youtube/v3/revision_history#november-19-2015 about how to Set localized titles and descriptions.
But when you try it, it seems impossible, even if you use the "app" of the api on https://developers.google.com/youtube/v3/docs/videos/update#prubalo you always get the same error with the parameter part. I set that parameter with the value "snippet", like you have to do. But it doesn't work, I tried with the rest of values or possible combinations and..it doesn't work.
Can someone give me an example of the code (i prefer python) or the request http ??
Please be sure you code o request http really works...even i found any mistakes on the examples on the documentation like 5 opening parenthesis and 4 closing parenthesis...
Following is an PHP code example. The concept is same, hope you can do it in the Phython.
Please make sure you set the default language of the video (snippet.defaultLanguage) before adding localisations.
// Call the API's videos.list method to retrieve the video resource.
// Part should be 'localizations' not 'snippet' because you are updating the localisation
$listResponse = $youtube->videos->listVideos('localizations', array('id' => 'YOUR_VIDEO_ID'));
// Since the request specified a video ID, the response only contains one video resource.
$video = $listResponse[0];
// Set the localisations array for the video localisation
// You can retrieve the language list from following API - https://developers.google.com/youtube/v3/docs/i18nLanguages/list
$video['localizations'] = array(
'ta' => array(
'title' => 'TITLE_IN_GIVEN_LANG',
'description' => 'DESC_IN_GIVEN_LANG'));
// Update the video resource by calling the videos.update() method.
$updateResponse = $youtube->videos->update('localizations', $video);
Update - Example of updating localisation of video using google developer console

Get length of videos calling youtube playlist API (v3)

$http.get("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLFgquLnL59alCl_2TQvOiD5Vgm1hCaGSI&key={mykey}&maxResults=10")
I used the playlistItems but couldn't get the statistic part which contain duration of the video. Do I need to call twice? Get the video Id and make another call? or I'm missing something in this case?
For whatever reason, playlistItems do not include some things like statistics or category. You'll need to make a separate call using the video ID and https://developers.google.com/youtube/v3/docs/videos/list in order to get those fields.
This is how I do it (using Python but you can adapt it for whatever language you are using with http requests and JSON parsing)
url = "https://www.googleapis.com/youtube/v3/videos?id=" + videoId
+ "&key=" + DEVELOPER_KEY + "&part=snippet,contentDetails"
r = requests.get(url)
metadata = r.json()["items"][0]
channelName = metadata["snippet"]["channelTitle"]
publishedTime = metadata["snippet"]["publishedAt"]
duration = metadata["contentDetails"]["duration"]
duration is in a strange format that looks like
PT4M11S
meaning 4 minutes 11 seconds. You will have to "parse" this.

My xpath query string doesn´t work

I would like to grab the following value on this website with xquery. After trying for awhile I couldn't figure it out. Here is what I am trying to fetch (image link)
And the code I tried using:
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "???????????????" );
Any ideas?
You can try to grep the value between script tags
Like
//form/div/div/div[#class="values span-7"]/skipt/Text()
After parse the value in PHP other language hat you use to get your string
Can you get the value off of the input?
//form//input[#name="val7"]/#value
Update:
You can get the stuff in the script tag, but that's as far as you can get using xpath. You'll then need to parse the contents of the script tag in order to get that value.
//form[#id="werte"]//div[#class="calval7"]//div[#class="values"][1]/script/text()

Resources