diff options
-rw-r--r-- | docs/index.rst | 1 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 36 |
2 files changed, 37 insertions, 0 deletions
diff --git a/docs/index.rst b/docs/index.rst index e1dc7ac..0cff37e 100644 --- a/docs/index.rst +++ b/docs/index.rst | |||
@@ -703,6 +703,7 @@ These functions allow you to get information about keyword filters. | |||
703 | 703 | ||
704 | .. automethod:: Mastodon.filters | 704 | .. automethod:: Mastodon.filters |
705 | .. automethod:: Mastodon.filter | 705 | .. automethod:: Mastodon.filter |
706 | .. automethod:: Mastodon.filters_apply | ||
706 | 707 | ||
707 | Reading data: Follow suggestions | 708 | Reading data: Follow suggestions |
708 | -------------------------------- | 709 | -------------------------------- |
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 | ### |