Completed
Push — master ( 610c9c...e93a81 )
by C
04:40
created

Youtubecom::makeSafe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
namespace Tartana\Host;
3
use GuzzleHttp\Psr7\Request;
4
use Tartana\Entity\Download;
5
use Tartana\Host\Common\Http;
6
7
class Youtubecom extends Http
8
{
9
10 4
	public function fetchDownloadInfo (array $downloads)
11
	{
12 4
		foreach ($downloads as $download)
13
		{
14
			// Connection check
15
			try
16
			{
17 4
				$data = $this->getStreamData($download);
18 3
				if (! empty($data['title']) && empty($download->getFileName()))
19
				{
20 1
					$download->setFileName($this->makeSafe($data['title'] . '.mp4'));
21
				}
22
23 3
				if (key_exists('errorcode', $data) && $data['errorcode'] > 0&&key_exists('reason', $data))
24
				{
25 1
					$download->setMessage($data['reason']);
26 3
					$download->setState(Download::STATE_DOWNLOADING_ERROR);
27
				}
28
			}
29 1
			catch (\Exception $e)
30
			{
31 1
				$this->log('Exception fetching head for connection test: ' . $e->getMessage());
32 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
33 4
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
34
			}
35
		}
36 4
	}
37
38 3
	protected function getUrlToDownload (Download $download)
39
	{
40 3
		$data = $this->getStreamData($download);
41
42 3
		if (! key_exists('url_encoded_fmt_stream_map', $data))
43
		{
44 1
			return null;
45
		}
46
47 2
		foreach (explode(',', $data['url_encoded_fmt_stream_map']) as $streamData)
48
		{
49 2
			parse_str($streamData, $streamData);
50
51 2
			if (key_exists('url', $streamData))
52
			{
53 1
				$url = urldecode($streamData['url']);
54 1
				$this->log('Youtube real url: ' . $url);
55 2
				return $url;
56
			}
57
		}
58 1
		return null;
59
	}
60
61 7
	private function getStreamData (Download $download)
62
	{
63 7
		if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $download->getLink(),
64
				$match))
65
		{
66
			// Getting the link information
67 6
			$res = $this->getClient()->request('get', 'http://www.youtube.com/get_video_info?video_id=' . $match[1]);
68
69 5
			parse_str($res->getBody()->getContents(), $videoData);
70 5
			return $videoData;
71
		}
72
73
		return [
74 1
				'title' => '',
75
				'url_encoded_fmt_stream_map' => ''
76
		];
77
	}
78
79
	/**
80
	 *
81
	 * @param string $file
82
	 * @return string
83
	 * @see http://stackoverflow.com/questions/2668854/sanitizing-strings-to-make-them-url-and-filename-safe
84
	 * @see http://stackoverflow.com/a/24812812/356375
85
	 */
86 1
	private function makeSafe ($file)
87
	{
88
		// Remove any trailing dots, as those aren't ever valid file names.
89 1
		$file = rtrim($file, '.');
90
91
		$regex = array(
92 1
				'#(\.){2,}#',
93
				'#[^A-Za-z0-9\.\_\- ]#',
94
				'#^\.#'
95
		);
96
97 1
		return trim(preg_replace($regex, '', $file));
98
	}
99
}
100