diff --git a/src/Sseffa/VideoApi/Services/Vimeo.php b/src/Sseffa/VideoApi/Services/Vimeo.php index 1db0978..0e2cdf5 100644 --- a/src/Sseffa/VideoApi/Services/Vimeo.php +++ b/src/Sseffa/VideoApi/Services/Vimeo.php @@ -65,6 +65,28 @@ public function getVideoDetail($id) ); } + /** + * Get Video Raw Data + * + * @param string $id + * @return array|mixed + * @throws \Exception + */ + public function getVideoRawData($id) + { + $this->setId($id); + + $data = $this->getData($this->baseVideoUrl); + + if(!$data) { + throw new \Exception("Video not found"); + } + + $data = $data[0]; + + return $data; + } + /** * Get Video Channel By Id (username) * @@ -100,4 +122,26 @@ public function getVideoList($id) } return $list; } + + /** + * Parse a vimeo URL to get the vimeo video id. + * Support vimeo.com url + * + * @param string $vimeo_url + * @throws \Exception + * @return string Video Id + */ + public function parseVIdFromURL($vimeo_url) + { + if (strpos($vimeo_url, 'vimeo.com')) { + $tokens = explode("/", $vimeo_url); + if(is_numeric($tokens[3])) + return $tokens[3]; + else + throw new \Exception('The supplied URL does not contain a valid Vimeo video id'); + } else { + throw new \Exception('The supplied URL does not look like a Vimeo URL'); + } + } } + diff --git a/src/Sseffa/VideoApi/Services/Youtube.php b/src/Sseffa/VideoApi/Services/Youtube.php index b6a9796..0b14adb 100644 --- a/src/Sseffa/VideoApi/Services/Youtube.php +++ b/src/Sseffa/VideoApi/Services/Youtube.php @@ -64,10 +64,30 @@ public function getVideoDetail($id) 'like_count' => isset($data->items[0]->statistics->likeCount) ? $data->items[0]->statistics->likeCount : 0, 'view_count' => isset($data->items[0]->statistics->viewCount) ? $data->items[0]->statistics->viewCount : 0, 'comment_count' => isset($data->items[0]->statistics->commentCount) ? $data->items[0]->statistics->commentCount : 0, - 'uploader' => null + 'uploader' => $data->items[0]->snippet->channelTitle ); } + /** + * Get Video Raw Data + * @param string $id + * @return array + * @throws \Exception + */ + public function getVideoRawData($id) + { + $this->setId($id); + + $data = $this->getData(str_replace('{key}', $this->key, $this->baseVideoUrl)); + + if(isset($data->error)) + { + throw new \Exception("Video not found"); + } + + return $data; + } + /** * Get Video List * @param string $id @@ -93,4 +113,71 @@ public function getVideoList($id) return $list; } + + /** + * Parse a youtube URL to get the youtube Vid. + * Support both full URL (www.youtube.com) and short URL (youtu.be) + * Source: https://github.com/alaouy/Youtube + * + * @param string $youtube_url + * @throws \Exception + * @return string Video Id + */ + public function parseVIdFromURL($youtube_url) + { + if (strpos($youtube_url, 'youtube.com')) { + if (strpos($youtube_url, 'embed')) { + $path = static::_parse_url_path($youtube_url); + $vid = substr($path, 7); + return $vid; + } else { + $params = static::_parse_url_query($youtube_url); + return $params['v']; + } + } else if (strpos($youtube_url, 'youtu.be')) { + $path = static::_parse_url_path($youtube_url); + $vid = substr($path, 1); + return $vid; + } else { + throw new \Exception('The supplied URL does not look like a Youtube URL'); + } + } + + /** + * Parse the input url string and return just the path part + * Source: https://github.com/alaouy/Youtube + * + * @param string $url the URL + * @return string the path string + */ + public static function _parse_url_path($url) + { + $array = parse_url($url); + + return $array['path']; + } + + /** + * Parse the input url string and return an array of query params + * Source: https://github.com/alaouy/Youtube + * + * @param string $url the URL + * @return array array of query params + */ + public static function _parse_url_query($url) + { + $array = parse_url($url); + $query = $array['query']; + + $queryParts = explode('&', $query); + + $params = array(); + foreach ($queryParts as $param) { + $item = explode('=', $param); + $params[$item[0]] = empty($item[1]) ? '' : $item[1]; + } + + return $params; + } } +