Youtube API returns wrong value - youtube-api

This is a part of my code:
$key = 'XXX';
function check_video($id, $url) {
$x = get_data('https://www.googleapis.com/youtube/v3/videos?part=status&id='.$url.'&key='.$key);
$x = json_decode($x, true);
if($x['items'][0]['status']['embeddable'] == true){
echo "$id = TRUE --- ";
}else{
echo "$id = FALSE --- ";
}
}
check_video(1, '9bZkp7q19f0');
The function should check if the video is embeddable and then simply echo true or false but it keeps printing "1 = FALSE --- " even though a video is embeddable.
Example of video: http://i.gyazo.com/7a84e78cc665ca4920cef18e341f09b6.png

First, in your code above the variable $key in function check_video() is undefined. The variable $key has to be defined in the function or passed in. If it's just a simple copy/paste snafu then continue:
I don't see the definition for function get_code() so I'll assume it's a simple cURL wrapper.
Second, put the finished url into your browser and see if it works.
https://www.googleapis.com/youtube/v3/videos?part=status&id=9bZkp7q19f0&key=YOUR_KEY
Third, put a print_r($x) after json_decode() to see if the YouTube API is returning an error because of something in the get_code() function (like a timeout). You should check for an error regardless.
For me it worked fine.

Related

Sticky variable value in lua vim.keymap

I want to create a mapping in my neovim lua setup such that when I have a visual selection and hit <leader>ps, a telescope search is initiated with the selected text.
Here is what I have for the moment:
local builtin = require('telescope.builtin')
local function get_visual_selection()
local s_start = vim.fn.getpos("'<")
local s_end = vim.fn.getpos("'>")
local n_lines = math.abs(s_end[2] - s_start[2]) + 1
local lines = vim.api.nvim_buf_get_lines(0, s_start[2] - 1, s_end[2], false)
lines[1] = string.sub(lines[1], s_start[3], -1)
if n_lines == 1 then
lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3] - s_start[3] + 1)
else
lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3])
end
return table.concat(lines, '\n')
end
vim.keymap.set('n', '<leader>ps', function() -- works fine
builtin.grep_string({ search = vim.fn.input("Grep > ") });
end)
vim.keymap.set('v', '<leader>ps', function() -- gets the same first value for sel everytime!
local sel = get_visual_selection();
builtin.grep_string({ search = vim.fn.input("Grep > " .. sel) });
end)
This code works on the first time; but then the first text retrieved from selection never changes! I get the first text I selected over and over. I don't understand what is causing that. It seems the function gets hit by some sort of caching mechanism from lua...?
I finally used a different and simpler way of doing this exact thing using a remap, yank and paste; I still don't know what's wrong in my question's lua script though.
Here is the working lua map:
vim.keymap.set('v', '<leader>ps', 'y<leader>ps<C-r>*', { remap = true });

Remove older $_GET from url

I've got some problems with my url on my website. I'm trying to get a link with the given GET parameters, but i'm getting my previous parameter aswell.
My url looks like this:
www.cdwinkel.dev/search-results?genre=Pop&medium=DVD&medium=Single .
It should be:
www.cdwinkel.dev/search-results?genre=Pop&medium=Single .
I'm running the following code:
$data['url'] = createurl();
function createurl(){
$i = 1;
$string = "?";
$keys = array_keys($_GET);
foreach($_GET as $get){
if($get != ""){
$string .= $keys[$i] . "=" . $get ."&";
$i++;
}
}
$string = rtrim($string, "&");
return $string;
}
$i = 1, because my first value in my array is empty.
And my button looks like this:
<a href='".$data['url'].'&medium='.$names[$i]."'>
I guess I should'nt set &medium=.$names[$i] in the href tag,
but I wont get the new $names[$i] in my function, so I won't get a new url if i wont add it in.
I'm looking forward to your responce.
Sincerely,
Kars Takens
At this point i've created an array with the right arraykeys and values.
$url = array_slice($_GET, 1);
This returns the following array:
array (size=2)
'genre' => string 'Pop' (length=3)
'medium' => string 'DVD' (length=3)
After this I decoded this into a new string:
genre=Pop&medium=DVD
I got 6 button which I created in a foreach loop, but i'm getting &medium='VALUE' Twice. This only happens after the first time. So the first time my button works well.
<?php
$names = array_keys($data['tellen']);
$i = 0;
foreach($data['tellen'] as $m){
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
$i++;
}
Hopefully you can help me further with this information.
I solved my problem by adding this in my forloop:
foreach($data['tellen'] as $m){
if(isset($_GET['medium'])){
unset($_GET['medium']);
$url = array_slice($_GET, 1);
$data['url'] = urldecode(http_build_query($url));
}
echo "<li><a href='search-results?".$data['url'].'&medium='.$names[$i]."'>". $names[$i] ." <span class='product-amount'>(". $m[0]->count. ")</span></a></li>";
$i++;
}

vlc lua: how do I get the full path of the current playing item?

I'm not a programmer so this is difficult for me. I want to make an extension to send the full path to the clipboard in the full format. Example:
D:\MyFolder\music\audio.mp3
I recently found and butchered this extension which sends the running time to the clipboard. Is it possible to modify it so it gets the full path instead of the running time?
I'm using VLC media player 2.0.5 Twoflower 32 bits.
Windows 7 professional 32bits SP1
Here's the content of the .lua file I'm using and want to modify:
-- Time2Clip.lua -- VLC extension --
--[[
INSTALLATION:
Put the file in the VLC subdir /lua/extensions, by default:
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
Restart the VLC.
Then you simply use the extension by going to the "View" menu and selecting it.
--]]
function descriptor()
return {
title = "Time2Clip";
version = "1.0";
author = "valuex";
url = 'https://forum.videolan.org/viewtopic.php?f=29&t=101114';
shortdesc = "Time2Clip";
description = "<div style=\"background-color:lightgreen;\"><b>just a simple VLC extension </b></div>";
capabilities = {"input-listener"}
}
end
function activate()
create_dialog()
end
function close()
vlc.deactivate()
end
function create_dialog()
w = vlc.dialog("Time2Clip")
--w2 = w:add_button("Save_to_Clip", click_SAVE,2,1,1,1)
click_SAVE()
end
function click_SAVE()
local input = vlc.object.input()
if input then
local curtime=vlc.path()
-- local curtime=vlc.var.get(input, "time")
-- w2:set_text( curtime )
save_to_clipboard(curtime)
end
end
function save_to_clipboard(var)
strCmd = 'echo '..var..' |clip'
os.execute(strCmd)
vlc.deactivate()
end
I read LUA's README.TXT file and found this but I don't know how to use it. Please help me. Thanks in advance.
input.item(): Get the current input item. Input item methods are:
:uri(): Get item's URI.
:name(): Get item's name.
How about:
function descriptor()
return {
title = "URI2Clip";
version = "1.0";
author = "";
url = '';
shortdesc = "URI2Clip";
description = "<div><b>Copy the media URI to the Windows clipboard</b></div>";
}
end
function activate()
local item = vlc.input.item()
local uri = item:uri()
uri = string.gsub(uri, '^file:///', '')
uri = string.gsub(uri, '/', '\\')
strCmd = 'echo '..uri..' |clip'
os.execute(strCmd)
end
URI returns something like file:///c:/users/username/Documents/song.mp3 so I convert that to c:\users\username... format instead. NB. This is only going to work for saved files, it will mangle web addresses.

How to detect if the path of a url starts with a certain word

I'm trying to figure out how to get the page name from a facebook page and although I've figured out how to get it from the http://www.facebook.com/pagename version, I can't figure out how to test and get it from the longer version that looks like this http://www.facebook.com/pages/pagename/433425324544. How can I test to see if the path starts with /pages and how can I extract the pagename from it all using one function. Here's what I use to get the page name in the standard form.
function get_facebook_username( $facebook_url ) {
$url = $facebook_url;
$result = preg_match("/(https|http)?:\/\/(www\.)?facebook\.com\/?([^\/]*)/", $url, $matches);
$fb_username = 'default value';
if($result == 1){
$fb_username = $matches[3];
} else {
return;
}
echo "$fb_username";
}
I found a better way to do this I think except it cuts the last letter off the username if the url is in it's shortened version. For example.
//---short
$url = http://www.facebook.com/pagename
//---long
$url = http://www.facebook.com/pages/pagename/7532527927346
<?php
$url = $facebook_username;
$path = parse_url($url, PHP_URL_PATH);
$pathTrimmed = trim($path, 'pages/../0123456789');
echo $pathTrimmed;
?>
//---short
pagenam
//---long
pagename
Why is the short version missing the last letter?
The way I would go about this is to use explode to place the url into an array using the below code:
$url = "http://www.facebook.com/pages/pagename/7532527927346";
$result = explode("/", $url);
You get the following array:
array(6) {
[0]=>
string(5) "http:"
[1]=>
string(0) ""
[2]=>
string(16) "www.facebook.com"
[3]=>
string(5) "pages"
[4]=>
string(8) "pagename"
[5]=>
string(13) "7532527927346"
}
Then a simple if $result[3] == "pages" will allow you to check if the url contains pages.
Ninja Edit
In place of explode, you can also use the preg_split which will return no empty array elements:
$result = preg_split("~/~", $url, -1, PREG_SPLIT_NO_EMPTY);

ob_get_clean and ob_get_contents return content to screen instead of putting it into variable when used with var_dump

I use this function for debugging:
function d($v,$tofile=null) {
static $wasused;
ob_start();
var_dump($v);
$dump = ob_get_clean();
if (is_array($v)) $dump = preg_replace("#=>\n#",'=>',$dump);
if (strlen($dump)>1000 or $tofile) {
fileput('debug.txt',$dump,$wasused);
echo n.n."strlen=".strlen($dump)." >> debug.txt".n.n;
}
elseif (strlen($dump)<80) echo $dump;
else echo n.n.$dump.n.n;
$wasused=true;
}
the problem is it sometimes return content to console, particularly when this content is var_dump result on a big array,
anyone of you have seen this problem before ?
If this is in your php.ini:
implicit_flush = On
change it to this:
implicit_flush = Off
Before assuming that there is a problem with var_dump itself, one would need to verify that fileput() does exactly what the question implies.

Resources