Source of file Vimeo.php
Size: 4,229 Bytes - Last Modified: 2018-03-22T03:11:31+01:00
C:/xampp/htdocs/PodTube/src/SupportedSites/Vimeo.php
1234567891011121314151617181920212223242526272829
Covered by 5 test(s):
303132
Covered by 5 test(s):
33
Covered by 5 test(s):
34
Covered by 5 test(s):
353637
Covered by 5 test(s):
38
Covered by 3 test(s):
39
Covered by 3 test(s):
40
Covered by 3 test(s):
4142
Covered by 3 test(s):
43
Covered by 2 test(s):
44
Covered by 2 test(s):
45
Covered by 2 test(s):
4647
Covered by 2 test(s):
484950
Covered by 3 test(s):
51
Covered by 3 test(s):
52
Covered by 3 test(s):
535455
Covered by 3 test(s):
56
Covered by 1 test(s):
575859
Covered by 2 test(s):
60
Covered by 2 test(s):
61
Covered by 2 test(s):
6263
Covered by 2 test(s):
64
Covered by 2 test(s):
65
Covered by 2 test(s):
6667
Covered by 2 test(s):
68
Covered by 2 test(s):
69
Covered by 2 test(s):
70
Covered by 2 test(s):
71
Covered by 2 test(s):
72
Covered by 2 test(s):
73
Covered by 2 test(s):
74
Covered by 2 test(s):
7576
Covered by 2 test(s):
777879
Covered by 2 test(s):
8081828384858687888990
Covered by 5 test(s):
9192
Covered by 5 test(s):
93
Covered by 5 test(s):
94
Covered by 3 test(s):
9596
Covered by 2 test(s):
97
Covered by 2 test(s):
9899100101102103104
Covered by 1 test(s):
105
Covered by 1 test(s):
106
Covered by 1 test(s):
107108
Covered by 1 test(s):
109
Covered by 1 test(s):
110111112113114115116
Covered by 1 test(s):
117
Covered by 1 test(s):
118
Covered by 1 test(s):
119120
Covered by 1 test(s):
121
Covered by 1 test(s):
122123124125126127128129
Covered by 1 test(s):
130131
Covered by 1 test(s):
132
Covered by 1 test(s):
133134135136137
Covered by 1 test(s):
138139140
Covered by 1 test(s):
141142143144145146
| <?php /** * Created by PhpStorm. * User: Mike * Date: 7/23/2017 * Time: 11:39 AM */ namespace AudioDidact\SupportedSites; use AudioDidact\Video; class Vimeo extends SupportedSite { // Setup global variables /** @var string Vimeo URL */ private $vimeoConfigBaseURL = "https://player.vimeo.com/video/"; private $vimeoBaseURL = "https://vimeo.com/"; private $downloadURL = ""; private $thumbnailURL = ""; /** * YouTube constructor. Gets the video information, checks for it in the user's feed. * * @param string $str * @param boolean $isVideo * @throws \Exception */ public function __construct($str, $isVideo = false){ $this->video = new Video(); // If there is a URL/ID, continue if($str != null){ $this->video->setURL($str); $this->video->setIsVideo($isVideo); // Set video ID from setYoutubeID and time to current time $this->video->setId($this->setVimeoID($str)); $this->video->setFilename($this->video->getId()); $this->video->setThumbnailFilename($this->video->getFilename() . ".jpg"); $this->video->setTime(time()); $info = $this->getInfo(); $this->video->setTitle($info["title"]); $this->video->setAuthor($info["author"]); $this->video->setDesc($info["description"]); } } /** * @return array * @throws \Exception */ private function getInfo(){ $configJSON = file_get_contents($this->vimeoConfigBaseURL . $this->video->getId() . "/config"); $config = json_decode($configJSON, true); if($config == false){ throw new \Exception("Unable to parse vimeo JSON"); } if(!isset($config["request"]["files"])){ throw new \Exception("Private video or some other parse error"); } $downloadURLs = $config["request"]["files"]["progressive"]; $this->downloadURL = $downloadURLs[count($downloadURLs) - 1]["url"]; $this->thumbnailURL = $config["video"]["thumbs"]["640"]; $info = []; $info["author"] = $config["video"]["owner"]["name"]; $info["title"] = $config["video"]["title"]; $videoHTML = file_get_contents($this->vimeoBaseURL . $this->video->getId()); $doc = new \DOMDocument(); libxml_use_internal_errors(true); $doc->loadHTML($videoHTML); $finder = new \DomXPath($doc); $info["description"] = ""; $nodes = $finder->query("//div[@class='clip_details-description description-wrapper iris_desc']"); foreach($nodes as $i){ /** @var \DOMElement $i */ $info["description"] = trim($i->textContent); } return $info; } /** * Set YouTube ID from a given string using parseYoutubeURL * * @param string $str * @return bool * @throws \Exception */ private function setVimeoID($str){ $vidId = []; // Try and parse the string into a usable ID $tmpId = mb_ereg("vimeo\.com\/(\d{9})", $str, $vidId); if($tmpId > 0 && count($vidId) == 2){ return $vidId[1]; } error_log("unable to parse vimeo URL " . $str); throw new \Exception("Could not parse that vimeo URL"); } /** * Download thumbnail using videoID from YouTube's image server */ public function downloadThumbnail(){ $path = getcwd() . DIRECTORY_SEPARATOR . DOWNLOAD_PATH . DIRECTORY_SEPARATOR; $thumbnail = $path . $this->video->getThumbnailFilename(); file_put_contents($thumbnail, fopen($this->thumbnailURL, "r")); // Set the thumbnail file as publicly accessible @chmod($thumbnail, 0775); } /** * Download video using download URL from Python script and then call downloadWithPercentage to actually download * the video * * @throws \Exception */ public function downloadVideo(){ $path = getcwd() . DIRECTORY_SEPARATOR . DOWNLOAD_PATH . DIRECTORY_SEPARATOR; $videoFilename = $this->video->getFilename() . ".mp4"; $videoPath = $path . $videoFilename; $url = $this->downloadURL; if(mb_strpos($url, "Error:") > -1){ error_log("$url"); throw new \Exception($url); } /* Actually download the video from the url and print the * percentage to the screen with JSON */ $this->downloadWithPercentage($url, $videoPath); // Set the video file as publicly accessible @chmod($videoPath, 0775); $this->video->setDuration(SupportedSite::getDurationSeconds($videoPath)); } public static function supportsURL($url){ return mb_strpos($url, "vimeo.com") > -1; } } |