|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Monitoring. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 28.04.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace app\components; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use app\components\instagram\AccountDetails; |
|
12
|
|
|
use app\components\instagram\AccountScraper; |
|
13
|
|
|
use app\components\instagram\AccountStats; |
|
14
|
|
|
use app\models\Account; |
|
15
|
|
|
use app\models\AccountTag; |
|
16
|
|
|
use app\models\Tag; |
|
17
|
|
|
use yii\base\Component; |
|
18
|
|
|
|
|
19
|
|
|
class AccountManager extends Component |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Fetch data from API, update details and stats. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \app\models\Account $account |
|
25
|
|
|
* @throws \yii\base\Exception |
|
26
|
|
|
* @throws \yii\base\InvalidConfigException |
|
27
|
|
|
* @throws \yii\db\Exception |
|
28
|
|
|
* @throws \yii\web\NotFoundHttpException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function update(Account $account) |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var AccountScraper $scraper */ |
|
33
|
|
|
$scraper = \Yii::createObject(AccountScraper::class); |
|
34
|
|
|
/** @var AccountDetails $details */ |
|
35
|
|
|
$details = \Yii::createObject(AccountDetails::class); |
|
36
|
|
|
/** @var AccountStats $stats */ |
|
37
|
|
|
$stats = \Yii::createObject(AccountStats::class); |
|
38
|
|
|
|
|
39
|
|
|
$data = $scraper->fetchDetails($account); |
|
40
|
|
|
// update account details |
|
41
|
|
|
$details->updateDetails($account, $data); |
|
42
|
|
|
|
|
43
|
|
|
// update profile pic |
|
44
|
|
|
if ($details->profilePicNeedUpdate($account, $data)) { |
|
45
|
|
|
$content = $scraper->fetchProfilePic($account, $data->profilePicUrl); |
|
46
|
|
|
$details->updateProfilePic($account, $data, $content); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// update account stats |
|
50
|
|
|
if ($stats->statsNeedUpdate($account, $data)) { |
|
51
|
|
|
$stats->updateStats($account, $data); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$posts = $scraper->fetchMedia($account); |
|
55
|
|
|
// update account media |
|
56
|
|
|
$details->updateMedia($account, $posts); |
|
57
|
|
|
// update account er |
|
58
|
|
|
$stats->updateEr($account); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function saveUsernames(array $usernames) |
|
62
|
|
|
{ |
|
63
|
|
|
$createdAt = (new \DateTime())->format('Y-m-d H:i:s'); |
|
64
|
|
|
$rows = array_map(function ($username) use ($createdAt) { |
|
65
|
|
|
return [ |
|
66
|
|
|
$username, |
|
67
|
|
|
$createdAt, |
|
68
|
|
|
$createdAt, |
|
69
|
|
|
]; |
|
70
|
|
|
}, $usernames); |
|
71
|
|
|
|
|
72
|
|
|
$sql = \Yii::$app->db->getQueryBuilder() |
|
73
|
|
|
->batchInsert(Account::tableName(), ['username', 'updated_at', 'created_at'], $rows); |
|
74
|
|
|
$sql = str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql); |
|
75
|
|
|
\Yii::$app->db->createCommand($sql) |
|
76
|
|
|
->execute(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function updateTags(Account $account, array $tags) |
|
80
|
|
|
{ |
|
81
|
|
|
// clearing |
|
82
|
|
|
AccountTag::deleteAll(['account_id' => $account->id]); |
|
83
|
|
|
|
|
84
|
|
|
// add |
|
85
|
|
|
foreach (array_filter($tags) as $tag) { |
|
86
|
|
|
$model = Tag::findOne(['name' => $tag]); |
|
87
|
|
|
if ($model === null && $tag) { |
|
88
|
|
|
$model = new Tag(['name' => $tag]); |
|
89
|
|
|
$model->insert(); |
|
90
|
|
|
} |
|
91
|
|
|
$account->link('tags', $model); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |