def get_likes(self, media, pointer=None, count=20, settings={}, limit=50): if not isinstance(media, Media): raise TypeError("'media' must be Media type") if not isinstance(count, int): raise TypeError("'count' must be int type") if not isinstance(settings, dict): raise TypeError("'settings' must be dict type") if not isinstance(limit, int): raise TypeError("'limit' must be int type") self.update(media, settings) if pointer: variables_string = '{{"shortcode":"{shortcode}","first":{first},"after":"{after}"}}' else: variables_string = '{{"shortcode":"{shortcode}","first":{first}}}' likes = [] while True: data = {"shortcode": media.code, "first": min(limit, count)} if pointer: data["after"] = pointer response = self._graphql_request( query_hash="1cb6ec562846122743b61e492c85999f", variables=variables_string.format(**data), settings=settings, ) try: data = response.json( )["data"]["shortcode_media"]["edge_liked_by"] edges = data["edges"] page_info = data["page_info"] media.likes_count = data["count"] for index in range(min(len(edges), count)): node = edges[index]["node"] account = Account(node["username"]) account.id = node["id"] account.profile_pic_url = node["profile_pic_url"] account.is_verified = node["is_verified"] account.full_name = node["full_name"] media.likes.add(account) likes.append(account) if page_info["has_next_page"]: pointer = page_info["end_cursor"] else: pointer = None if len(edges) < count and page_info["has_next_page"]: count = count - len(edges) variables_string = \ '{{"shortcode":"{shortcode}","first":{first},"after":"{after}"}}' else: return likes, pointer except (ValueError, KeyError) as exception: raise UnexpectedResponse(exception, response.url, response.text)
def get_likes(self, media, settings={}): # Check data if not isinstance(settings, dict): raise TypeError("'settings' must be dict type") if not isinstance(media, Media): raise TypeError("'media' must be Media type") data = self.update(media, settings) likes = [] try: data = data["edge_media_preview_like"] edges = data["edges"] for edge in edges: node = edge["node"] account = Account(node["username"]) account.id = node["id"] account.profile_pic_url = node["profile_pic_url"] if "is_verified" in node: account.is_verified = node["is_verified"] if "full_name" in node: account.full_name = node["full_name"] media.likes.add(account) likes.append(account) except (ValueError, KeyError): raise UnexpectedResponse() return likes, None
def get_followers(self, account=None, pointer=None, count=20, settings={}, limit=50): if account is None: account = self if not isinstance(settings, dict): raise TypeError("'settings' must be dict type") if not isinstance(count, int): raise TypeError("'count' must be int type") if not isinstance(account, Account): raise TypeError("'account' must be Account type") if not isinstance(limit, int): raise TypeError("'limit' must be int type") self.update(account, settings) if pointer is None: variables_string = '{{"id":"{id}","first":{first}}}' else: variables_string = '{{"id":"{id}","first":{first},"after":"{after}"}}' followers = [] while True: data = {"first": min(limit, count), "id": account.id} if not pointer is None: data["after"] = pointer response = self._graphql_request( query_hash="37479f2b8209594dde7facb0d904896a", variables=variables_string.format(**data), settings=settings, ) try: data = response.json()["data"]["user"]["edge_followed_by"] edges = data["edges"] page_info = data["page_info"] account.followers_count = data["count"] for index in range(min(len(edges), count)): node = edges[index]["node"] a = Account(node["username"]) a.id = node["id"] a.profile_pic_url = node["profile_pic_url"] a.is_verified = node["is_verified"] a.full_name = node["full_name"] account.followers.add(a) followers.append(a) if page_info["has_next_page"]: pointer = page_info["end_cursor"] else: pointer = None if len(edges) < count and page_info["has_next_page"]: count = count - len(edges) variables_string = '{{"id":"{id}","first":{first},"after":"{after}"}}' else: return followers, pointer except (ValueError, KeyError) as exception: raise UnexpectedResponse(exception, response.url, response.text)
def get_followers(self, account=None, pointer=None, count=20, settings={}, limit=50): if account is None: account = self if not isinstance(settings, dict): raise TypeError("'settings' must be dict type") if not isinstance(count, int): raise TypeError("'count' must be int type") if not isinstance(account, Account): raise TypeError("'account' must be Account type") if not isinstance(limit, int): raise TypeError("'limit' must be int type") self.update(account, settings) query_hash = "37479f2b8209594dde7facb0d904896a" if pointer is None: variables_string = '{{"id":"{id}","first":{first}}}' else: variables_string = '{{"id":"{id}","first":{first},"after":"{after}"}}' followers = [] if "params" in settings: settings["params"]["query_hash"] = query_hash else: settings["params"] = {"query_hash": query_hash} while True: data = {"first": min(limit, count), "id": account.id} if not pointer is None: data["after"] = pointer settings["params"]["variables"] = variables_string.format(**data) if not "headers" in settings: settings["headers"] = { "X-Instagram-GIS": "%s:%s" % (self._rhx_gis, settings["params"]["variables"]), } else: settings["headers"]["X-Instagram-GIS"] = \ "%s:%s" % (self._rhx_gis, settings["params"]["variables"]) settings["headers"]["X-Instagram-GIS"] = \ hashlib.md5(settings["headers"]["X-Instagram-GIS"].encode("utf-8")).hexdigest() settings["headers"]["X-Requested-With"] = "XMLHttpRequest" response = self._get_request("https://www.instagram.com/graphql/query/", **settings) try: data = response.json()["data"]["user"]["edge_followed_by"] edges = data["edges"] page_info = data["page_info"] account.followers_count = data["count"] for index in range(min(len(edges), count)): node = edges[index]["node"] a = Account(node["username"]) a.id = node["id"] a.profile_pic_url = node["profile_pic_url"] a.is_verified = node["is_verified"] a.full_name = node["full_name"] account.followers.add(a) followers.append(a) if page_info["has_next_page"]: pointer = page_info["end_cursor"] else: pointer = None if len(edges) < count and page_info["has_next_page"]: count = count-len(edges) variables_query = '{{"id":"{id}","first":{first},"after":"{after}"}}' else: return followers, pointer except (ValueError, KeyError): raise UnexpectedResponse(response.url, response.text)
def get_likes(self, media, pointer=None, count=20, settings={}, limit=50): if not isinstance(media, Media): raise TypeError("'media' must be Media type") if not isinstance(count, int): raise TypeError("'count' must be int type") if not isinstance(settings, dict): raise TypeError("'settings' must be dict type") if not isinstance(limit, int): raise TypeError("'limit' must be int type") self.update(media, settings) query_hash = "1cb6ec562846122743b61e492c85999f" if pointer: variables_string = '{{"shortcode":"{shortcode}","first":{first},"after":"{after}"}}' else: variables_string = '{{"shortcode":"{shortcode}","first":{first}}}' likes = [] if "params" in settings: settings["params"]["query_hash"] = query_hash else: settings["params"] = {"query_hash": query_hash} while True: data = {"shortcode": media.code, "first": min(limit, count)} if pointer: data["after"] = pointer settings["params"]["variables"] = variables_string.format(**data) if not "headers" in settings: settings["headers"] = { "X-Instagram-GIS": "%s:%s" % (self._rhx_gis, settings["params"]["variables"]), } else: settings["headers"]["X-Instagram-GIS"] = \ "%s:%s" % (self._rhx_gis, settings["params"]["variables"]) settings["headers"]["X-Instagram-GIS"] = \ hashlib.md5(settings["headers"]["X-Instagram-GIS"].encode("utf-8")).hexdigest() settings["headers"]["X-Requested-With"] = "XMLHttpRequest" response = self._get_request("https://www.instagram.com/graphql/query/", **settings) try: data = response.json()["data"]["shortcode_media"]["edge_liked_by"] edges = data["edges"] page_info = data["page_info"] media.likes_count = data["count"] for index in range(min(len(edges), count)): node = edges[index]["node"] account = Account(node["username"]) account.id = node["id"] account.profile_pic_url = node["profile_pic_url"] account.is_verified = node["is_verified"] account.full_name = node["full_name"] media.likes.add(account) likes.append(account) if page_info["has_next_page"]: pointer = page_info["end_cursor"] else: pointer = None if len(edges) < count and page_info["has_next_page"]: count = count-len(edges) variables_string = \ '{{"shortcode":"{shortcode}","first":{first},"after":"{after}"}}' else: return likes, pointer except (ValueError, KeyError): raise UnexpectedResponse(response.url, response.text)