diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..71e91f94 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1 @@ +Add getPaginateMediasByLocationId Function \ No newline at end of file diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index c9b4816a..df255376 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -942,6 +942,67 @@ public function getPaginateMediasByTag($tag, $maxId = '') return $toReturn; } + /** + * @param string $facebookLocationId + * @param string $maxId + * + * @return array + * @throws InstagramException + */ + public function getPaginateMediasByLocationId($facebookLocationId, $maxId = '') + { + $hasNextPage = true; + $medias = []; + + $toReturn = [ + 'medias' => $medias, + 'maxId' => $maxId, + 'hasNextPage' => $hasNextPage, + ]; + + $response = Request::get(Endpoints::getMediasJsonByLocationIdLink($facebookLocationId, $maxId), + $this->generateHeaders($this->userSession)); + + if ($response->code !== static::HTTP_OK) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); + } + + $this->parseCookies($response->headers); + + $arr = $this->decodeRawBodyToJson($response->raw_body); + + if (!is_array($arr)) { + throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue'); + } + + if (empty($arr['graphql']['location']['edge_location_to_media']['count'])) { + return $toReturn; + } + + $nodes = $arr['graphql']['location']['edge_location_to_media']['edges']; + + if (empty($nodes)) { + return $toReturn; + } + + foreach ($nodes as $mediaArray) { + $medias[] = Media::create($mediaArray['node']); + } + + $maxId = $arr['graphql']['location']['edge_location_to_media']['page_info']['end_cursor']; + $hasNextPage = $arr['graphql']['location']['edge_location_to_media']['page_info']['has_next_page']; + $count = $arr['graphql']['location']['edge_location_to_media']['count']; + + $toReturn = [ + 'medias' => $medias, + 'count' => $count, + 'maxId' => $maxId, + 'hasNextPage' => $hasNextPage, + ]; + + return $toReturn; + } + /** * @param $tagName *