First filter implementation. Needs lots of tests. #102

This commit is contained in:
Manuel Cortez 2017-11-13 17:45:01 -06:00
parent c2413489e5
commit 1e22ebce61
4 changed files with 29 additions and 9 deletions

View File

@ -74,7 +74,7 @@ class Session(object):
if i["id"] < last_id: if i["id"] < last_id:
log.error("Ignoring an older tweet... Last id: {0}, tweet id: {1}".format(last_id, i["id"])) log.error("Ignoring an older tweet... Last id: {0}, tweet id: {1}".format(last_id, i["id"]))
continue 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) try: i = self.check_quoted_status(i)
except: pass except: pass
i = self.check_long_tweet(i) i = self.check_long_tweet(i)

View File

@ -24,7 +24,7 @@ class timelinesStreamer(TwythonStreamer):
def check_tls(self, data): def check_tls(self, data):
for i in self.session.settings["other_buffers"]["timelines"]: for i in self.session.settings["other_buffers"]["timelines"]:
if data["user"]["id_str"] == i: 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...") log.error("duplicated tweet. Ignoring it...")
return return
# try: # try:
@ -60,7 +60,7 @@ class timelinesStreamer(TwythonStreamer):
self.friends = friends self.friends = friends
def on_success(self, data): 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"): if data.has_key("extended_tweet"):
data["full_text"] = data["extended_tweet"]["full_text"] data["full_text"] = data["extended_tweet"]["full_text"]
if data.has_key("retweeted_status"): if data.has_key("retweeted_status"):

View File

@ -27,7 +27,7 @@ class streamer(TwythonStreamer):
def put_data(self, place, data): def put_data(self, place, data):
if self.session.db.has_key(place): 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...") log.error("duplicated tweet. Ignoring it...")
return False return False
# try: # try:
@ -128,7 +128,7 @@ class streamer(TwythonStreamer):
elif "friends" in data: elif "friends" in data:
self.friends = data["friends"] self.friends = data["friends"]
pub.sendMessage("friends-receibed") 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"): if data.has_key("extended_tweet"):
data["full_text"] = data["extended_tweet"]["full_text"] data["full_text"] = data["extended_tweet"]["full_text"]
data["entities"] = data["extended_tweet"]["entities"] data["entities"] = data["extended_tweet"]["entities"]

View File

@ -128,16 +128,36 @@ def api_call(parent=None, call_name=None, preexec_message="", success="", succes
parent.parent.sound.play("error.ogg") parent.parent.sound.play("error.ogg")
return val 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 if tweet.has_key("sender"): return True
allowed = True allowed = True
if tweet.has_key("retweeted_status"): tweet = tweet["retweeted_status"] if tweet.has_key("retweeted_status"): tweet = tweet["retweeted_status"]
source = re.sub(r"(?s)<.*?>", "", tweet["source"]) source = re.sub(r"(?s)<.*?>", "", tweet["source"])
for i in clients: for i in clients:
if i.lower() == source.lower(): if i.lower() == source.lower():
allowed = False return False
# log.exception("Tuit not allowed: %r" % (tweet,)) return filter_tweet(tweet, settings, buffer_name)
return allowed
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): def twitter_error(error):
if error.error_code == 403: if error.error_code == 403: