1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created for IG Monitoring. |
4
|
|
|
* User: jakim <[email protected]> |
5
|
|
|
* Date: 18.06.2018 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace app\components\services; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use app\components\http\Client; |
12
|
|
|
use app\components\http\ProxyManager; |
13
|
|
|
use app\components\instagram\AccountScraper; |
14
|
|
|
use app\components\instagram\models\Account; |
15
|
|
|
use app\components\MediaManager; |
16
|
|
|
use app\components\services\contracts\ServiceInterface; |
17
|
|
|
use app\components\updaters\AccountUpdater; |
18
|
|
|
use app\dictionaries\AccountInvalidationType; |
19
|
|
|
use GuzzleHttp\Exception\ClientException; |
20
|
|
|
use GuzzleHttp\Exception\RequestException; |
21
|
|
|
use Jakim\Exception\RestrictedProfileException; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\base\BaseObject; |
24
|
|
|
use yii\web\NotFoundHttpException; |
25
|
|
|
|
26
|
|
|
class AccountFullUpdate extends BaseObject implements ServiceInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \app\models\Account |
30
|
|
|
*/ |
31
|
|
|
public $account; |
32
|
|
|
|
33
|
|
|
public function run() |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var ProxyManager $proxyManager |
37
|
|
|
*/ |
38
|
|
|
$proxyManager = Yii::createObject(ProxyManager::class); |
39
|
|
|
/** |
40
|
|
|
* @var AccountUpdater $accountUpdater |
41
|
|
|
*/ |
42
|
|
|
$accountUpdater = Yii::createObject([ |
43
|
|
|
'class' => AccountUpdater::class, |
44
|
|
|
'account' => $this->account, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$proxy = $proxyManager->reserve($this->account); |
48
|
|
|
try { |
49
|
|
|
$httpClient = Client::factory($proxy, [], 3600); |
50
|
|
|
|
51
|
|
|
$scraper = Yii::createObject(AccountScraper::class, [ |
52
|
|
|
$httpClient, |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$accountData = $this->fetchAccountData($scraper); |
56
|
|
|
$posts = $scraper->fetchLastPosts($accountData->username); |
57
|
|
|
|
58
|
|
|
$proxyManager->release($proxy); |
59
|
|
|
unset($proxy); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
if ($accountData->isPrivate) { |
63
|
|
|
$accountUpdater |
64
|
|
|
->setIsInValid(AccountInvalidationType::IS_PRIVATE) |
65
|
|
|
->setNextStatsUpdate(true) |
66
|
|
|
->save(); |
67
|
|
|
} else { |
68
|
|
|
$accountUpdater |
69
|
|
|
->setDetails($accountData) |
70
|
|
|
->setIdents($accountData) |
71
|
|
|
->setIsValid() |
72
|
|
|
->setStats($accountData, $posts) |
73
|
|
|
->setNextStatsUpdate() |
74
|
|
|
->save(); |
75
|
|
|
|
76
|
|
|
$mediaManager = Yii::createObject(MediaManager::class); |
77
|
|
|
$mediaManager->addToAccount($this->account, $posts); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} catch (NotFoundHttpException $exception) { |
82
|
|
|
$accountUpdater |
83
|
|
|
->setIsInValid(AccountInvalidationType::NOT_FOUND) |
84
|
|
|
->setNextStatsUpdate(true) |
85
|
|
|
->save(); |
86
|
|
|
} catch (RestrictedProfileException $exception) { |
87
|
|
|
$accountUpdater |
88
|
|
|
->setIsInValid(AccountInvalidationType::RESTRICTED_PROFILE) |
89
|
|
|
->setNextStatsUpdate(true) |
90
|
|
|
->save(); |
91
|
|
|
} catch (RequestException $exception) { |
92
|
|
|
if (isset($proxy) && (strpos($exception->getMessage(), "Failed to connect to " . $proxy->ip) !== false || strpos($exception->getMessage(), "malformed") !== false)) { |
93
|
|
|
$type = strpos($exception->getMessage(), "malformed") !== false ? AccountInvalidationType::PROXY_ERROR : AccountInvalidationType::PROXY_TIMEOUT; |
94
|
|
|
$proxyManager->invalidate($proxy); |
95
|
|
|
$accountUpdater |
96
|
|
|
->setIsInvalid($type) |
97
|
|
|
->setNextStatsUpdate(true) |
98
|
|
|
->save(); |
99
|
|
|
} else { |
100
|
|
|
$accountUpdater |
101
|
|
|
->setIsInvalidUnknown($exception->getMessage()) |
102
|
|
|
->setNextStatsUpdate(true) |
103
|
|
|
->save(); |
104
|
|
|
} |
105
|
|
|
} finally { |
106
|
|
|
if (isset($proxy)) { |
107
|
|
|
$proxyManager->release($proxy); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function fetchAccountData(AccountScraper $scraper): Account |
114
|
|
|
{ |
115
|
|
|
$idents = array_filter([ |
116
|
|
|
$this->account->username, |
117
|
|
|
$this->account->instagram_id, |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
foreach ($idents as $ident) { |
121
|
|
|
try { |
122
|
|
|
$accountData = $scraper->fetchOne($ident); |
123
|
|
|
if ($this->account->instagram_id && $accountData->id != $this->account->instagram_id) { |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
} catch (ClientException $exception) { |
127
|
|
|
Yii::error($exception->getMessage(), __METHOD__); |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (empty($accountData)) { |
134
|
|
|
throw new NotFoundHttpException(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $accountData; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |