From ecdbc298fa9cbc85fdfdbc9ee3b33c5c26407d10 Mon Sep 17 00:00:00 2001 From: KolyaDobrydnev Date: Tue, 15 Aug 2023 14:22:25 +0300 Subject: [PATCH] recent searches retrieve method --- instagrapi/mixins/fbsearch.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/instagrapi/mixins/fbsearch.py b/instagrapi/mixins/fbsearch.py index 2a5072dc..eecf823b 100644 --- a/instagrapi/mixins/fbsearch.py +++ b/instagrapi/mixins/fbsearch.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Dict, Tuple, Union from instagrapi.extractors import ( extract_hashtag_v1, @@ -73,3 +73,34 @@ def fbsearch_suggested_profiles(self, user_id: str) -> List[UserShort]: } result = self.private_request("fbsearch/accounts_recs/", params=params) return result["users"] + + def fbsearch_recent(self) -> List[Tuple[int, Union[UserShort, Hashtag, Dict]]]: + ''' + Retrieves recently searched results + + Returns + ------- + List[Tuple[int, Union[UserShort, Hashtag, Dict]]] + Returns list of Tuples where first value is timestamp of searh, second is retrived result + ''' + result = self.private_request("fbsearch/recent_searches/") + assert result.get("status", "") == "ok", "Failed to retrieve recent searches" + + data = [] + for item in result.get("recent", []): + if "user" in item.keys(): + data.append(( + item.get("client_time", None), + extract_user_short(item['user']))) + if "hashtag" in item.keys(): + hashtag = item.get("hashtag") + hashtag["media_count"] = hashtag.pop("formatted_media_count") + data.append(( + item.get("client_time", None), + Hashtag(**hashtag))) + if "keyword" in item.keys(): + data.append(( + item.get("client_time", None), + item["keyword"] + )) + return data