Hello.
Today, I continue from
Today, I continue from
"{\n \"kind\": \"youtube#playlistItemListResponse\",\n \"etag\": \"s6QcrvkzWWOAgqLpxGuOTwmYE-s\",\n \"items\": [\n {\n \"kind\": \"youtube#playlistItem\",\n \"etag\": \"bXmH501ZhdvFDcAuhNF1e2hnFTM\",\n \"id\": \"UEx3akRSUDVaWF9NRU9FLXpPdVRJTU9WSDBWT1lnR182OS41NkI0NEY2RDEwNTU3Q0M2\"\n }\n ],\n \"pageInfo\": {\n \"totalResults\": 1,\n \"resultsPerPage\": 5\n }\n}\n"
Human can read values of "totalResults" and "id" from this response body. My previous post was about how to get the playlist item id.
But how do you do that in the code? We can start from there.
require 'json' JSON.parse("{\n \"kind\": \"youtube#playlistItemListResponse\",\n \"etag\": \"s6QcrvkzWWOAgqLpxGuOTwmYE-s\",\n \"items\": [\n {\n \"kind\": \"youtube#playlistItem\",\n \"etag\": \"bXmH501ZhdvFDcAuhNF1e2hnFTM\",\n \"id\": \"UEx3akRSUDVaWF9NRU9FLXpPdVRJTU9WSDBWT1lnR182OS41NkI0NEY2RDEwNTU3Q0M2\"\n }\n ],\n \"pageInfo\": {\n \"totalResults\": 1,\n \"resultsPerPage\": 5\n }\n}\n") => {"kind" => "youtube#playlistItemListResponse", "etag" => "s6QcrvkzWWOAgqLpxGuOTwmYE-s", "items" => [{"kind" => "youtube#playlistItem", "etag" => "bXmH501ZhdvFDcAuhNF1e2hnFTM", "id" => "UEx3akRSUDVaWF9NRU9FLXpPdVRJTU9WSDBWT1lnR182OS41NkI0NEY2RDEwNTU3Q0M2"}], "pageInfo" => {"totalResults" => 1, "resultsPerPage" => 5}}
You can totally do the same at home. By the way JSON means the format of response body in our case. (Other APIs may send a different format of body in their response)
So when you get the id from your code
require 'net/http' require 'uri' require 'json' path = "/youtube/v3/playlistItems" host = "www.googleapis.com" params = "part=id&playlistId=PLwjDRP5ZX_MEOE-zOuTIMOVH0VOYgG_69" uri = URI::HTTPS.build(host: host, path: path, query: params) http_request = Net::HTTP::Get.new(uri.request_uri) response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| http.request http_request end parsed_json = JSON.parse(response.body) first_item_id = parsed_json["items"][0]["id"] => "UEx3akRSUDVaWF9NRU9FLXpPdVRJTU9WSDBWT1lnR182OS41NkI0NEY2RDEwNTU3Q0M2" item_count = parsed_json["pageInfo"]["totalResults"] => 1
So, to accomplish the goal of this post, how about checking a privacy status ("private", "unlisted", or "public") of the video.
There's YouTube Data API documentation for that.
string The part parameter specifies a comma-separated list of one or more playlistItem resource properties that the API response will include. If the parameter identifies a property that contains child properties, the child properties will be included in the response. For example, in a playlistItem resource, the snippet property contains numerous fields, including the title, description, position, and resourceId properties. As such, if you set part=snippet, the API response will contain all of those properties. The following list contains the part names that you can include in the parameter value:
- contentDetails
- id
- snippet
- status
Here "status" is where the privacy status is supposed to be. It can be more complicated than that, I think.
params = "part=id,status&playlistId=PLwjDRP5ZX_MEOE-zOuTIMOVH0VOYgG_69" ... parsed_json = JSON.parse(response.body) first_item = parsed_json["items"][0] => {"kind" => "youtube#playlistItem", "etag" => "od889XrSnwF1FDP0l0xb7zpIZEI", "id" => "UEx3akRSUDVaWF9NRU9FLXpPdVRJTU9WSDBWT1lnR182OS41NkI0NEY2RDEwNTU3Q0M2", "status" => {"privacyStatus" => "public"}} first_item["status"]["privacyStatus"] => "public"
Oh, the video is "public." In that case, I am not going to delete that item. I don't delete public videos. You still can, but I usually don't.
😁
By the way, deleting playlist item doesn't delete the video. It just removes the entry from the list.
If you copy and pasted (to your ruby prompt "irb" or "rails console") to run all the code I showed, you may have found they are not working. You may have seen some errors.
response => #<Net::HTTPUnauthorized 401 Unauthorized readbody=true>
This error (or only a response saying "401 Unauthorized" to you) is for safety. Of course, YouTube does not allow anyone else to delete your playlist item without your permission.
So, I should not avoid telling you how to login into the API ("how to authenticate" in other words)
Bye. Thank you for reading. See you next time!