Completed
Push — master ( 1e5574...2c7872 )
by C
06:04
created

Uploadednet::fetchDownloadInfo()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 47
Code Lines 24

Duplication

Lines 19
Ratio 40.43 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 47
rs 8.5125
cc 6
eloc 24
nc 16
nop 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 Uploadednet extends Http
8
{
9
10
	public function fetchDownloadInfo (array $downloads)
11
	{
12
		foreach ($downloads as $download)
13
		{
14
			try
15
			{
16
				if (preg_match("/file\/(.*?)\//", $download->getLink(), $matches))
17
				{
18
					// Getting the link information
19
					$res = $this->getClient()->request('get',
20
							'https://uploaded.net/api/filemultiple?apikey=lhF2IeeprweDfu9ccWlxXVVypA5nA3EL&id_0=' . $matches[1]);
21
					$csv = explode(',', $res->getBody()->getContents(), 5);
22 View Code Duplication
					if (count($csv) >= 5)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
					{
24
						if ($csv[0] != 'online')
25
						{
26
							$download->setState(Download::STATE_DOWNLOADING_ERROR);
27
							$download->setMessage($csv[0]);
28
						}
29
						else
30
						{
31
							$download->setFileName($csv[4]);
32
							$download->setSize($csv[2]);
33
						}
34
					}
35
					else
36
					{
37
						parent::fetchDownloadInfo([
38
								$download
39
						]);
40
					}
41
				}
42
				else
43
				{
44
					parent::fetchDownloadInfo([
45
							$download
46
					]);
47
				}
48
			}
49
			catch (\Exception $e)
50
			{
51
				$this->log('Exception fetching head for connection test: ' . $e->getMessage());
52
				$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_URL');
53
				$download->setState(Download::STATE_DOWNLOADING_ERROR);
54
			}
55
		}
56
	}
57
58
	protected function getUrlToDownload (Download $download)
59
	{
60
		$res = $this->getClient()->request('get', $download->getLink());
61
		$html = $res->getBody()->getContents();
62
63
		if (! preg_match("#https?://[0-9a-z\-]*stor\d+.uploaded.net/dl/([0-9a-z\-]+)#mi", $html, $matches))
64
		{
65
			$download->setMessage('TARTANA_DOWNLOAD_MESSAGE_INVALID_MD5');
66
			return null;
67
		}
68
69
		$this->log('Uploaded net real url: ' . $matches[0]);
70
71
		return $matches[0];
72
	}
73
74
	protected function login ()
75
	{
76
		if ($this->hasCookie('login'))
77
		{
78
			return true;
79
		}
80
81
		$args = [
82
				'id' => trim($this->getConfiguration()->get('uploadednet.username')),
83
				'pw' => trim($this->getConfiguration()->get('uploadednet.password'))
84
		];
85
86
		if (! $args['id'])
87
		{
88
			return false;
89
		}
90
91
		$res = $this->getClient()->request('post', 'https://uploaded.net/io/login', [
92
				'form_params' => $args
93
		]);
94
		$html = $res->getBody()->getContents();
95
		return ! preg_match('#.' . preg_quote('{"err":') . '#si', $html);
96
	}
97
}
98