diff --git a/doc/changelog.md b/doc/changelog.md index 3dc37288..651f215f 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -13,6 +13,7 @@ * Fixed the way we use to count characters in Twitter. The new methods in TWBlue take into account special characters and URLS as documented in Twitter. ([#199,](https://github.com/manuelcortez/TWBlue/issues/199) [#315](https://github.com/manuelcortez/TWBlue/issues/315)) * Proxy support now works as expected. * Changed translation service from yandex.translate to Google Translator. ([#355,](https://github.com/manuelcortez/TWBlue/issues/355)) +* Improved method to load direct messages in the buffers. Now it should be faster due to less calls to Twitter API performed from the client. * And more. ([#352,](https://github.com/manuelcortez/TWBlue/issues/352)) ## Changes in version 0.95 diff --git a/src/controller/buffers/twitterBuffers.py b/src/controller/buffers/twitterBuffers.py index 6e4d68c0..5e5d03ff 100644 --- a/src/controller/buffers/twitterBuffers.py +++ b/src/controller/buffers/twitterBuffers.py @@ -181,11 +181,14 @@ class baseBufferController(baseBuffers.buffer): val = results val.reverse() log.debug("Retrieved %d items from the cursored search on function %s." %(len(val), self.function)) + user_ids = [item.message_create["sender_id"] for item in val] + self.session.save_users(user_ids) except TweepError as e: log.error("Error %s: %s" % (e.api_code, e.reason)) return number_of_items = self.session.order_buffer(self.name, val) log.debug("Number of items retrieved: %d" % (number_of_items,)) + self.put_items_on_list(number_of_items) if hasattr(self, "finished_timeline") and self.finished_timeline == False: if "-timeline" in self.name: @@ -664,9 +667,10 @@ class directMessagesController(baseBufferController): self.session.db[self.name].append(i) received.insert(0, i) total = total+1 + user_ids = [item.message_create["sender_id"] for item in items] + self.session.save_users(user_ids) pub.sendMessage("more-sent-dms", data=sent, account=self.session.db["user_name"]) selected = self.buffer.list.get_selected() - if self.session.settings["general"]["reverse_timelines"] == True: for i in received: if int(i.message_create["sender_id"]) == self.session.db["user_id"]: diff --git a/src/sessions/twitter/session.py b/src/sessions/twitter/session.py index fcdbea79..e480f83c 100644 --- a/src/sessions/twitter/session.py +++ b/src/sessions/twitter/session.py @@ -430,4 +430,16 @@ class Session(base.baseSession): return self.db["users"][i].id_str user = utils.if_user_exists(self.twitter, screen_name) self.db["users"][user.id_str] = user - return user.id_str \ No newline at end of file + return user.id_str + + def save_users(self, user_ids): + """ Adds all new users to the users database. """ + log.debug("Received %d user IDS to be added in the database." % (len(user_ids))) + users_to_retrieve = [user_id for user_id in user_ids if user_id not in self.db["users"]] + # Remove duplicates + users_to_retrieve = list(dict.fromkeys(users_to_retrieve)) + log.debug("TWBlue will get %d new users from Twitter." % (len(users_to_retrieve))) + users = self.twitter.lookup_users(user_ids=users_to_retrieve, tweet_mode="extended") + for user in users: + self.db["users"][user.id_str] = user + log.debug("Added %d new users" % (len(users))) \ No newline at end of file