@ -24,6 +24,20 @@ class InstagramService
// private $password = 'Qwer2020';
// private $password = 'Qwer2020';
private $debug = false ;
private $debug = false ;
private $truncatedDebug = false ;
private $truncatedDebug = false ;
private $ig ;
public function __construct ()
{
$this -> ig = new Instagram ( $this -> debug , $this -> truncatedDebug );
try {
$this -> ig -> login ( $this -> username , $this -> password );
} catch
( \Exception $e ) {
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
exit ( 0 );
}
}
private $userList = [
private $userList = [
[ " bedich520 " => 17707667205 ],
[ " bedich520 " => 17707667205 ],
@ -139,6 +153,10 @@ class InstagramService
$myfile = file_put_contents ( $failLogFile , $filePrefix . " \t " . $fileUrl . PHP_EOL , FILE_APPEND | LOCK_EX );
$myfile = file_put_contents ( $failLogFile , $filePrefix . " \t " . $fileUrl . PHP_EOL , FILE_APPEND | LOCK_EX );
}
}
function oldFileDirList () {
return [ " /Users/shixuesen/OneDrive/Pictures/instagram/Likes_old/ " ];
}
function downloadFile ( $filenameUrl , $flag = 0 , $filePrefix = " " , $fileNamePrefix = " " )
function downloadFile ( $filenameUrl , $flag = 0 , $filePrefix = " " , $fileNamePrefix = " " )
{
{
//echo $filenameUrl;exit;
//echo $filenameUrl;exit;
@ -165,6 +183,14 @@ class InstagramService
echo " \n file exists " . $filePrefix . $filename ;
echo " \n file exists " . $filePrefix . $filename ;
return 0 ;
return 0 ;
}
}
// 去老的目录检查下是否已存在
$oldFileDirList = $this -> oldFileDirList ();
foreach ( $oldFileDirList as $oldFileDir ) {
if ( file_exists ( $oldFileDir . $filename )) {
echo " \n file exists " . $oldFileDir . $filename ;
return 0 ;
}
}
try {
try {
$cn_match = " https://scontent-lax3-1.cdninstagram.com " ;
$cn_match = " https://scontent-lax3-1.cdninstagram.com " ;
$options = array (
$options = array (
@ -227,21 +253,13 @@ class InstagramService
public function scrapeLikedUsers ()
public function scrapeLikedUsers ()
{
{
$ig = new Instagram ( $this -> debug , $this -> truncatedDebug );
try {
$ig -> login ( $this -> username , $this -> password );
} catch
( \Exception $e ) {
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
exit ( 0 );
}
$baseImageDir = " /Users/shixuesen/OneDrive/Pictures/instagram/Likes/ " ;
$baseImageDir = " /Users/shixuesen/OneDrive/Pictures/instagram/Likes_new/ " ;
try {
try {
$maxId = null ;
$maxId = null ;
do {
do {
$response = $ig -> media -> getLikedFeed ();
$response = $this -> ig -> media -> getLikedFeed ();
foreach ( $response -> getItems () as $item ) {
foreach ( $response -> getItems () as $item ) {
//echo json_encode($response->getItems());exit;
//echo json_encode($response->getItems());exit;
$userName = $item -> getUser () -> getUsername () . " _ " ;
$userName = $item -> getUser () -> getUsername () . " _ " ;
@ -295,22 +313,13 @@ class InstagramService
public function scrapeFeeds ()
public function scrapeFeeds ()
{
{
$ig = new Instagram ( $this -> debug , $this -> truncatedDebug );
try {
$ig -> login ( $this -> username , $this -> password );
} catch
( \Exception $e ) {
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
exit ( 0 );
}
$count = 0 ;
$count = 0 ;
$baseImageDir = " /Users/shixuesen/OneDrive/Pictures/instagram/Likes/ " ;
$baseImageDir = " /Users/shixuesen/OneDrive/Pictures/instagram/Likes_new/ " ;
try {
try {
$maxId = null ;
$maxId = null ;
do {
do {
$response = $ig -> timeline -> getTimelineFeed ( $maxId );
$response = $this -> ig -> timeline -> getTimelineFeed ( $maxId );
foreach ( $response -> getFeedItems () as $item ) {
foreach ( $response -> getFeedItems () as $item ) {
if ( $item -> getMediaOrAd () == null || $item -> getMediaOrAd () -> getProductType () == " ad " ) {
if ( $item -> getMediaOrAd () == null || $item -> getMediaOrAd () -> getProductType () == " ad " ) {
continue ;
continue ;
@ -355,9 +364,10 @@ class InstagramService
break ;
break ;
}
}
$count ++ ;
$count ++ ;
if ( $count > 20 0) {
if ( $count > 5 0) {
return ;
return ;
}
}
sleep ( 5 * random_int ( 1 , 10 ));
}
}
// Now we must update the maxId variable to the "next page".
// Now we must update the maxId variable to the "next page".
@ -371,7 +381,7 @@ class InstagramService
// always pause between requests that may run very rapidly, otherwise
// always pause between requests that may run very rapidly, otherwise
// Instagram will throttle you temporarily for abusing their API!
// Instagram will throttle you temporarily for abusing their API!
echo " \n Sleeping for 5s... \n " ;
echo " \n Sleeping for 5s... \n " ;
sleep ( 5 * random_int ( 1 , 10 ));
sleep ( 5 * random_int ( 1 , 100 ));
} while ( $maxId != null );
} while ( $maxId != null );
} catch ( \Exception $e ) {
} catch ( \Exception $e ) {
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
@ -380,17 +390,7 @@ class InstagramService
public function scrapeUsers ( $start = 0 )
public function scrapeUsers ( $start = 0 )
{
{
$ig = new Instagram ( $this -> debug , $this -> truncatedDebug );
try {
// echo 1;
$ig -> login ( $this -> username , $this -> password );
} catch
( \Exception $e ) {
dump ( $e );
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
exit ( 0 );
}
// $list = $ig->collection->getFeed("17906577283646940");
// $list = $ig->collection->getFeed("17906577283646940");
// dump($list->getItems()[0]->getMedia()->getCarouselMedia());exit;
// dump($list->getItems()[0]->getMedia()->getCarouselMedia());exit;
@ -409,7 +409,7 @@ class InstagramService
$thisUserImageDir = $baseImageDir . $trueName . " / " ;
$thisUserImageDir = $baseImageDir . $trueName . " / " ;
// $existFiles = $this->traceExistFiles($thisUserImageDir);
// $existFiles = $this->traceExistFiles($thisUserImageDir);
try {
try {
$userId = $ig -> people -> getUserIdForName ( trim ( $userName ));
$userId = $this -> ig -> people -> getUserIdForName ( trim ( $userName ));
} catch ( \Exception $e ) {
} catch ( \Exception $e ) {
// if ($e instanceof UserNotFou)
// if ($e instanceof UserNotFou)
Log :: error ( " ins get user id for name error: " . $e -> getMessage () . " username is " . $userName );
Log :: error ( " ins get user id for name error: " . $e -> getMessage () . " username is " . $userName );
@ -431,7 +431,7 @@ class InstagramService
// } else {
// } else {
try {
try {
$response = $ig -> story -> getUserReelMediaFeed ( $userId );
$response = $this -> ig -> story -> getUserReelMediaFeed ( $userId );
} catch ( \Exception $e ) {
} catch ( \Exception $e ) {
Log :: error ( " current user has error, $userName , " . $e -> getMessage ());
Log :: error ( " current user has error, $userName , " . $e -> getMessage ());
}
}
@ -449,7 +449,7 @@ class InstagramService
// Request the page corresponding to maxId.
// Request the page corresponding to maxId.
echo " \n current maxId: " . $maxId ;
echo " \n current maxId: " . $maxId ;
try {
try {
$response = $ig -> timeline -> getUserFeed ( $userId , $maxId );
$response = $this -> ig -> timeline -> getUserFeed ( $userId , $maxId );
} catch ( \Exception $e ) {
} catch ( \Exception $e ) {
Log :: error ( " current user has error, $userName , " . $e -> getMessage ());
Log :: error ( " current user has error, $userName , " . $e -> getMessage ());
continue 2 ;
continue 2 ;
@ -552,17 +552,6 @@ class InstagramService
public function scrapeCollection ()
public function scrapeCollection ()
{
{
$ig = new Instagram ( $this -> debug , $this -> truncatedDebug );
try {
// echo 1;
$ig -> login ( $this -> username , $this -> password );
} catch
( \Exception $e ) {
dump ( $e );
echo 'Something went wrong: ' . $e -> getMessage () . " \n " ;
exit ( 0 );
}
// dump($list->getItems()[0]->getMedia()->getCarouselMedia());
// dump($list->getItems()[0]->getMedia()->getCarouselMedia());
@ -573,7 +562,7 @@ class InstagramService
do {
do {
// Request the page corresponding to maxId.
// Request the page corresponding to maxId.
echo " \n current maxId: " . $maxId ;
echo " \n current maxId: " . $maxId ;
$response = $ig -> collection -> getFeed ( " 17906577283646940 " , $maxId );
$response = $this -> ig -> collection -> getFeed ( " 17906577283646940 " , $maxId );
// In this example we're simply printing the IDs of this page's items.
// In this example we're simply printing the IDs of this page's items.
foreach ( $response -> getItems () as $item ) {
foreach ( $response -> getItems () as $item ) {
$userFullName = str_replace ( " / " , " " , $item -> getMedia () -> getUser () -> getFullName ());
$userFullName = str_replace ( " / " , " " , $item -> getMedia () -> getUser () -> getFullName ());