Optimisation of google api requests to retrieve data from youtube videos - optimization

I'm trying to build a website with a section that displays the last video of a youtube channel and the thumbnails and titles of the next 9 most recent videos. The problem I'm having is that I consume the quota of requests just by testing the website.
To get this information I use Google's API. I retrieve the last 10 videos of the channel as follows:
<?PHP
include('global_variables.php');
$randomNumber = rand(2,10);
$randomKey = "api.key$randomNumber";
$data = file_get_contents("https://www.googleapis.com/youtube/v3/search?key=".$GLOBALS['api_key']."&part=snippet&channelId=".$GLOBALS['channel_id']."&order=date&maxResults=".$GLOBALS['videos_in_playlist']);
$json = json_decode($data);
echo $data;
?>
I call this PHP file in these ways:
// TO LOAD PLAYING YOUTUBE VIDEO
function loadVideo(iframe) {
$.getJSON("load_playing_video.php",
function(data) {
var videoNumber = (iframe.getAttribute('vnum') ? Number(iframe.getAttribute('vnum')) : 0);
iframe.setAttribute("src", "https://youtube.com/embed/" + data.items[videoNumber].id.videoId+ "?controls=1&autoplay=1&color=white");
// PLACE VIDEO TITLE
$("#playing-video-title").html(data.items[videoNumber].snippet.title);
// PLACE VIDEO DESCRIPTION
$("#playing-video-description").html(data.items[videoNumber].snippet.description);
}
);
}
loadVideo(document.querySelector("#playing-video"));
// TO LOAD THUMBNAILS AND TITLES ON SIDEBAR
$.getJSON("load_playing_video.php",
function(data) {
for (var index = 0; index < <?php echo $GLOBALS['videos_in_playlist']?>; index++) {
var videoImage = data.items[index].snippet.thumbnails.default.url;
var title = data.items[index].snippet.title;
var imgId = "#playlist-thumbnail" + index;
var titleId = "#playlist-title" + index;
document.querySelector(imgId).setAttribute("src",videoImage);
document.querySelector(titleId).innerHTML = title;
}
}
);
I was hoping someone could help me optimise the number of requests I do.
And if someone knows how I could increase the quota of requests (even if I have to pay) I would really appreciate some guidelines.
Thank you!!

Related

How can I totally stop my video files downloadable from my laravel 8 website project?

I have a Laravel 8 web project which need to stop downloading video files from my website. I need a function which will totally stop download & will say "The requested file doesn't exist" if the video file link input on IDM or at others downloader softwares. Tell me how can I stop?
View page & Controller code:
<video oncontextmenu="return false;" src="{{asset('uploads/drama-movie-video/trailers/'. $video->trailer)}}" controls controlsList="nodownload" width="250px" height="250px" poster="{{asset('images/profile1.jpg')}}">
</video>
**My video controller:**
public function show($id) {
//views count
$oldviews = Video_views::where('video_id', $id)->value('amount');
$newviews = $oldviews + 1 ;
$updateviews = Video_views::where('video_id', $id)
->update(['amount' => $newviews]);
//seen count
$visitor = request()->ip();
$searchvisitorip = Video_seen::where('video_id', $id)->where('visitor_ip', $visitor)->value('id');
if ($searchvisitorip < 1) {
$seen = new Video_seen;
$seen->video_id = $id;
$seen->visitor_ip = $visitor;
$seen->save();
}
$video = Video::find($id);
$user = Auth::User()->id;
$queryviewer= Viewer::where('video_id', $video->id)->where('viewer_id', $user)->value('viewer_id');
return view('frontend.pages.video.video.show', compact('video', 'queryviewer', 'user', 'newviews'));
}

Fetch users liked Instagram-images after API update 2016

Hey!
After Instagram updated its API on the 1. june 2016 we are not able to get out JavaScript Instagram implementation to work. We inspected the client-setup and noticed that it has been put in Sandbox mode. When trying to resubmit then none of the valid user cases did fit, and we understood that they had removed our rights to fetch other peoples content through the account without getting permission from each individual owner.
As far as i understood, the new parameter "scope" needs to be set to "public_content" and this requires the users permission.
So at the moment the solution below only fetches the users own pictures that is self-liked. Before we also got other peoples images that the user had liked.
So, do anyone know how to fetch liked content of an account post 1. june 2016?
Does getting a Instagram Business account help you for displaying other peoples content?
Here is the code example with an additional filter on the hashtags.
If anyone got a better idea of how to filter on hashtags and liked content at the same time please let me know :)
$.fn.instagramFeedInit = function()
{
if(!$('#instagram-content').length)
{
return;
}
var container = $('#instagram-content');
var tag = container.data('hashtag');
var image_display_count = container.data('image-count');
var access_token = [INSERT_TOKEN_OR_SOUTH_PARK_REFERENCE_HERE];
$.ajax({
type:'GET',
url: 'https://api.instagram.com/v1/users/self/media/liked?access_token=' + access_token + '&callback=callback&count=99999',
jsonp: 'callback',
dataType: 'jsonp',
success: function(response) {
var container = $('#instagram-content');
var i = 0;
// Loops through images
$(response.data).each(function(i, el){
// Check that it has correct tag
if(jQuery.inArray(tag, el.tags) !== -1)
{
var imageTitle = el.caption.text;
var image = el.images.low_resolution.url;
var refImage = el.images.standard_resolution.url;
if (el.type === 'video') {
var videoLink = el.videos.standard_resolution.url;
container.append('<a class="lightbox instagramimage mfp-iframe" title="' + imageTitle + '" href="' + videoLink + '" ><img src="' + image + '"></a>');
}
else {
container.append('<a class="lightbox instagramimage" title="' + imageTitle + '" href="' + refImage + '" ><img src="' + image + '" class="img-responsive"></a>');
}
i++;
// If we have reached maximum display images
if(i == image_display_count) return false;
}
});
}
});
};
Be aware that if you are in Sandbox mode, only post from authorized Sandboxed users will appear in any api requests. Regardless of whether you have the public_access scope.
Note: you need to add scope=public_access to your authorization URL now. It's no longer included by default
E.g A and B are both sandbox users C is not.
A likes a post from C.
The liked media endpoint for A will not include the post from C. He is invisible to your sandbox app
A then goes an likes a post from B
The liked media endpoint for A will now include the post from B as he is visible to your sandbox app

