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):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
303132
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
33
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
34
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
353637
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
38
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
39
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
40
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
4142
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
43
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
44
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
45
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
4647
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
484950
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
51
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
52
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
535455
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
56
Covered by 1 test(s):
  • VimeoTest::testPrivateVideo
575859
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
60
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
61
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
6263
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
64
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
65
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
6667
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
68
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
69
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
70
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
71
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
72
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
73
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
74
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
7576
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
777879
Covered by 2 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testDownload
8081828384858687888990
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
9192
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
93
Covered by 5 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
94
Covered by 3 test(s):
  • VimeoTest::testConstructor
  • VimeoTest::testPrivateVideo
  • VimeoTest::testDownload
9596
Covered by 2 test(s):
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
97
Covered by 2 test(s):
  • VimeoTest::testBadURL
  • VimeoTest::testBadID
9899100101102103104
Covered by 1 test(s):
  • VimeoTest::testDownload
105
Covered by 1 test(s):
  • VimeoTest::testDownload
106
Covered by 1 test(s):
  • VimeoTest::testDownload
107108
Covered by 1 test(s):
  • VimeoTest::testDownload
109
Covered by 1 test(s):
  • VimeoTest::testDownload
110111112113114115116
Covered by 1 test(s):
  • VimeoTest::testDownload
117
Covered by 1 test(s):
  • VimeoTest::testDownload
118
Covered by 1 test(s):
  • VimeoTest::testDownload
119120
Covered by 1 test(s):
  • VimeoTest::testDownload
121
Covered by 1 test(s):
  • VimeoTest::testDownload
122123124125126127128129
Covered by 1 test(s):
  • VimeoTest::testDownload
130131
Covered by 1 test(s):
  • VimeoTest::testDownload
132
Covered by 1 test(s):
  • VimeoTest::testDownload
133134135136137
Covered by 1 test(s):
  • VimeoTest::testDownload
138139140
Covered by 1 test(s):
  • SupportedSitesTest::testVimeoSupportsURL
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;
	}

}