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\AccountUpdater; |
12
|
|
|
use app\components\http\Client; |
13
|
|
|
use app\components\http\ProxyManager; |
14
|
|
|
use app\components\instagram\AccountScraper; |
15
|
|
|
use app\components\instagram\models\Account; |
16
|
|
|
use app\components\MediaManager; |
17
|
|
|
use app\components\services\contracts\ServiceInterface; |
18
|
|
|
use GuzzleHttp\Exception\ClientException; |
19
|
|
|
use yii\base\BaseObject; |
20
|
|
|
use yii\web\NotFoundHttpException; |
21
|
|
|
|
22
|
|
|
class AccountFullUpdate extends BaseObject implements ServiceInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \app\models\Account |
26
|
|
|
*/ |
27
|
|
|
public $account; |
28
|
|
|
|
29
|
|
|
public function run() |
30
|
|
|
{ |
31
|
|
|
$proxyManager = \Yii::createObject(ProxyManager::class); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
$proxy = $proxyManager->reserve($this->account); |
35
|
|
|
$httpClient = Client::factory($proxy, [], 3600); |
36
|
|
|
|
37
|
|
|
$scraper = \Yii::createObject(AccountScraper::class, [ |
38
|
|
|
$httpClient, |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$accountData = $this->fetchAccountData($scraper); |
42
|
|
|
$posts = $scraper->fetchLastPosts($accountData->username); |
43
|
|
|
|
44
|
|
|
$proxyManager->release($proxy); |
45
|
|
|
unset($proxy); |
46
|
|
|
|
47
|
|
|
$this->updateAccount($accountData, $posts); |
48
|
|
|
|
49
|
|
|
} catch (NotFoundHttpException $exception) { |
50
|
|
|
$this->account->disabled = 1; |
|
|
|
|
51
|
|
|
$this->account->save(); |
52
|
|
|
} finally { |
53
|
|
|
if (isset($proxy)) { |
54
|
|
|
$proxyManager->release($proxy); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function fetchAccountData(AccountScraper $scraper): Account |
61
|
|
|
{ |
62
|
|
|
$idents = array_filter([ |
63
|
|
|
$this->account->username, |
64
|
|
|
$this->account->instagram_id, |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
foreach ($idents as $ident) { |
68
|
|
|
try { |
69
|
|
|
$accountData = $scraper->fetchOne($ident); |
70
|
|
|
} catch (ClientException $exception) { |
71
|
|
|
\Yii::error($exception->getMessage(), __METHOD__); |
72
|
|
|
continue; |
73
|
|
|
}; |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (empty($accountData)) { |
78
|
|
|
throw new NotFoundHttpException(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $accountData; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function updateAccount($accountData, $posts): void |
85
|
|
|
{ |
86
|
|
|
$updater = \Yii::createObject([ |
87
|
|
|
'class' => AccountUpdater::class, |
88
|
|
|
'account' => $this->account, |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$updater->details($accountData); |
92
|
|
|
|
93
|
|
|
$stats = $updater->stats($accountData); |
94
|
|
|
$updater->er($stats, $posts); |
95
|
|
|
|
96
|
|
|
$mediaManager = \Yii::createObject(MediaManager::class); |
97
|
|
|
$mediaManager->saveForAccount($this->account, $posts); |
98
|
|
|
} |
99
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.