Uploadednet::fetchDownloadInfo()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 23
cts 23
cp 1
rs 8.439
cc 6
eloc 26
nc 17
nop 1
crap 6
1
<?php
2
namespace Tartana\Host;
3
4
use GuzzleHttp\Psr7\Request;
5
use Tartana\Entity\Download;
6
use Tartana\Host\Common\Http;
7
8
class Uploadednet extends Http
9
{
10
11 5
	public function fetchDownloadInfo(array $downloads)
12
	{
13 5
		foreach ($downloads as $download) {
14
			try {
15 5
				if (preg_match("/file\/(.*?)\//", $download->getLink(), $matches)) {
16
					// Getting the link information
17 4
					$res = $this->getClient()->request(
18 4
						'get',
19 4
						'https://uploaded.net/api/filemultiple?apikey=lhF2IeeprweDfu9ccWlxXVVypA5nA3EL&id_0=' . $matches[1]
20
					);
21 3
					$csv = explode(',', $res->getBody()->getContents(), 5);
22 3
					if (count($csv) >= 5) {
23 2
						if ($csv[0] != 'online') {
24 1
							$download->setState(Download::STATE_DOWNLOADING_ERROR);
25 1
							$download->setMessage($csv[0]);
26
						} else {
27 1
							$download->setHash($csv[3]);
28 1
							$download->setFileName(trim($csv[4]));
29 2
							$download->setSize($csv[2]);
30
						}
31
					} else {
32 1
						parent::fetchDownloadInfo([
33 3
							$download
34
						]);
35
					}
36
				} else {
37 1
					parent::fetchDownloadInfo([
38 4
						$download
39
					]);
40
				}
41 1
			} catch (\Exception $e) {
42 1
				$this->log('Exception fetching file info for connection test: ' . $e->getMessage());
43 1
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
44 5
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
45
			}
46
		}
47 5
	}
48
49 6
	protected function getUrlToDownload(Download $download)
50
	{
51 6
		$res  = $this->getClient()->request('get', $download->getLink());
52 5
		$html = $res->getBody()->getContents();
53
54 5
		if (!preg_match("#https?://[0-9a-z\-]*stor\d+.uploaded.net/dl/([0-9a-z\-]+)#mi", $html, $matches)) {
55 1
			return null;
56
		}
57
58 4
		$this->log('Uploaded net real url: ' . $matches[0]);
59
60 4
		return $matches[0];
61
	}
62
63 7
	protected function login()
64
	{
65 7
		if ($this->getCookie('login')) {
66 1
			return true;
67
		}
68
69
		$args = [
70 6
			'id' => trim($this->getConfiguration()->get('uploadednet.username')),
71 6
			'pw' => trim($this->getConfiguration()->get('uploadednet.password'))
72
		];
73
74 6
		if (!$args['id']) {
75 1
			return false;
76
		}
77
78 5
		$res  = $this->getClient()->request('post', 'https://uploaded.net/io/login', [
79 5
			'form_params' => $args
80
		]);
81 5
		$html = $res->getBody()->getContents();
82 5
		return !preg_match('#.' . preg_quote('{"err":') . '#si', $html);
83
	}
84
}
85