aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2018-07-30 22:09:14 +0200
committerLorenz Diener <[email protected]>2018-07-30 22:09:14 +0200
commitd04b3da7d5a8f130ad723e5f8797f314cdc072ca (patch)
tree410bff0173f68259dca7ab2bd1ef6c030c5dd985 /mastodon
parentba3c2a8605806916bf4124daa1afcea4d3a7c9b9 (diff)
downloadmastodon.py-d04b3da7d5a8f130ad723e5f8797f314cdc072ca.tar.gz
Add filter applies function
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index fc5c178..4173c8c 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -855,7 +855,43 @@ class Mastodon:
855 id = self.__unpack_id(id) 855 id = self.__unpack_id(id)
856 url = '/api/v1/filters/{0}'.format(str(id)) 856 url = '/api/v1/filters/{0}'.format(str(id))
857 return self.__api_request('GET', url) 857 return self.__api_request('GET', url)
858
859 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
860 def filters_apply(self, objects, filters, context):
861 """
862 Helper function: Applies a list of filters to a list of either statuses
863 or notifications and returns only those matched by none. This function will
864 apply all filters that match the context provided in `context`, i.e.
865 if you want to apply only notification-relevant filters, specify
866 'notifications'. Valid contexts are 'home', 'notifications', 'public' and 'thread'.
867 """
858 868
869 # Build filter regex
870 filter_strings = []
871 for keyword_filter in filters:
872 if not context in keyword_filter["context"]:
873 continue
874
875 filter_string = re.escape(keyword_filter["phrase"])
876 if keyword_filter["whole_word"] == True:
877 filter_string = "\\b" + filter_string + "\\b"
878 filter_strings.append(filter_string)
879 filter_re = re.compile("|".join(filter_strings))
880 return filter_re
881
882 # Apply
883 filter_results = []
884 for filter_object in objects:
885 filter_status = filter_object
886 if "status" in filter_object:
887 filter_status = filter_object["status"]
888 filter_text = filter_status["content"]
889 filter_text = re.sub("<.*?>", " ", filter_text)
890 filter_text = re.sub('\s+', ' ', filter_text).strip()
891 if not filter_re.match(filter_text):
892 filter_results.append(filter_object)
893 return filter_results
894
859 ### 895 ###
860 # Reading data: Follow suggestions 896 # Reading data: Follow suggestions
861 ### 897 ###
Powered by cgit v1.2.3 (git 2.41.0)