Youtube Data API is returning invalid data - youtube-api

I'm not really sure how to ask this, but I have a php script that pulls data from the youtube-v3-api for my youtube channel, mainly a list of videos that I have published. It's been working great up until earlier today when I went to run it again because I added a new video. Here's the output for the first object in the items array
items:[
{
kind:"youtube#searchResult",
etag:"" XpPGQXPnxQJhLgs6enD_n8JR4Qk/tluoWYe5GE9lVFAkcMtcec2Ycug"",
id:{
kind:"youtube#channel",
channelId:"UCD8d3FGC907iS1qiMF0ccxA"
},
snippet:{
publishedAt:"2006-04-30T19:39:08.000Z",
channelId:"UCD8d3FGC907iS1qiMF0ccxA",
title:"Travis Ballard",
description:"",
thumbnails:{
default:{
url:"https://yt3.ggpht.com/-L5MV7tUNjlk/AAAAAAAAAAI/AAAAAAAAAAA/dXNuqxAYprw/s88-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
medium:{
url:"https://yt3.ggpht.com/-L5MV7tUNjlk/AAAAAAAAAAI/AAAAAAAAAAA/dXNuqxAYprw/s240-c-k-no-mo-rj-c0xffffff/photo.jpg"
},
high:{
url:"https://yt3.ggpht.com/-L5MV7tUNjlk/AAAAAAAAAAI/AAAAAAAAAAA/dXNuqxAYprw/s800-c-k-no-mo-rj-c0xffffff/photo.jpg"
}
},
channelTitle:"Travis Ballard",
liveBroadcastContent:"none"
}
}
]
This does not represent a video on my channel? also, it's not in order at all. It should be ordered by date as i'm asking it to in my php script:
<?php
const APIKEY = 'MyAPIKeyHere';
const CHANNELID = 'UCD8d3FGC907iS1qiMF0ccxA';
const VIDEO_COUNT = 50;
class FetchYoutubeVideos {
private $api_key = null;
private $channel_id = null;
private $count = null;
public function __construct($api_key, $channel_id, $count = 10) {
$this->api_key = $api_key;
$this->channel_id = $channel_id;
$this->count = $count;
$this->writeToFile(
'videos.json',
$this->fetch($this->getApiUrl())
);
printf( 'fetched videos from: %s', $this->getApiUrl());
}
public function getApiUrl($options = array()) {
$endpoint = 'https://www.googleapis.com/youtube/v3/search';
$default_options = array(
'key' => $this->api_key,
'channelId' => $this->channel_id,
'part' => 'snippet,id',
'order' => 'date',
'maxResults' => $this->count
);
$options = array_merge($options, $default_options);
return sprintf('%s/?%s', $endpoint, http_build_query($options));
}
public function fetch($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
public function writeToFile($filename, $data) {;
$fh = fopen($filename, 'w+');
fwrite($fh, $data, strlen($data));
fclose($fh);
}
}
$fetchVideos = new FetchYoutubeVideos(APIKEY, CHANNELID, VIDEO_COUNT);
The newest video should be 'Tragic - Rock-a-Hoola Revisited' and then 'Rocket Timelapse - Anycubic i3 Mega'. You can reference my yt channel to see what it should be returning: https://www.youtube.com/channel/UCD8d3FGC907iS1qiMF0ccxA?view_as=subscriber
Any insights into why it changed? Or why it's not returning the data that it should be?

You experienced an API issue which is known for a few days now. Follow-up https://issuetracker.google.com/issues/128673552, or the answers already given on this site (e.g. https://stackoverflow.com/a/55246970/8327971).
Either of the two links we'll lead you to find a workaround for the issue at hand.
Note that, until now, Google has refrained itself from providing an ETA for when it'll enable back the API's features it disabled. I suppose that it may well take a few more days (perhaps weeks?) until we'll see those disabled features working again.

Related

Memory leak using repository in iteration (In dev env)

Hello I'm running into memory leakage when trying to create a lot of product reviews using the following code:
echo "Start: " . memory_get_usage();
foreach ($reviews['items'] as $review) {
$newReview = $this->handleReviewData($review, $languages, $salesChannels, $context);
if ($newReview) {
$reviewsToImport[] = $newReview;
}
echo "End: " . memory_get_usage();
}
$this->productReviewRepository->create($reviewsToImport, $context);
I'm running multiple queries on $this->productReviewRepository within $this->handleReviewData. So I'm guessing that somehow creates the leak, but I can't find a ->flush() or ->clear($entity) (because it isn't Symfony). Any idea's?
It adds +-1000000 bytes (1mb) of memory for each iteration.
HandleReviewData function:
public function handleReviewData($review, $languages, $salesChannels, $context): ?array
{
$reviewLocaleCode = $review['questionnaire']['locale'];
$productSku = $review['product']['sku'];
$salesChannels = $salesChannels->filter(static function ($salesChannel) use ($review) {
/* #var $salesChannel TrustApiEntity */
return $salesChannel->getChannelId() === $review['channelRef'];
});
$salesChannelId = $salesChannels->first()?->getSalesChannel()->getId();
if (!$salesChannelId) {
return null;
}
$languageId = array_search(str_replace('_', '-', $reviewLocaleCode), $languages, true);
if (!$languageId) {
return null;
}
$products = $this->productRepository
->search((new Criteria())
->addFilter(new EqualsFilter('productNumber',
$productSku))
, $context);
$productId = $products->first()?->getId();
if (!$productId) {
return null;
}
$existingReview = $this->productReviewRepository
->searchIds((new Criteria())
->addFilter(
new EqualsFilter('title', $review['title']),
new EqualsFilter('content', $review['comment'])
)
, $context)->getIds();
if (count($existingReview) > 0) {
return null;
}
return [
'productId' => $productId,
'salesChannelId' => $salesChannelId,
'languageId' => $languageId,
'externalUser' => $review['customer']['firstName'] ?? 'Anonymous',
'externalEmail' => $review['customer']['email'] ?? 'anonymous',
'title' => $review['title'],
'content' => $review['comment'],
'points' => round($review['rating'], 0),
'status' => true,
'createdAt' => new \DateTime('#' . strtotime($review['createdAt']))
];
}
I think this is intended behaviour. I encountered similiar issues when doing migrations from other systems to Shopware 6, where also a lot of data is read and written.
The issue with this is no issue in Shopware itself but the underlying Symfony framework because in dev environment the Symfony profiler is logging a lot of data for the current request which leads to huge amounts of memory used. I've seen it grow to several GB of data.

Pagination is not working in twilio call logs laravel

<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use App\Model\User;
use Illuminate\Support\Facades\Validator;
use Auth;
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
class TwilioController extends ApiController
{
public function __construct(){
$sid = env('TWILIO_SID');
$token = env('TWILIO_TOKEN');
$this->twilio = new Client($sid, $token);
if (request('lang'))
\App::setLocale(request('lang'));
}
public function callLogs(Request $request){
try{
$twilioNumber = Auth::user()->twilio_number;
$calls = $this->twilio->calls
->read([], 20);
$data = [];
$i = 0;
foreach($calls as $call){
$data[$i]['from'] = $call->from;
$i++;
}
$responseData = array('status'=>true, 'message'=>'Data has been returned successfully!', 'data'=>$data);
} catch (\Exception $e) {
$responseData = array('status'=>false, 'message'=>$e->getMessage(), 'data'=>[]);
}
$res = json_encode($responseData);
print $res;
}
Pagination is not working when i get call history in twilio using laravel rest api. when i use page parameter with this then pagination is not working it give me save output as given me 1st page.
Postman parameters - Page:2
Thanks
I think Twilio's page() function will help you. Like read(), it accepts an array of options to narrow your search along with a pageSize parameter defaulting to 50 calls per page. (I believe the maximum value is 1000.)
Here is an example.
//Get all call logs. 20 per page
$calls = $twilio->calls->page( [], 20 );
$data = [];
foreach($calls as $call){
$data[] = $call->sid;
}
//You can access the previous and next page urls using the resource functions in the calls object return from twilio.
return response()->json(["prev" => $calls->getPreviousPageUrl(), "next" => $calls->getNextPageUrl(), "calls" => $data]);

Get all clientID from MCC adwords account by adwordsAPI

I want to retrieve all clientID from my MCC account. I'm using this code
AdWordsUser user = new AdWordsUser(adwordsPropertyService.getEmail(), adwordsPropertyService.getPassword(),
null, adwordsPropertyService.getUseragent(), adwordsPropertyService.getDeveloperToken(),
adwordsPropertyService.getUseSandbox());
InfoServiceInterface infoService = user.getService(AdWordsService.V201109.INFO_SERVICE);
InfoSelector selector = new InfoSelector();
selector.setApiUsageType(ApiUsageType.UNIT_COUNT_FOR_CLIENTS);
String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
selector.setDateRange(new DateRange(today, today));
selector.setIncludeSubAccounts(true);
ApiUsageInfo apiUsageInfo = infoService.get(selector);
for (ApiUsageRecord record : apiUsageInfo.getApiUsageRecords()) {
......
But apiUsageInfo.getApiUsageRecords return my only some clientId.
Have you any suggests?
My Answer will be helpful for PHP Developers
I am using v201502(php), You will get all account details from ManagedCustomerService api. Please refer the following URL https://developers.google.com/adwords/api/docs/reference/v201502/ManagedCustomerService
This is the sample code i used,
function DisplayAccountTree($account, $link, $accounts, $links, $depth) {
print str_repeat('-', $depth * 2);
printf("%s, %s\n", $account->customerId, $account->name);
if (array_key_exists($account->customerId, $links)) {
foreach ($links[$account->customerId] as $childLink) {
$childAccount = $accounts[$childLink->clientCustomerId];
DisplayAccountTree($childAccount, $childLink, $accounts, $links,
$depth +1);
}
}
}
function GetAccountHierarchyExample(AdWordsUser $user) {
// Get the service, which loads the required classes.
$user->SetClientCustomerId('xxx-xxx-xxxx');
$managedCustomerService =
$user->GetService('ManagedCustomerService');
// Create selector.
$selector = new Selector();
// Specify the fields to retrieve.
$selector->fields = array('CustomerId', 'Name');
// Make the get request.
$graph = $managedCustomerService->get($selector);
// Display serviced account graph.
if (isset($graph->entries)) {
// Create map from customerId to parent and child links.
$childLinks = array();
$parentLinks = array();
if (isset($graph->links)) {
foreach ($graph->links as $link) {
$childLinks[$link->managerCustomerId][] = $link;
$parentLinks[$link->clientCustomerId][] = $link;
}
}
// Create map from customerID to account, and find root account.
$accounts = array();
$rootAccount = NULL;
foreach ($graph->entries as $account) {
$accounts[$account->customerId] = $account;
if (!array_key_exists($account->customerId, $parentLinks)) {
$rootAccount = $account;
}
}
// The root account may not be returned in the sandbox.
if (!isset($rootAccount)) {
$rootAccount = new Account();
$rootAccount->customerId = 0;
}
// Display account tree.
print "(Customer Id, Account Name)\n";
DisplayAccountTree($rootAccount, NULL, $accounts, $childLinks, 0);
} else {
print "No serviced accounts were found.\n";
}
}
GetAccountHierarchyExample($user);
SetClientCustomerId will be the parent ID of your all accounts, It will be appeared near the Sign Out button of you google AdWords account, Please see the attached image
I hope this answer will be helpful, Please add your comments below if you want any further help
If you need just the list of clientCustomerIds, try ServicedAccountService.
Here is a code example that shows how this may be done.
Next time, you might also want to consider asking the question on the official forum for AdWords API: https://groups.google.com/forum/?fromgroups#!forum/adwords-api

How to correctly use oembed to pull thumbs from youtube

I show a lot of thumbs on my homepage from youtube videos. I was using this function below to grab the thumb from a youtube url which works fast but it doesn't work for url's in the shortned form like youtu.be/JSHDLSKL.
function get_youtube_screen_link( $url = '', $type = 'default', $echo = true ) {
if( empty( $url ) )
return false;
if( !isset( $type ) )
$type = '';
$url = esc_url( $url );
preg_match("|[\\?&]v=([^&#]*)|",$url,$vid_id);
if( !isset( $vid_id[1] ) )
return false;
$img_server_num = 'i'. rand(1,4);
switch( $type ) {
case 'large':
$img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/0.jpg";
break;
case 'first':
// Thumbnail of the first frame
$img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/1.jpg";
break;
case 'small':
// Thumbnail of a later frame(i'm not sure how they determine this)
$img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/2.jpg";
break;
case 'default':
case '':
default:
$img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/default.jpg";
break;
}
if( $echo )
echo $img_link;
else
return $img_link;
}
So I tried to use Oembed to get the thumbs instead which works for all variations of the youtube url but it retrieves the 480px/360px thumb which causes a lot of cropping to get it down to the 120px/90px size I use to display them. The other issue was it caused my page speed to increase by 4 seconds which Im guessing is a problem with the way I'm implementing it. Here's how I call the thumb inside a loop.
<?php
require_once(ABSPATH.'wp-includes/class-oembed.php');
$oembed= new WP_oEmbed;
$name = get_post_meta($post->ID,'video_code',true);
$url = $name;
//As noted in the comments below, you can auto-detect the video provider with the following
$provider = $oembed->discover($name);
//$provider = 'http://www.youtube.com/oembed';
$video = $oembed->fetch($provider, $url, array('width' => 300, 'height' => 175));
$thumb = $video->thumbnail_url; if ($thumb) { ?>
<img src="<?php echo $thumb; ?>" width="120px" height="90px" />
<?php } ?>
So how should I be doing this to maximize efficiency?
I came across this page from youtube explaining their oembed support, They mentioned that they output to json format so I made a function that gets the json data and then enables you to use it.
Feel free to ask if you need more help.
<?php
$youtube_url = 'http://youtu.be/oHg5SJYRHA0'; // url to youtube video
function getJson($youtube_url){
$baseurl = 'http://www.youtube.com/oembed?url='; // youtube oembed base url
$url = $baseurl . $youtube_url . '&format=json'; // combines the url with format json
$json = json_decode(file_get_contents($url)); // gets url and decodes the json
return $json;
}
$json = getJson($youtube_url);
// from this point on you have all your data placed in variables.
$provider_url = $json->{'provider_url'};
$thumbnail_url = $json->{'thumbnail_url'};
$title = $json->{'title'};
$html = $json->{'html'};
$author_name = $json->{'author_name'};
$height = $json->{'height'};
$thumbnail_width = $json->{'thumbnail_width'};
$thumbnail_height = $json->{'thumbnail_height'};
$width = $json->{'width'};
$version = $json->{'version'};
$author_url = $json->{'author_url'};
$provider_name = $json->{'provider_name'};
$type = $json->{'type'};
echo '<img src="'.$thumbnail_url.'" />'; // echo'ing out the thumbnail image
Ok I came up with a solution from pieces of other questions. First we need to get the id from any type of url youtube has using this function.
function getVideoId($url)
{
$parsedUrl = parse_url($url);
if ($parsedUrl === false)
return false;
if (!empty($parsedUrl['query']))
{
$query = array();
parse_str($parsedUrl['query'], $query);
if (!empty($query['v']))
return $query['v'];
}
if (strtolower($parsedUrl['host']) == 'youtu.be')
return trim($parsedUrl['path'], '/');
return false;
}
Now we can get use YouTube Data API to get the thumbnail from the video id. Looks like this.
<?php
$vid_id = getVideoId($video_code);
$json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/$vid_id?v=2&alt=jsonc"));
echo '<img src="' . $json->data->thumbnail->sqDefault . '" width="176" height="126">';
?>
The problem is that is causing an extra 2 seconds load time so I simply use the $vid_id and place it inside http://i3.ytimg.com/vi/<?php echo $vid_id; ?>/default.jpg which gets rid of the 2 seconds added by accessing the youtube api.

Problems decoding JSON data from Twitter API (THM oauth)

I am using tmhOAuth.php / class to login into twitter. I have been successful in logging in and sending a tweet.
When I go to use the friends.php script, I am having some problems inserting into my database. I do believe that my problem lies some where with the $paging variable in the code. Because it is looping only seven times. I am following 626 people so my $ids is 626 and then $paging is 7.
When I run the php in a web browser, I am only able to extract 7 of the followers (ie following user #626,following user 526,following user 426...) It seem to be echoing the last user on each page request. This due in part to requesting 100 user ids at a time, via the PAGESIZE constant. When I adjust the $paging with different number such as the number 626 I get {"errors":[{"code":17,"message":"No user matches for specified terms"}]}
Unfortunately, I suspect this is fairly simple php looping problem, but after the amount of time I have spent trying to crack this I can no longer think straight.
Thanks in advance.
define('PAGESIZE', 100);
require 'tmhOAuth.php';
require 'tmhUtilities.php';
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$ids += $data['ids'];
$cursor = $data['next_cursor_str'];
} else {
echo $tmhOAuth->response['response'];
break;
}
endwhile;
// lookup users
$paging = ceil(count($ids) / PAGESIZE);
$users = array();
for ($i=0; $i < $paging ; $i++) {
$set = array_slice($ids, $i*PAGESIZE, PAGESIZE);
$tmhOAuth->request('GET', $tmhOAuth->url('1/users/lookup'), array(
'user_id' => implode(',', $set)
));
// check the rate limit
check_rate_limit($tmhOAuth->response);
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$name = array();
foreach ($data as $val)
{
$name = $data[0]['screen_name'];
}
echo "this is the screen name " .$name. "\n";
$users += $data;
} else {
echo $tmhOAuth->response['response'];
break;
}
}
var_dump($users);
?>
The data I am trying to echo, then parse and insert into database is the standard twitter JSON data, so I won't include this in the message. Any help would be
Problem solved:
foreach ($data as $val)
{
$name = $val['screen_name'];
echo "this is the screen name " .$name. "\n";
$users[] = $name;
}

Resources