How to work with youtube api V3

I need help with youtube API V3.
as when on the browser I type:
https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Titanic%201997%20Official%20Trailer&key=
it's show return values.
However I am trying to collect the array from php.
How can I use GET https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Titanic%201997%20Official%20Trailer&key= in php so I get the result in php?
or is there any option to get the search result in a rss feed format.
Thanks in advance
Make a simple video search on YouTube API and you will get video title, description, video id and image source so best approach full code is given below.
<?php
error_reporting(0);
$search = "Search Query"; // Search Query
$api = "YouTube API Key"; // YouTube Developer API Key
$results = 10; // Max results
$link = "https://www.googleapis.com/youtube/v3/search?safeSearch=moderate&order=relevance&part=snippet&q=".urlencode($search). "&maxResults=$results&key=". $api;
$video = file_get_contents($link);
$video = json_decode($video, true);
foreach ($video['items'] as $data){
$title = $data['snippet']['title'];
$description = $data['snippet']['title'];
$vid = $data['id']['videoId'];
$image = "https://i.ytimg.com/vi/$vid/default.jpg";
// Output Title/Description/Image URL If Video ID exist
if($vid){
echo "Title: $title<br />Description: $description<br />Video ID: $vid<br />Image URL: $image<hr>";
}
}
?>

Recieving very slow response from twitter usertimeline requests, is the twitter API slow?

I am retrieving tweets from multiple accounts (around 20) and displaying them on a page. The request are very slow and my page takes one to two minutes to load. I am using the twitteroauth library (PHP). If i reduce the number of accounts, the loading time kind of decreases.
Here's the function
//twitter credentials and connection
$consumer_key = variable_get('tw_consumer_key', 'xxxxxxxxxxx'); //consumer key
$consumer_secret = variable_get('tw_consumer_secret', 'xxxxxxx'); // consumer secret
$oauth_token = variable_get('tw_access_token', 'xxxxxxxxxxxx'); //oAuth Token
$oauth_token_secret = variable_get('tw_access_token_secret', 'xxxxxxxxxx'); //oAuth Token Secret
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
$connection->host = "https://api.twitter.com/1.1/";
//Retrieve feeds now
foreach ($twitter_accounts as $account_twitter) {
if (!empty($account_twitter['lien'])) {
$page_url = $account_twitter['lien'];
$twitter_name = $account_twitter['compte'];
$query = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $twitter_name . '&exclude_replies=true&include_rts=true&include_entities=true';
$content = $connection->get($query);
if (sizeof($content) > 0 && empty($content->errors)) {
$tw_tweets['posts'] = $content;
$tw_tweets['url'] = $page_url;
$twitter_feeds[] = $tw_tweets;
}//end if sizeof
else {
if (!empty($content->errors)) {
$error = '';
$error = (isset($content->errors[0]->message)) ? $content->errors[0]->message : '';
$error .= (isset($content->errors[0]->code)) ? ' code' . $content->errors[0]->code : '';
watchdog('ffbb_hubsocial', 'Twitter Account ' . $account_twitter['compte'] . ' failed to return results :' . $error);
}
}
}
}
Is the API slow ?
Anyone knows if the problem is with twitter ?
Duplicate question: Why are the Twitter api calls so slow?
As the above answer states, try testing the URL in your browser and see how long it takes. Hence, you'll be able to see if the issue is on your side or due to Twitter.

Twitter API - pull hashtags from a single user

I am trying to pull all tweets that include #multitouch from a single twitter. (not just pull everyone that ever tweets using #multitouch, just from one person)
my code right now [which just pulls by hashtag]:
$(document).ready(function () {
$.getJSON('http://search.twitter.com/search.json?q=%23multitouch:&screen_name=25KDIGITAL:&callback=?', function (json) {
for(i = 0; i < 3; i++)
{
$("#TweetBar").append("<li>" + json.results[i].text + "</li>", "<p>" + json.results[i].from_user + "</p>");
}
//$("#TweetBar").show(2500, 'linear');
});
});
is this at all possible? thanks!
Sure, just make sure you're using the right URL to pull the data
http://search.twitter.com/search.json?q=%23multitouch%20from:25KDIGITAL
(query: "#multitouch from:25KDIGITAL")