From 1e22ebce6175401f9758a0a653caa0f9f2350e78 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Mon, 13 Nov 2017 17:45:01 -0600 Subject: [PATCH] First filter implementation. Needs lots of tests. #102 --- src/sessionmanager/session.py | 2 +- src/twitter/buffers/indibidual.py | 4 ++-- src/twitter/buffers/stream.py | 4 ++-- src/twitter/utils.py | 28 ++++++++++++++++++++++++---- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/sessionmanager/session.py b/src/sessionmanager/session.py index 4c1cd3dd..e6fc0769 100644 --- a/src/sessionmanager/session.py +++ b/src/sessionmanager/session.py @@ -74,7 +74,7 @@ class Session(object): 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["twitter"]["ignored_clients"]) == True: + if utils.find_item(i["id"], self.db[name]) == None and utils.is_allowed(i, self.settings, name) == True: try: i = self.check_quoted_status(i) except: pass i = self.check_long_tweet(i) diff --git a/src/twitter/buffers/indibidual.py b/src/twitter/buffers/indibidual.py index e23e5078..b8f130ca 100644 --- a/src/twitter/buffers/indibidual.py +++ b/src/twitter/buffers/indibidual.py @@ -24,7 +24,7 @@ class timelinesStreamer(TwythonStreamer): def check_tls(self, data): for i in self.session.settings["other_buffers"]["timelines"]: if data["user"]["id_str"] == i: - if utils.find_item(data["id"], self.session.db["%s-timeline" % (i,)]) != None: + if utils.find_item(data["id"], self.session.db["%s-timeline" % (i,)]) != None and utils.is_allowed(data, self.session.settings, "%s-timeline" % (i,)): log.error("duplicated tweet. Ignoring it...") return # try: @@ -60,7 +60,7 @@ class timelinesStreamer(TwythonStreamer): self.friends = friends def on_success(self, data): - if "text" in data and utils.is_allowed(data, self.session.settings["twitter"]["ignored_clients"]) == True: + if "text" in data: if data.has_key("extended_tweet"): data["full_text"] = data["extended_tweet"]["full_text"] if data.has_key("retweeted_status"): diff --git a/src/twitter/buffers/stream.py b/src/twitter/buffers/stream.py index f8a624f0..45fd77fa 100644 --- a/src/twitter/buffers/stream.py +++ b/src/twitter/buffers/stream.py @@ -27,7 +27,7 @@ class streamer(TwythonStreamer): def put_data(self, place, data): if self.session.db.has_key(place): - if utils.find_item(data["id"], self.session.db[place]) != None: + if utils.find_item(data["id"], self.session.db[place]) != None and utils.is_allowed(data, self.session.settings, place): log.error("duplicated tweet. Ignoring it...") return False # try: @@ -128,7 +128,7 @@ class streamer(TwythonStreamer): elif "friends" in data: self.friends = data["friends"] pub.sendMessage("friends-receibed") - elif "text" in data and utils.is_allowed(data, self.session.settings["twitter"]["ignored_clients"]) == True: + elif "text" in data: if data.has_key("extended_tweet"): data["full_text"] = data["extended_tweet"]["full_text"] data["entities"] = data["extended_tweet"]["entities"] diff --git a/src/twitter/utils.py b/src/twitter/utils.py index 821fda55..6ee5ab43 100644 --- a/src/twitter/utils.py +++ b/src/twitter/utils.py @@ -128,16 +128,36 @@ def api_call(parent=None, call_name=None, preexec_message="", success="", succes parent.parent.sound.play("error.ogg") return val -def is_allowed(tweet, clients): +def is_allowed(tweet, settings, buffer_name): + clients = settings["twitter"]["ignored_clients"] if tweet.has_key("sender"): return True allowed = True if tweet.has_key("retweeted_status"): tweet = tweet["retweeted_status"] source = re.sub(r"(?s)<.*?>", "", tweet["source"]) for i in clients: if i.lower() == source.lower(): - allowed = False -# log.exception("Tuit not allowed: %r" % (tweet,)) - return allowed + return False + return filter_tweet(tweet, settings, buffer_name) + +def filter_tweet(tweet, settings, buffer_name): + for i in settings["filters"]: + if settings["filters"][i]["in_buffer"] == buffer_name: + result = True + regexp = settings["filters"][i]["regexp"] + word = settings["filters"][i]["word"] + if word != "" and settings["filters"][i]["if_word_exists"]: + if word not in tweet["full_text"]: + return False + elif word != "" and settings["filters"][i]["if_word_exists"] == False: + if word in tweet["full_text"]: + return False + if settings["filters"][i]["in_lang"] == True: + if 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"]: + return False + return True def twitter_error(error): if error.error_code == 403: