Skip to content

Commit 77a6849

Browse files
committed
Merge pull request #9 from jesusvazquez/master
Youtube uploader and two new functions
2 parents 339697f + 5c751e5 commit 77a6849

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

src/Sseffa/VideoApi/Services/Vimeo.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ public function getVideoDetail($id)
6565
);
6666
}
6767

68+
/**
69+
* Get Video Raw Data
70+
*
71+
* @param string $id
72+
* @return array|mixed
73+
* @throws \Exception
74+
*/
75+
public function getVideoRawData($id)
76+
{
77+
$this->setId($id);
78+
79+
$data = $this->getData($this->baseVideoUrl);
80+
81+
if(!$data) {
82+
throw new \Exception("Video not found");
83+
}
84+
85+
$data = $data[0];
86+
87+
return $data;
88+
}
89+
6890
/**
6991
* Get Video Channel By Id (username)
7092
*
@@ -100,4 +122,26 @@ public function getVideoList($id)
100122
}
101123
return $list;
102124
}
125+
126+
/**
127+
* Parse a vimeo URL to get the vimeo video id.
128+
* Support vimeo.com url
129+
*
130+
* @param string $vimeo_url
131+
* @throws \Exception
132+
* @return string Video Id
133+
*/
134+
public function parseVIdFromURL($vimeo_url)
135+
{
136+
if (strpos($vimeo_url, 'vimeo.com')) {
137+
$tokens = explode("/", $vimeo_url);
138+
if(is_numeric($tokens[3]))
139+
return $tokens[3];
140+
else
141+
throw new \Exception('The supplied URL does not contain a valid Vimeo video id');
142+
} else {
143+
throw new \Exception('The supplied URL does not look like a Vimeo URL');
144+
}
145+
}
103146
}
147+

src/Sseffa/VideoApi/Services/Youtube.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,30 @@ public function getVideoDetail($id)
6464
'like_count' => isset($data->items[0]->statistics->likeCount) ? $data->items[0]->statistics->likeCount : 0,
6565
'view_count' => isset($data->items[0]->statistics->viewCount) ? $data->items[0]->statistics->viewCount : 0,
6666
'comment_count' => isset($data->items[0]->statistics->commentCount) ? $data->items[0]->statistics->commentCount : 0,
67-
'uploader' => null
67+
'uploader' => $data->items[0]->snippet->channelTitle
6868
);
6969
}
7070

71+
/**
72+
* Get Video Raw Data
73+
* @param string $id
74+
* @return array
75+
* @throws \Exception
76+
*/
77+
public function getVideoRawData($id)
78+
{
79+
$this->setId($id);
80+
81+
$data = $this->getData(str_replace('{key}', $this->key, $this->baseVideoUrl));
82+
83+
if(isset($data->error))
84+
{
85+
throw new \Exception("Video not found");
86+
}
87+
88+
return $data;
89+
}
90+
7191
/**
7292
* Get Video List
7393
* @param string $id
@@ -93,4 +113,71 @@ public function getVideoList($id)
93113

94114
return $list;
95115
}
116+
117+
/**
118+
* Parse a youtube URL to get the youtube Vid.
119+
* Support both full URL (www.youtube.com) and short URL (youtu.be)
120+
* Source: https://github.com/alaouy/Youtube
121+
*
122+
* @param string $youtube_url
123+
* @throws \Exception
124+
* @return string Video Id
125+
*/
126+
public function parseVIdFromURL($youtube_url)
127+
{
128+
if (strpos($youtube_url, 'youtube.com')) {
129+
if (strpos($youtube_url, 'embed')) {
130+
$path = static::_parse_url_path($youtube_url);
131+
$vid = substr($path, 7);
132+
return $vid;
133+
} else {
134+
$params = static::_parse_url_query($youtube_url);
135+
return $params['v'];
136+
}
137+
} else if (strpos($youtube_url, 'youtu.be')) {
138+
$path = static::_parse_url_path($youtube_url);
139+
$vid = substr($path, 1);
140+
return $vid;
141+
} else {
142+
throw new \Exception('The supplied URL does not look like a Youtube URL');
143+
}
144+
}
145+
146+
/**
147+
* Parse the input url string and return just the path part
148+
* Source: https://github.com/alaouy/Youtube
149+
*
150+
* @param string $url the URL
151+
* @return string the path string
152+
*/
153+
public static function _parse_url_path($url)
154+
{
155+
$array = parse_url($url);
156+
157+
return $array['path'];
158+
}
159+
160+
/**
161+
* Parse the input url string and return an array of query params
162+
* Source: https://github.com/alaouy/Youtube
163+
*
164+
* @param string $url the URL
165+
* @return array array of query params
166+
*/
167+
public static function _parse_url_query($url)
168+
{
169+
$array = parse_url($url);
170+
$query = $array['query'];
171+
172+
$queryParts = explode('&', $query);
173+
174+
$params = array();
175+
foreach ($queryParts as $param) {
176+
$item = explode('=', $param);
177+
$params[$item[0]] = empty($item[1]) ? '' : $item[1];
178+
}
179+
180+
return $params;
181+
}
96182
}
183+

0 commit comments

Comments
 (0)