|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace X3dgoo\InstagramScraper\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use InstagramScraper\Instagram; |
|
6
|
|
|
use InstagramScraper\Model\Media; |
|
7
|
|
|
use Phpfastcache\Helper\Psr16Adapter; |
|
8
|
|
|
use SilverStripe\Core\Environment; |
|
9
|
|
|
use SilverStripe\Dev\BuildTask; |
|
10
|
|
|
use SilverStripe\ORM\DB; |
|
11
|
|
|
use X3dgoo\InstagramScraper\Model\InstagramPost; |
|
12
|
|
|
|
|
13
|
|
|
class ImportInstagramPostsTask extends BuildTask |
|
14
|
|
|
{ |
|
15
|
|
|
private static $segment = 'import-instagram-posts'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
protected $title = 'Import Instagram feed'; |
|
18
|
|
|
|
|
19
|
|
|
protected $description = ''; |
|
20
|
|
|
|
|
21
|
|
|
public function run($request) |
|
22
|
|
|
{ |
|
23
|
|
|
$handle = $request->getVar('handle'); |
|
24
|
|
|
$loginUsername = Environment::getEnv('INSTAGRAM_USERNAME'); |
|
|
|
|
|
|
25
|
|
|
$loginPassword = Environment::getEnv('INSTAGRAM_PASSWORD'); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
if (!$handle) { |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$instagram = new Instagram(); |
|
32
|
|
|
|
|
33
|
|
|
if ($loginUsername && $loginPassword) { |
|
34
|
|
|
$instagram = Instagram::withCredentials($loginUsername, $loginPassword, new Psr16Adapter('Files')); |
|
35
|
|
|
$instagram->login(); |
|
36
|
|
|
$instagram->saveSession(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$instagramMedias = $instagram->getMedias($handle); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($instagramMedias as $instagramMedia) { |
|
42
|
|
|
$this->importInstagramPost($instagramMedia); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Media $instagramMedia |
|
48
|
|
|
*/ |
|
49
|
|
|
public function importInstagramPost($instagramMedia) |
|
50
|
|
|
{ |
|
51
|
|
|
$shortCode = $instagramMedia->getShortCode(); |
|
52
|
|
|
|
|
53
|
|
|
$instagramPost = InstagramPost::get()->filter(['ShortCode' => $shortCode])->first(); |
|
54
|
|
|
|
|
55
|
|
|
if (!$instagramPost || !$instagramPost->exists()) { |
|
56
|
|
|
$instagramPost = InstagramPost::create(); |
|
57
|
|
|
$instagramPost->ShortCode = $shortCode; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$instagramPost->InstagramID = $instagramMedia->getId(); |
|
61
|
|
|
$instagramPost->Caption = $instagramMedia->getCaption(); |
|
62
|
|
|
$instagramPost->Handle = $instagramMedia->getOwner()->getUsername(); |
|
63
|
|
|
$instagramPost->Link = $instagramMedia->getLink(); |
|
64
|
|
|
$instagramPost->Type = $instagramMedia->getType(); |
|
65
|
|
|
$instagramPost->ImageLowResolutionUrl = $instagramMedia->getImageLowResolutionUrl(); |
|
66
|
|
|
$instagramPost->ImageThumbnailURL = $instagramMedia->getImageThumbnailUrl(); |
|
67
|
|
|
$instagramPost->ImageStandardResolutionUrl = $instagramMedia->getImageStandardResolutionUrl(); |
|
68
|
|
|
$instagramPost->ImageHighResolutionUrl = $instagramMedia->getImageHighResolutionUrl(); |
|
69
|
|
|
$instagramPost->Posted = $instagramMedia->getCreatedTime(); |
|
70
|
|
|
$instagramPost->LikesCount = $instagramMedia->getLikesCount(); |
|
71
|
|
|
$instagramPost->CommentsCount = $instagramMedia->getCommentsCount(); |
|
72
|
|
|
$instagramPost->write(); |
|
73
|
|
|
|
|
74
|
|
|
DB::alteration_message('Imported instagram post ' . $shortCode); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|