-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.php
82 lines (67 loc) · 1.93 KB
/
youtube.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Plugin youtube
*
* Get youtube metadata
*/
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
use Shaarli\Bookmark\LinkUtils;
use Shaarli\Render\TemplatePage;
function youtube_url($url)
{
$pattern = '/^https:\/\/(www|m)\.youtube\.com\/watch\?v=(?<video_id>[^&]+)/i';
if (preg_match($pattern, $url, $match)) {
return $match['video_id'];
}
$pattern = '/^https:\/\/youtu\.be\/(?<video_id>[^?]+)/i';
if (preg_match($pattern, $url, $match)) {
return $match['video_id'];
}
return '';
}
function hook_youtube_render_editlink($data, $conf)
{
if (!$data['link_is_new']) {
return $data;
}
$video_id = youtube_url($data['link']['url']);
if (!$video_id) {
return $data;
}
$api_key = $conf->get('plugins.YOUTUBE_API_KEY');
if (empty($api_key)) {
$url = 'https://www.youtube.com/oembed?url=' . $data['link']['url'] . '&format=json';
} else {
$url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&id=' . $video_id . '&key=' . $api_key;
}
list($headers, $content) = get_http_response($url);
$metadata = json_decode($content, true);
if ($metadata['items']) {
$metadata = $metadata['items'][0]['snippet'];
}
$data['link']['title'] = $metadata['title'];
$data['link']['description'] = $metadata['description'];
// Make sure there is at least one tag (youtube)
$metadata['tags'][] = 'youtube';
$data['link']['tags'] = tags_array2str(array_map(function ($tag) {
return str_replace(' ', '-', $tag);
}, $metadata['tags']), ' ');
return $data;
}
function hook_youtube_render_linklist($data)
{
return $data;
}
function hook_youtube_render_includes($data)
{
return $data;
}
/**
* This function is never called, but contains translation calls for GNU gettext extraction.
*/
function youtube_dummy_translation()
{
// meta
t('Adds youtube metadata content.');
}