How to schedule a video with Youtube API - youtube

I'll like to programmatically schedule a video once uploaded via youtube API... I know the videoId.
Here is my code:
$videos = $youtube->videos->listVideos("snippet, localizations",
array('id' => $videoId));
// Since the request specified a video ID, the response only
// contains one video resource.
$video = $videos[0];
$playlistItemResponse = $youtube->playlistItems->update("snippet, contentDetails, status", $video);
//$playlistItem:Google_Service_YouTube_PlaylistItem
$contentDetails=$playlistItemResponse->getContentDetails();
$contentDetails->setStartAt("2024-12-24T05:42:08.000Z");
I'm getting an error that playlistItems->update requires a playlist not a video...
<b>Fatal error</b>: Uncaught TypeError: Argument 2 passed to Google\Service\YouTube\Resource\PlaylistItems::update() must be an instance of Google\Service\YouTube\PlaylistItem, instance of Google\Service\YouTube\Video given, called in /var/www/html/API/publishCompilation.php on line 217 and defined in /var/www/html/vendor/google/apiclient-services/src/YouTube/Resource/PlaylistItems.php:157
Stack trace:
#0 /var/www/html/API/publishCompilation.php(217): Google\Service\YouTube\Resource\PlaylistItems->update('snippet, conten...', Object(Google\Service\YouTube\Video))
#1 {main}
thrown in <b>/var/www/html/vendor/google/apiclient-services/src/YouTube/Resource/PlaylistItems.php</b> on line <b>157</b><br />
How is the correct way of scheduling a single video?
Thanks

Related

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?

Is there a way to get the last chat massage in shout or say?

I want to search the last message for a few strings and then echo the message back with those strings replaced with other strings.
I searched multiple documentations but didn't find a way to get the last message.
This is the first forum I ask as I already have an account so have no real starting point to give you.
Thanks in advance!
There is no way in the WoW API to get the last chat message of a specific channel. You will have to handle the CHAT_MSG_CHANNEL event (see Event Handling) to read all messages, and store the newest one. Specifically for the say or yell (shout) channels there are the CHAT_MSG_SAY and CHAT_MSG_YELL events respectively.
To do this your addon needs to own a Frame, these frames can register event handlers and you will have to store the last message you receive from that handler in a local variable in your script (let's call it last_message). Then when your other piece of code executes you can read the last_message variable:
local frame = CreateFrame("FRAME", "FooAddonFrame");
local last_message = nil;
frame:RegisterEvent("CHAT_MSG_CHANNEL");
local function eventHandler(self, event, ...)
-- Look up the arguments that are given to your specific event
-- and assign them to variables in order by retrieving them from
-- the `...` variable arguments
local msg, author, language, channel = ...
print("Hello World! Hello " .. event);
last_message = msg
end
frame:SetScript("OnEvent", eventHandler);

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.

youtube api - loop throug huge data

I need to retrieve the 'view count' for each video channel , and I’m using this library .
this is my code
okay the code works fine and print me the view count foreach video , except that i got these warnings with some other videos without printing the view count
A PHP Error was encountered Severity: Warning Message:
simplexml_load_string() [function.simplexml-load-string]: Entity:
line 547: parser error : attributes construct error
Message:
simplexml_load_string() [function.simplexml-load-string]:
outube_gdata'/>
Message:
simplexml_load_string() [function.simplexml-load-string]: ^
Message:
simplexml_load_string() [function.simplexml-load-string]: Entity:
line 547: parser error : Couldn't find end of Start Tag link line 547
Message: simplexml_load_string() [function.simplexml-load-string]:
outube_gdata'/>
how can i deal with this large number of videos and channels without causing this warning msgs and lost in time , cause if i tried the same code on one channel with fewer videos i got no errors
$channels=array('google','apple','mac','xyz','abc','test');
for ($j=0; $j<count($channels) $j++)
{
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/users/".$channels[$j]."/uploads?v=2&alt=jsonc&max-results=0");
$JSON_Data = json_decode($JSON);
$total_videos = $JSON_Data->{'data'}->{'totalItems'};
for($i=1; $i<=$total_videos; )
{
$this->get_userfeed($channels[$j],$maxresult=20,$start=$i);
$i+=20;
}
}
public function get_userfeed($ch_id,$maxresult=10,$start=0,$do=null)
{
$output = $this->youtube->getUserUploads($ch_id, array('max-results'=>$maxresult,'start-index'=>$start));
$xml = simplexml_load_string($output);
// single entry for testing
foreach($xml->entry as $entry)
{
foreach($entry->id as $key=>$val)
{
$id = explode('videos/', (string)$val);
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/".$id[1]."?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$v_count = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
if($v_count == NULL) $v_count =0;
echo $v_count;
// store the v_count into database
}
}
}
You're doing a few things wrong.
First off, if you want to minimize the number of calls you're making to the API, you should be setting max-results=50, which is the largest value that the API supports.
Second, I don't understand why you're making individual calls to http://.../videos/VIDEO_ID to retrieve the statistics for each video, since that information is already returned as part of the video entries you're getting from the http://.../users/USER_ID/uploads feed. You can just store the values returned by that feed and avoid having to make all those additional calls to retrieve each video.
Finally, the underlying issue is almost certainly that you're running into quota errors, and you can read more about them at http://apiblog.youtube.com/2010/02/best-practices-for-avoiding-quota.html
Taking any of the steps I mention should cut down on the total requests that you're making and potentially get around any quota problems, but you should familiarize yourself with the quota system anyway.

Resources