From 7ceb806af2b22461176f8bfa252ca9486711b55a Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Tue, 22 Dec 2020 17:29:33 -0600 Subject: [PATCH] Added more changes in order to deal with tweet objects --- src/controller/mainController.py | 28 +++---- src/sessions/twitter/compose.py | 118 +++++++++++++------------- src/sessions/twitter/session.py | 80 +++++++++--------- src/sessions/twitter/utils.py | 139 ++++++++++++++++--------------- 4 files changed, 183 insertions(+), 182 deletions(-) diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 4ed92cd0..f6f75c54 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -304,15 +304,15 @@ class Controller(object): self.view.add_buffer(account.buffer , name=session.db["user_name"]) for i in session.settings['general']['buffer_order']: if i == 'home': - home = twitterBuffers.baseBufferController(self.view.nb, "get_home_timeline", "home_timeline", session, session.db["user_name"], sound="tweet_received.ogg", tweet_mode="extended") + home = twitterBuffers.baseBufferController(self.view.nb, "home_timeline", "home_timeline", session, session.db["user_name"], sound="tweet_received.ogg", tweet_mode="extended") self.buffers.append(home) self.view.insert_buffer(home.buffer, name=_(u"Home"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'mentions': - mentions = twitterBuffers.baseBufferController(self.view.nb, "get_mentions_timeline", "mentions", session, session.db["user_name"], sound="mention_received.ogg", tweet_mode="extended") + mentions = twitterBuffers.baseBufferController(self.view.nb, "mentions_timeline", "mentions", session, session.db["user_name"], sound="mention_received.ogg", tweet_mode="extended") self.buffers.append(mentions) self.view.insert_buffer(mentions.buffer, name=_(u"Mentions"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'dm': - dm = twitterBuffers.directMessagesController(self.view.nb, "get_direct_messages", "direct_messages", session, session.db["user_name"], bufferType="dmPanel", compose_func="compose_direct_message", sound="dm_received.ogg", full_text=True, items="events") + dm = twitterBuffers.directMessagesController(self.view.nb, "list_direct_messages", "direct_messages", session, session.db["user_name"], bufferType="dmPanel", compose_func="compose_direct_message", sound="dm_received.ogg", full_text=True, items="events") self.buffers.append(dm) self.view.insert_buffer(dm.buffer, name=_(u"Direct messages"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'sent_dm': @@ -320,62 +320,62 @@ class Controller(object): self.buffers.append(sent_dm) self.view.insert_buffer(sent_dm.buffer, name=_(u"Sent direct messages"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'sent_tweets': - sent_tweets = twitterBuffers.baseBufferController(self.view.nb, "get_user_timeline", "sent_tweets", session, session.db["user_name"], screen_name=session.db["user_name"], tweet_mode="extended") + sent_tweets = twitterBuffers.baseBufferController(self.view.nb, "user_timeline", "sent_tweets", session, session.db["user_name"], screen_name=session.db["user_name"], tweet_mode="extended") self.buffers.append(sent_tweets) self.view.insert_buffer(sent_tweets.buffer, name=_(u"Sent tweets"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'favorites': - favourites = twitterBuffers.baseBufferController(self.view.nb, "get_favorites", "favourites", session, session.db["user_name"], sound="favourite.ogg", tweet_mode="extended") + favourites = twitterBuffers.baseBufferController(self.view.nb, "favorites", "favourites", session, session.db["user_name"], sound="favourite.ogg", tweet_mode="extended") self.buffers.append(favourites) self.view.insert_buffer(favourites.buffer, name=_(u"Likes"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'followers': - followers = twitterBuffers.peopleBufferController(self.view.nb, "get_followers_list", "followers", session, session.db["user_name"], sound="update_followers.ogg", screen_name=session.db["user_name"]) + followers = twitterBuffers.peopleBufferController(self.view.nb, "followers", "followers", session, session.db["user_name"], sound="update_followers.ogg", screen_name=session.db["user_name"]) self.buffers.append(followers) self.view.insert_buffer(followers.buffer, name=_(u"Followers"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'friends': - friends = twitterBuffers.peopleBufferController(self.view.nb, "get_friends_list", "friends", session, session.db["user_name"], screen_name=session.db["user_name"]) + friends = twitterBuffers.peopleBufferController(self.view.nb, "friends", "friends", session, session.db["user_name"], screen_name=session.db["user_name"]) self.buffers.append(friends) self.view.insert_buffer(friends.buffer, name=_(u"Friends"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'blocks': - blocks = twitterBuffers.peopleBufferController(self.view.nb, "list_blocks", "blocked", session, session.db["user_name"]) + blocks = twitterBuffers.peopleBufferController(self.view.nb, "blocks", "blocked", session, session.db["user_name"]) self.buffers.append(blocks) self.view.insert_buffer(blocks.buffer, name=_(u"Blocked users"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'muted': - muted = twitterBuffers.peopleBufferController(self.view.nb, "list_mutes", "muted", session, session.db["user_name"]) + muted = twitterBuffers.peopleBufferController(self.view.nb, "mutes", "muted", session, session.db["user_name"]) self.buffers.append(muted) self.view.insert_buffer(muted.buffer, name=_(u"Muted users"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) timelines = baseBuffers.emptyPanel(self.view.nb, "timelines", session.db["user_name"]) self.buffers.append(timelines) self.view.insert_buffer(timelines.buffer , name=_(u"Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["timelines"]: - tl = twitterBuffers.baseBufferController(self.view.nb, "get_user_timeline", "%s-timeline" % (i,), session, session.db["user_name"], sound="tweet_timeline.ogg", bufferType=None, user_id=i, tweet_mode="extended") + tl = twitterBuffers.baseBufferController(self.view.nb, "user_timeline", "%s-timeline" % (i,), session, session.db["user_name"], sound="tweet_timeline.ogg", bufferType=None, user_id=i, tweet_mode="extended") self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Timeline for {}").format(i,), pos=self.view.search("timelines", session.db["user_name"])) favs_timelines = baseBuffers.emptyPanel(self.view.nb, "favs_timelines", session.db["user_name"]) self.buffers.append(favs_timelines) self.view.insert_buffer(favs_timelines.buffer , name=_(u"Likes timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["favourites_timelines"]: - tl = twitterBuffers.baseBufferController(self.view.nb, "get_favorites", "%s-favorite" % (i,), session, session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, tweet_mode="extended") + tl = twitterBuffers.baseBufferController(self.view.nb, "favorites", "%s-favorite" % (i,), session, session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, tweet_mode="extended") self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Likes for {}").format(i,), pos=self.view.search("favs_timelines", session.db["user_name"])) followers_timelines = baseBuffers.emptyPanel(self.view.nb, "followers_timelines", session.db["user_name"]) self.buffers.append(followers_timelines) self.view.insert_buffer(followers_timelines.buffer , name=_(u"Followers' Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["followers_timelines"]: - tl = twitterBuffers.peopleBufferController(self.view.nb, "get_followers_list", "%s-followers" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) + tl = twitterBuffers.peopleBufferController(self.view.nb, "followers", "%s-followers" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Followers for {}").format(i,), pos=self.view.search("followers_timelines", session.db["user_name"])) friends_timelines = baseBuffers.emptyPanel(self.view.nb, "friends_timelines", session.db["user_name"]) self.buffers.append(friends_timelines) self.view.insert_buffer(friends_timelines.buffer , name=_(u"Friends' Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["friends_timelines"]: - tl = twitterBuffers.peopleBufferController(self.view.nb, "get_friends_list", "%s-friends" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) + tl = twitterBuffers.peopleBufferController(self.view.nb, "friends", "%s-friends" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Friends for {}").format(i,), pos=self.view.search("friends_timelines", session.db["user_name"])) lists = baseBuffers.emptyPanel(self.view.nb, "lists", session.db["user_name"]) self.buffers.append(lists) self.view.insert_buffer(lists.buffer , name=_(u"Lists"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["lists"]: - tl = twitterBuffers.listBufferController(self.view.nb, "get_list_statuses", "%s-list" % (i,), session, session.db["user_name"], bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), tweet_mode="extended") + tl = twitterBuffers.listBufferController(self.view.nb, "list_timeline", "%s-list" % (i,), session, session.db["user_name"], bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), tweet_mode="extended") session.lists.append(tl) self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"List for {}").format(i), pos=self.view.search("lists", session.db["user_name"])) diff --git a/src/sessions/twitter/compose.py b/src/sessions/twitter/compose.py index 3b7abdaf..21496d2f 100644 --- a/src/sessions/twitter/compose.py +++ b/src/sessions/twitter/compose.py @@ -39,41 +39,41 @@ chars = "abcdefghijklmnopqrstuvwxyz" def compose_tweet(tweet, db, relative_times, show_screen_names=False, session=None): """ It receives a tweet and returns a list with the user, text for the tweet or message, date and the client where user is.""" if system == "Windows": - original_date = arrow.get(tweet["created_at"], "ddd MMM DD H:m:s Z YYYY", locale="en") + original_date = arrow.get(tweet.created_at, "ddd MMM DD H:m:s Z YYYY", locale="en") if relative_times == True: ts = original_date.humanize(locale=languageHandler.curLang[:2]) else: ts = original_date.shift(seconds=db["utc_offset"]).format(_(u"dddd, MMMM D, YYYY H:m:s"), locale=languageHandler.curLang[:2]) else: - ts = tweet["created_at"] - if "message" in tweet: + ts = tweet.created_at + if hasattr(tweet, "message"): value = "message" - elif "full_text" in tweet: + elif hasattr(tweet, "full_text"): value = "full_text" else: value = "text" - if "retweeted_status" in tweet and value != "message": - text = StripChars(tweet["retweeted_status"][value]) + if hasattr(tweet, "retweeted_status") and value != "message": + text = StripChars(getattr(tweet.retweeted_status, value)) else: - text = StripChars(tweet[value]) + text = StripChars(getattr(tweet, value)) if show_screen_names: - user = tweet["user"]["screen_name"] + user = tweet.user.screen_name else: - user = tweet["user"]["name"] - source = re.sub(r"(?s)<.*?>", "", tweet["source"]) - if "retweeted_status" in tweet: - if ("message" in tweet) == False and tweet["retweeted_status"]["is_quote_status"] == False: - text = "RT @%s: %s" % (tweet["retweeted_status"]["user"]["screen_name"], text) - elif tweet["retweeted_status"]["is_quote_status"]: + user = tweet.user.name + source = re.sub(r"(?s)<.*?>", "", tweet.source) + if hasattr(tweet, "retweeted_status"): + if (hasattr(tweet, "message")) == False and tweet.retweeted_status.is_quote_status == False: + text = "RT @%s: %s" % (tweet.retweeted_status.user.screen_name, text) + elif tweet.retweeted_status.is_quote_status: text = "%s" % (text) else: - text = "RT @%s: %s" % (tweet["retweeted_status"]["user"]["screen_name"], text) - if ("message" in tweet) == False: + text = "RT @%s: %s" % (tweet.retweeted_status.user.screen_name, text) + if not hasattr(tweet, "message"): - if "retweeted_status" in tweet: - text = utils.expand_urls(text, tweet["retweeted_status"]["entities"]) + if hasattr(tweet, "retweeted_status"): + text = utils.expand_urls(text, tweet.retweeted_status.entities) else: - text = utils.expand_urls(text, tweet["entities"]) + text = utils.expand_urls(text, tweet.entities) if config.app['app-settings']['handle_longtweets']: pass return [user+", ", text, ts+", ", source] @@ -83,76 +83,76 @@ def compose_direct_message(item, db, relative_times, show_screen_names=False, se if system == "Windows": # Let's remove the last 3 digits in the timestamp string. # Twitter sends their "epoch" timestamp with 3 digits for milliseconds and arrow doesn't like it. - original_date = arrow.get(int(item["created_timestamp"][:-3])) + original_date = arrow.get(int(item.created_timestamp[:-3])) if relative_times == True: ts = original_date.humanize(locale=languageHandler.curLang[:2]) else: ts = original_date.shift(seconds=db["utc_offset"]).format(_(u"dddd, MMMM D, YYYY H:m:s"), locale=languageHandler.curLang[:2]) else: - ts = item["created_timestamp"] - text = StripChars(item["message_create"]["message_data"]["text"]) + ts = item.created_timestamp + text = StripChars(item.message_create.message_data.text) source = "DM" - sender = session.get_user(item["message_create"]["sender_id"]) - if db["user_name"] == sender["screen_name"]: + sender = session.get_user(item.message_create.sender_id) + if db["user_name"] == sender.screen_name: if show_screen_names: - user = _(u"Dm to %s ") % (session.get_user(item["message_create"]["target"]["recipient_id"])["screen_name"]) + user = _(u"Dm to %s ") % (session.get_user(item.message_create.target.recipient_id).screen_name) else: - user = _(u"Dm to %s ") % (session.get_user(item["message_create"]["target"]["recipient_id"])["name"]) + user = _(u"Dm to %s ") % (session.get_user(item.message_create.target.recipient_id).name) else: if show_screen_names: - user = sender["screen_name"] + user = sender.screen_name else: - user = sender["name"] + user = sender.name if text[-1] in chars: text=text+"." - text = utils.expand_urls(text, item["message_create"]["message_data"]["entities"]) + text = utils.expand_urls(text, item.message_create.message_data.entities) return [user+", ", text, ts+", ", source] def compose_quoted_tweet(quoted_tweet, original_tweet, show_screen_names=False, session=None): """ It receives a tweet and returns a list with the user, text for the tweet or message, date and the client where user is.""" - if "retweeted_status" in quoted_tweet: - if "full_text" in quoted_tweet["retweeted_status"]: + if hasattr(quoted_tweet, "retweeted_status"): + if hasattr(quoted_tweet.retweeted_status, "full_text"): value = "full_text" else: value = "text" - text = StripChars(quoted_tweet["retweeted_status"][value]) + text = StripChars(getattr(quoted_tweet.retweeted_status, value)) else: - if "full_text" in quoted_tweet: + if hasattr(quoted_tweet, "full_text"): value = "full_text" else: value = "text" - text = StripChars(quoted_tweet[value]) + text = StripChars(getattr(quoted_tweet, value)) if show_screen_names: - quoting_user = quoted_tweet["user"]["screen_name"] + quoting_user = quoted_tweet.user.screen_name else: - quoting_user = quoted_tweet["user"]["name"] - source = re.sub(r"(?s)<.*?>", "", quoted_tweet["source"]) - if "retweeted_status" in quoted_tweet: - text = "rt @%s: %s" % (quoted_tweet["retweeted_status"]["user"]["screen_name"], text) + quoting_user = quoted_tweet.user.name + source = re.sub(r"(?s)<.*?>", "", quoted_tweet.source) + if hasattr(quoted_tweet, "retweeted_status"): + text = "rt @%s: %s" % (quoted_tweet.retweeted_status.user.screen_name, text) if text[-1] in chars: text=text+"." - original_user = original_tweet["user"]["screen_name"] - if "message" in original_tweet: - original_text = original_tweet["message"] - elif "full_text" in original_tweet: - original_text = StripChars(original_tweet["full_text"]) + original_user = original_tweet.user.screen_name + if hasattr(original_tweet, "message"): + original_text = original_tweet.message + elif hasattr(original_tweet, "full_text"): + original_text = StripChars(original_tweet.full_text) else: - original_text = StripChars(original_tweet["text"]) - quoted_tweet["message"] = _(u"{0}. Quoted tweet from @{1}: {2}").format( text, original_user, original_text) + original_text = StripChars(original_tweet.text) + quoted_tweet.message = _(u"{0}. Quoted tweet from @{1}: {2}").format( text, original_user, original_text) quoted_tweet = tweets.clear_url(quoted_tweet) - quoted_tweet["entities"]["urls"].extend(original_tweet["entities"]["urls"]) + quoted_tweet.entities.urls.extend(original_tweet.entities.urls) return quoted_tweet def compose_followers_list(tweet, db, relative_times=True, show_screen_names=False, session=None): if system == "Windows": - original_date = arrow.get(tweet["created_at"], "ddd MMM D H:m:s Z YYYY", locale="en") + original_date = arrow.get(tweet.created_at, "ddd MMM D H:m:s Z YYYY", locale="en") if relative_times == True: ts = original_date.humanize(locale=languageHandler.curLang[:2]) else: ts = original_date.shift(seconds=db["utc_offset"]).format(_(u"dddd, MMMM D, YYYY H:m:s"), locale=languageHandler.curLang[:2]) else: - ts = tweet["created_at"] - if "status" in tweet: - if len(tweet["status"]) > 4 and system == "Windows": - original_date2 = arrow.get(tweet["status"]["created_at"], "ddd MMM D H:m:s Z YYYY", locale="en") + ts = tweet.created_at + if hasattr(tweet, "status"): + if len(tweet.status) > 4 and system == "Windows": + original_date2 = arrow.get(tweet.status.created_at, "ddd MMM D H:m:s Z YYYY", locale="en") if relative_times: ts2 = original_date2.humanize(locale=languageHandler.curLang[:2]) else: @@ -161,14 +161,14 @@ def compose_followers_list(tweet, db, relative_times=True, show_screen_names=Fal ts2 = _("Unavailable") else: ts2 = _("Unavailable") - return [_(u"%s (@%s). %s followers, %s friends, %s tweets. Last tweeted %s. Joined Twitter %s") % (tweet["name"], tweet["screen_name"], tweet["followers_count"], tweet["friends_count"], tweet["statuses_count"], ts2, ts)] + return [_(u"%s (@%s). %s followers, %s friends, %s tweets. Last tweeted %s. Joined Twitter %s") % (tweet.name, tweet.screen_name, tweet.followers_count, tweet.friends_count, tweet.statuses_count, ts2, ts)] def compose_list(list): - name = list["name"] - if list["description"] == None: description = _(u"No description available") - else: description = list["description"] - user = list["user"]["name"] - members = str(list["member_count"]) - if list["mode"] == "private": status = _(u"private") + name = list.name + if list.description == None: description = _(u"No description available") + else: description = list.description + user = list.user.name + members = str(list.member_count) + if list.mode == "private": status = _(u"private") else: status = _(u"public") return [name, description, user, members, status] diff --git a/src/sessions/twitter/session.py b/src/sessions/twitter/session.py index 8fffc5fe..3b584d40 100644 --- a/src/sessions/twitter/session.py +++ b/src/sessions/twitter/session.py @@ -40,24 +40,24 @@ class Session(base.baseSession): self.db["users"] = {} if ignore_older and len(self.db[name]) > 0: if self.settings["general"]["reverse_timelines"] == False: - last_id = self.db[name][0]["id"] + last_id = self.db[name][0].id else: - last_id = self.db[name][-1]["id"] + last_id = self.db[name][-1].id for i in data: if ignore_older and last_id != None: - if i["id"] < last_id: - log.error("Ignoring an older tweet... Last id: {0}, tweet id: {1}".format(last_id, i["id"])) + if i.id < last_id: + log.error("Ignoring an older tweet... Last id: {0}, tweet id: {1}".format(last_id, i.id)) continue - if utils.find_item(i["id"], self.db[name]) == None and utils.is_allowed(i, self.settings, name) == True: + if utils.find_item(i.id, self.db[name]) == None and utils.is_allowed(i, self.settings, name) == True: i = self.check_quoted_status(i) i = self.check_long_tweet(i) if i == False: continue if self.settings["general"]["reverse_timelines"] == False: self.db[name].append(i) else: self.db[name].insert(0, i) num = num+1 - if ("user" in i) == True: - if (i["user"]["id"] in self.db["users"]) == False: - self.db["users"][i["user"]["id"]] = i["user"] + if hasattr(i, "user"): + if (i.user.id in self.db["users"]) == False: + self.db["users"][i.user.id] = i.user return num def order_cursored_buffer(self, name, data): @@ -74,7 +74,7 @@ class Session(base.baseSession): self.db[name] = {} self.db[name]["items"] = [] for i in data: - if utils.find_item(i["id"], self.db[name]["items"]) == None: + if utils.find_item(i.id, self.db[name]["items"]) == None: if self.settings["general"]["reverse_timelines"] == False: self.db[name]["items"].append(i) else: self.db[name]["items"].insert(0, i) num = num+1 @@ -90,13 +90,13 @@ class Session(base.baseSession): self.db["direct_messages"] = {} self.db["direct_messages"]["items"] = [] for i in data: - if i["message_create"]["sender_id"] == self.db["user_id"]: - if "sent_direct_messages" in self.db and utils.find_item(i["id"], self.db["sent_direct_messages"]["items"]) == None: + if i.message_create.sender_id == self.db["user_id"]: + if "sent_direct_messages" in self.db and utils.find_item(i.id, self.db["sent_direct_messages"]["items"]) == None: if self.settings["general"]["reverse_timelines"] == False: self.db["sent_direct_messages"]["items"].append(i) else: self.db["sent_direct_messages"]["items"].insert(0, i) sent = sent+1 else: - if utils.find_item(i["id"], self.db["direct_messages"]["items"]) == None: + if utils.find_item(i.id, self.db["direct_messages"]["items"]) == None: if self.settings["general"]["reverse_timelines"] == False: self.db["direct_messages"]["items"].append(i) else: self.db["direct_messages"]["items"].insert(0, i) incoming = incoming+1 @@ -229,15 +229,15 @@ class Session(base.baseSession): def search(self, name, *args, **kwargs): """ Search in twitter, passing args and kwargs as arguments to the Twython function.""" tl = self.twitter.search(*args, **kwargs) - tl["statuses"].reverse() - return tl["statuses"] + tl.reverse() + return tl # @_require_login def get_favourites_timeline(self, name, *args, **kwargs): """ Gets favourites for the authenticated user or a friend or follower. name str: Name for storage in the database. args and kwargs are passed directly to the Twython function.""" - tl = self.call_paged("get_favorites", *args, **kwargs) + tl = self.call_paged("favorites", *args, **kwargs) return self.order_buffer(name, tl) def call_paged(self, update_function, *args, **kwargs): @@ -251,8 +251,8 @@ class Session(base.baseSession): data = getattr(self.twitter, update_function)(count=self.settings["general"]["max_tweets_per_call"], *args, **kwargs) results.extend(data) for i in range(0, max): - if i == 0: max_id = results[-1]["id"] - else: max_id = results[0]["id"] + if i == 0: max_id = results[-1].id + else: max_id = results[0].id data = getattr(self.twitter, update_function)(max_id=max_id, count=self.settings["general"]["max_tweets_per_call"], *args, **kwargs) results.extend(data) results.reverse() @@ -357,25 +357,25 @@ class Session(base.baseSession): def get_quoted_tweet(self, tweet): """ Process a tweet and extract all information related to the quote.""" quoted_tweet = tweet - if "full_text" in tweet: + if hasattr(tweet, "full_text"): value = "full_text" else: value = "text" - quoted_tweet[value] = utils.expand_urls(quoted_tweet[value], quoted_tweet["entities"]) - if "quoted_status" in quoted_tweet: - original_tweet = quoted_tweet["quoted_status"] - elif "retweeted_status" in quoted_tweet and "quoted_status" in quoted_tweet["retweeted_status"]: - original_tweet = quoted_tweet["retweeted_status"]["quoted_status"] + setattr(quoted_tweet, value)(utils.expand_urls(getattr(quoted_tweet, value), quoted_tweet.entities)) + if hasattr(quoted_tweet, "quoted_status"): + original_tweet = quoted_tweet.quoted_status + elif hasattr(quoted_tweet, "retweeted_status") and hasattr(quoted_tweet.retweeted_status, "quoted_status"): + original_tweet = quoted_tweet.retweeted_status.quoted_status else: return quoted_tweet original_tweet = self.check_long_tweet(original_tweet) - if "full_text" in original_tweet: + if hasattr(original_tweet, "full_text"): value = "full_text" - elif "message" in original_tweet: + elif hasattr(original_tweet, "message"): value = "message" else: value = "text" - original_tweet[value] = utils.expand_urls(original_tweet[value], original_tweet["entities"]) + setattr(original_tweet, value)(utils.expand_urls(getattr(original_tweet, value), original_tweet.entities)) return compose.compose_quoted_tweet(quoted_tweet, original_tweet) def check_long_tweet(self, tweet): @@ -385,22 +385,22 @@ class Session(base.baseSession): long = twishort.is_long(tweet) if long != False and config.app["app-settings"]["handle_longtweets"]: message = twishort.get_full_text(long) - if "quoted_status" in tweet: - tweet["quoted_status"]["message"] = message - if tweet["quoted_status"]["message"] == False: return False - tweet["quoted_status"]["twishort"] = True - for i in tweet["quoted_status"]["entities"]["user_mentions"]: - if "@%s" % (i["screen_name"]) not in tweet["quoted_status"]["message"] and i["screen_name"] != tweet["user"]["screen_name"]: - if "retweeted_status" in tweet["quoted_status"] and tweet["retweeted_status"]["user"]["screen_name"] == i["screen_name"]: + if hasattr(tweet, "quoted_status"): + tweet.quoted_status.message = message + if tweet.quoted_status.message == False: return False + tweet.quoted_status.twishort = True + for i in tweet.quoted_status.entities.user_mentions: + if "@%s" % (i.screen_name) not in tweet.quoted_status.message and i.screen_name != tweet.user.screen_name: + if hasattr(tweet["quoted_status"], "retweeted_status") and tweet.retweeted_status.user.screen_name == i.screen_name: continue - tweet["quoted_status"]["message"] = u"@%s %s" % (i["screen_name"], tweet["message"]) + tweet.quoted_status.message = u"@%s %s" % (i.screen_name, tweet.message) else: - tweet["message"] = message - if tweet["message"] == False: return False - tweet["twishort"] = True - for i in tweet["entities"]["user_mentions"]: - if "@%s" % (i["screen_name"]) not in tweet["message"] and i["screen_name"] != tweet["user"]["screen_name"]: - if "retweeted_status" in tweet and tweet["retweeted_status"]["user"]["screen_name"] == i["screen_name"]: + tweet.message = message + if tweet.message == False: return False + tweet.twishort = True + for i in tweet.entities.user_mentions: + if "@%s" % (i.screen_name) not in tweet.message and i.screen_name != tweet.user.screen_name: + if hasattr(tweet, "retweeted_status") and tweet.retweeted_status.user.screen_name == i.screen_name: continue return tweet diff --git a/src/sessions/twitter/utils.py b/src/sessions/twitter/utils.py index ff4db939..dfdabfa5 100644 --- a/src/sessions/twitter/utils.py +++ b/src/sessions/twitter/utils.py @@ -28,35 +28,35 @@ def find_urls_in_text(text): def find_urls (tweet): urls = [] # Let's add URLS from tweet entities. - if "message_create" in tweet: - entities = tweet["message_create"]["message_data"]["entities"] + if hasattr(tweet, "message_create"): + entities = tweet.message_create.message_data.entities else: - entities = tweet["entities"] - for i in entities["urls"]: - if i["expanded_url"] not in urls: - urls.append(i["expanded_url"]) - if "quoted_status" in tweet: - for i in tweet["quoted_status"]["entities"]["urls"]: - if i["expanded_url"] not in urls: - urls.append(i["expanded_url"]) - if "retweeted_status" in tweet: - for i in tweet["retweeted_status"]["entities"]["urls"]: - if i["expanded_url"] not in urls: - urls.append(i["expanded_url"]) - if "quoted_status" in tweet["retweeted_status"]: - for i in tweet["retweeted_status"]["quoted_status"]["entities"]["urls"]: - if i["expanded_url"] not in urls: - urls.append(i["expanded_url"]) - if "message" in tweet: + entities = tweet.entities + for i in entities.urls: + if i.expanded_url not in urls: + urls.append(i.expanded_url) + if hasattr(tweet, "quoted_status"): + for i in tweet.quoted_status.entities.urls: + if i.expanded_url not in urls: + urls.append(i.expanded_url) + if hasattr(tweet, "retweeted_status"): + for i in tweet.retweeted_status.entities.urls: + if i.expanded_url not in urls: + urls.append(i.expanded_url) + if hasattr(tweet["retweeted_status"], "quoted_status"): + for i in tweet.retweeted_status.quoted_status.entities.urls: + if i.expanded_url not in urls: + urls.append(i.expanded_url) + if hasattr(tweet, "message"): i = "message" - elif "full_text" in tweet: + elif hasattr(tweet, "full_text"): i = "full_text" else: i = "text" - if "message_create" in tweet: - extracted_urls = find_urls_in_text(tweet["message_create"]["message_data"]["text"]) + if hasattr(tweet, "message_create"): + extracted_urls = find_urls_in_text(tweet.message_create.message_data.text) else: - extracted_urls = find_urls_in_text(tweet[i]) + extracted_urls = find_urls_in_text(getattr(tweet, i)) # Don't include t.co links (mostly they are photos or shortened versions of already added URLS). for i in extracted_urls: if i not in urls and "https://t.co" not in i: @@ -65,70 +65,70 @@ def find_urls (tweet): def find_item(id, listItem): for i in range(0, len(listItem)): - if listItem[i]["id"] == id: return i + if listItem[i].id == id: return i return None def find_list(name, lists): for i in range(0, len(lists)): - if lists[i]["name"] == name: return lists[i]["id"] + if lists[i].name == name: return lists[i].id def is_audio(tweet): try: if len(find_urls(tweet)) < 1: return False - if "message_create" in tweet: - entities = tweet["message_create"]["message_data"]["entities"] + if hasattr(tweet, "message_create"): + entities = tweet.message_create.message_data.entities else: - entities = tweet["entities"] - if len(entities["hashtags"]) > 0: - for i in entities["hashtags"]: - if i["text"] == "audio": + entities = tweet.entities + if len(entities.hashtags) > 0: + for i in entities.hashtags: + if i.text == "audio": return True except IndexError: - print(tweet["entities"]["hashtags"]) + print(tweet.entities.hashtags) log.exception("Exception while executing is_audio hashtag algorithm") def is_geocoded(tweet): - if "coordinates" in tweet and tweet["coordinates"] != None: + if hasattr(tweet, "coordinates") and tweet.coordinates != None: return True def is_media(tweet): - if "message_create" in tweet: - entities = tweet["message_create"]["message_data"]["entities"] + if hasattr(tweet, "message_create"): + entities = tweet.message_create.message_data.entities else: - entities = tweet["entities"] - if ("media" in entities) == False: + entities = tweet.entities + if not hasattr(entities, media): return False - for i in entities["media"]: - if "type" in i and i["type"] == "photo": + for i in entities.media: + if hasattr(i, "type") and i.type == "photo": return True return False def get_all_mentioned(tweet, conf, field="screen_name"): """ Gets all users that have been mentioned.""" results = [] - for i in tweet["entities"]["user_mentions"]: - if i["screen_name"] != conf["user_name"] and i["screen_name"] != tweet["user"]["screen_name"]: - if i[field] not in results: - results.append(i[field]) + for i in tweet.entities.user_mentions: + if i.screen_name != conf["user_name"] and i.screen_name != tweet.user.screen_name: + if getattr(i, field) not in results: + results.append(getattr(i, field)) return results def get_all_users(tweet, conf): string = [] - if "retweeted_status" in tweet: - string.append(tweet["user"]["screen_name"]) - tweet = tweet["retweeted_status"] - if "sender" in tweet: - string.append(tweet["sender"]["screen_name"]) + if hasattr(tweet, "retweeted_status"): + string.append(tweet.user.screen_name) + tweet = tweet.retweeted_status + if hasattr(tweet, "sender"): + string.append(tweet.sender.screen_name) else: - if tweet["user"]["screen_name"] != conf["user_name"]: - string.append(tweet["user"]["screen_name"]) - for i in tweet["entities"]["user_mentions"]: - if i["screen_name"] != conf["user_name"] and i["screen_name"] != tweet["user"]["screen_name"]: - if i["screen_name"] not in string: - string.append(i["screen_name"]) + if tweet.user.screen_name != conf["user_name"]: + string.append(tweet.user.screen_name) + for i in tweet.entities.user_mentions: + if i.screen_name != conf["user_name"] and i.screen_name != tweet.user.screen_name: + if i.screen_name not in string: + string.append(i.screen_name) if len(string) == 0: - string.append(tweet["user"]["screen_name"]) + string.append(tweet.user.screen_name) return string def if_user_exists(twitter, user): @@ -143,24 +143,25 @@ def if_user_exists(twitter, user): def is_allowed(tweet, settings, buffer_name): clients = settings["twitter"]["ignored_clients"] - if "sender" in tweet: return True + if hasattr(tweet, "sender"): return True allowed = True tweet_data = {} - if "retweeted_status" in tweet: + if hasattr(tweet, "retweeted_status"): tweet_data["retweet"] = True - if tweet["in_reply_to_status_id_str"] != None: + if tweet.in_reply_to_status_id_str != None: tweet_data["reply"] = True - if "quoted_status" in tweet: + if hasattr(tweet, "quoted_status"): tweet_data["quote"] = True - if "retweeted_status" in tweet: tweet = tweet["retweeted_status"] - source = re.sub(r"(?s)<.*?>", "", tweet["source"]) + if hasattr(tweet, "retweeted_status"): + tweet = tweet.retweeted_status + source = re.sub(r"(?s)<.*?>", "", tweet.source) for i in clients: if i.lower() == source.lower(): return False return filter_tweet(tweet, tweet_data, settings, buffer_name) def filter_tweet(tweet, tweet_data, settings, buffer_name): - if "full_text" in tweet: + if hasattr(tweet, "full_text"): value = "full_text" else: value = "text" @@ -188,16 +189,16 @@ def filter_tweet(tweet, tweet_data, settings, buffer_name): if allow_replies == "False" and "reply" in tweet_data: return False if word != "" and settings["filters"][i]["if_word_exists"]: - if word in tweet[value]: + if word in getattr(tweet, value): return False elif word != "" and settings["filters"][i]["if_word_exists"] == False: - if word not in tweet[value]: + if word not in getattr(tweet, value): return False if settings["filters"][i]["in_lang"] == "True": - if tweet["lang"] not in settings["filters"][i]["languages"]: + if getattr(tweet, lang) not in settings["filters"][i]["languages"]: return False elif settings["filters"][i]["in_lang"] == "False": - if tweet["lang"] in settings["filters"][i]["languages"]: + if tweet.lang in settings["filters"][i]["languages"]: return False return True @@ -213,7 +214,7 @@ def twitter_error(error): def expand_urls(text, entities): """ Expand all URLS present in text with information found in entities""" urls = find_urls_in_text(text) - for url in entities["urls"]: - if url["url"] in text: - text = text.replace(url["url"], url["expanded_url"]) + for url in entities.urls: + if url.url in text: + text = text.replace(url.url, url.expanded_url) return text