-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vid.php
145 lines (119 loc) · 4.11 KB
/
Vid.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
class Vid extends DataObject implements PermissionProvider {
private static $db = array(
'Title' => 'Text',
'TrackName' => 'Varchar',
'Artist' => 'Varchar',
'Album' => 'Varchar',
'VidID' => 'Varchar'
);
private static $summary_fields = array(
'Artist',
'TrackName',
'Title'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main',
new LiteralField(
'Link',
'<strong>Check link </strong><span style="display: inline-block;width: 126px;"></span>
<a href="https://www.youtube.com/watch?v=' . $this->VidID . '" target="_blank">' . $this->VidID . '</a><br><br>'));
$fields->addFieldToTab('Root.Main',
new LiteralField(
'Preview',
'<iframe width="420" height="315" src="https://www.youtube.com/embed/' . $this->VidID . '" frameborder="0" allowfullscreen></iframe>'));
return $fields;
}
public function getCMSValidator() {
return new RequiredFields(array('Url'));
}
//check if the vid is in the DB
public static function db_exists($id) {
return Vid::get()->filter('VidID', $id)->first();
}
//check if the vid is online and embeddable
public static function yt_exists($id) {
$theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$id&format=json";
$headers = get_headers($theURL);
if (substr($headers[0], 9, 3) == "200") {
return true;
} else {
return false;
}
}
//get best quality thumbnail from YouTube
public function bestRes($VidID) {
$theURL = "http://img.youtube.com/vi/" . $VidID . "/hqdefault.jpg";
$headers = get_headers($theURL);
if (substr($headers[0], 9, 3) == "200") {
return $theURL;
} else {
$theURL = "http://img.youtube.com/vi/" . $VidID . "/default.jpg";
$headers = get_headers($theURL);
if (substr($headers[0], 9, 3) == "200") {
return $theURL;
}
}
}
//import a vid from a YouTubeID
public static function importVidViaYouTubeId($id) {
$vid = new Vid();
$vid->VidID = $id;
$vid->Title = Vid::yt_getTitle($id);
$vid->Artist = Vid::vid_getTitleParts(Vid::yt_getTitle($id), 'Artist');
$vid->TrackName = Vid::vid_getTitleParts(Vid::yt_getTitle($id), 'TrackName');
return $vid->write();
}
//retrieve title from YouTube
public static function yt_getTitle($videoID) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/'.$videoID);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
$xml = new SimpleXMLElement($response);
return (string) $xml->title;
}
}
//split the title into parts ie. 'Artist', 'TrackName'
public static function vid_getTitleParts($title, $part) {
$titleComponents = explode("-", $title);
//If explode didn't split the title try a different splitter
if ($title == $titleComponents[0]) {
$titleComponents = explode("|", $title);
//If second splitter wasn't found, give up
if ($title == $titleComponents[0]) {
return '';
}
}
if ($part == 'Artist') {
return $titleComponents[0];
}
if ($part == 'TrackName') {
return $titleComponents[1];
}
}
//permissions
function providePermissions() {
return array(
'VID_EDIT' => 'Edit vids',
'VID_DELETE' => 'Delete vids',
'VID_CREATE' => 'Create vids',
);
}
function canView($member = null) {
return true;
}
function canCreate($member = null) {
return Permission::check('VID_CREATE');
}
function canEdit($member = null) {
return Permission::check('VID_EDIT');
}
function canDelete($member = null) {
return Permission::check('VID_DELETE');
}
}