-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.php
70 lines (61 loc) · 1.53 KB
/
scraper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
require __DIR__.'/vendor/autoload.php';
use \InstagramAPI\Instagram as Instagram;
$ig = new Instagram();
echo "Instagram Comments Scraper by @flippofinke".PHP_EOL;
$username = readline("Username without @: ");
$password = readline("Password: ");
echo "I'm trying to access to your account.".PHP_EOL;
define("MAX_PAGES", 10);
try
{
$ig->login($username, $password);
}
catch(Exception $ex)
{
echo "Error ".$ex->getMessage().PHP_EOL;
exit;
}
$tags = [];
echo "All right, access granted. Write the tags from which get the comments (without #), once finished send an empty line".PHP_EOL;
while(($read = readline("Tag: ")) != "")
{
$tags[] = $read;
}
foreach($tags as $tag)
{
$rankToken = \InstagramAPI\Signatures::generateUUID();
$maxId = null;
$page = 0;
do {
echo "Searching for #$tag".PHP_EOL;
$posts = $ig->hashtag->getFeed($tag, $rankToken, $maxId);
foreach ($posts->getItems() as $item) {
try
{
$mediaId = $item->getId();
echo "#tag ".$mediaId." ".$item->getCode();
$data = $ig->media->getComments($mediaId);
$comments = $data->getComments();
$count = $data->getCommentCount();
echo " comments: $count".PHP_EOL;
if($count != 0)
{
foreach($comments as $comment)
{
$text = $comment->getText();
echo $text.PHP_EOL;
file_put_contents($tag.".txt", $text."\n", FILE_APPEND);
}
}
}
catch(Exception $ex)
{
echo "Error ".$ex->getMessage().PHP_EOL;
}
}
$maxId = $posts->getNextMaxId();
$page++;
} while ($maxId !== null && $page < MAX_PAGES);
}
?>