|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Monitoring. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 11.01.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace app\components\instagram; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use app\components\instagram\base\Scraper; |
|
12
|
|
|
use app\components\instagram\contracts\AccountScraperInterface; |
|
13
|
|
|
use app\components\instagram\models\Account as IgAccount; |
|
14
|
|
|
use Jakim\Query\AccountQuery; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
|
|
17
|
|
|
class AccountScraper extends Scraper implements AccountScraperInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $ident username or id |
|
21
|
|
|
* @return \app\components\instagram\models\Account|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public function fetchOne(string $ident): ?IgAccount |
|
24
|
|
|
{ |
|
25
|
|
|
$query = new AccountQuery($this->httpClient); |
|
26
|
|
|
$account = $query->findOne($ident); |
|
27
|
|
|
$profilePic = $this->fetchProfilePicIfNeeded($account->username, $account->profilePicUrl); |
|
28
|
|
|
|
|
29
|
|
|
$model = new IgAccount(); |
|
30
|
|
|
$model->id = $account->id; |
|
31
|
|
|
$model->username = $account->username; |
|
32
|
|
|
$model->profilePicUrl = $profilePic; |
|
33
|
|
|
$model->fullName = $account->fullName; |
|
34
|
|
|
$model->biography = $account->biography; |
|
35
|
|
|
$model->externalUrl = $account->externalUrl; |
|
36
|
|
|
$model->followedBy = $account->followedBy; |
|
37
|
|
|
$model->follows = $account->follows; |
|
38
|
|
|
$model->media = $account->media; |
|
39
|
|
|
$model->isPrivate = $account->isPrivate; |
|
40
|
|
|
$model->isVerified = $account->isVerified; |
|
41
|
|
|
$model->isBusiness = $account->isBusiness; |
|
42
|
|
|
$model->businessCategory = $account->businessCategory; |
|
43
|
|
|
|
|
44
|
|
|
return $model; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $username |
|
49
|
|
|
* @return \app\components\instagram\models\Post[] |
|
50
|
|
|
* @throws \Jakim\Exception\EmptyContentException |
|
51
|
|
|
* @throws \Jakim\Exception\RestrictedProfileException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function fetchLastPosts(string $username): array |
|
54
|
|
|
{ |
|
55
|
|
|
$query = new AccountQuery($this->httpClient); |
|
56
|
|
|
$posts = $query->findLastPosts($username, 10); |
|
57
|
|
|
$posts = $this->preparePosts($posts); |
|
58
|
|
|
|
|
59
|
|
|
return $posts; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function fetchProfilePicIfNeeded($username, $profilePicUrl) |
|
63
|
|
|
{ |
|
64
|
|
|
$username = strtolower($username); |
|
65
|
|
|
$filename = sprintf('%s_%s', $username, basename($profilePicUrl)); |
|
66
|
|
|
$filename = explode('?', $filename)['0']; |
|
67
|
|
|
$path = sprintf('/uploads/%s', substr($username, 0, 2)); |
|
68
|
|
|
|
|
69
|
|
|
$fullPath = Yii::getAlias("@app/web/{$path}"); |
|
70
|
|
|
@mkdir($fullPath); |
|
|
|
|
|
|
71
|
|
|
@chmod($fullPath, 0777); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$file = "{$fullPath}/{$filename}"; |
|
74
|
|
|
if (!file_exists($file)) { |
|
75
|
|
|
$content = $this->httpClient->get($profilePicUrl)->getBody()->getContents(); |
|
76
|
|
|
file_put_contents($file, $content); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return "{$path}/{$filename}"; |
|
80
|
|
|
} |
|
81
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: