aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon/filters.py')
-rw-r--r--mastodon/filters.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/mastodon/filters.py b/mastodon/filters.py
new file mode 100644
index 0000000..5f373d4
--- /dev/null
+++ b/mastodon/filters.py
@@ -0,0 +1,119 @@
1# filters.py - Filter-related endpoints
2
3import re
4
5from .versions import _DICT_VERSION_FILTER
6from .errors import MastodonIllegalArgumentError
7from .utility import api_version
8
9from .internals import Mastodon as Internals
10
11class Mastodon(Internals):
12 ###
13 # Reading data: Keyword filters
14 ###
15 @api_version("2.4.3", "2.4.3", _DICT_VERSION_FILTER)
16 def filters(self):
17 """
18 Fetch all of the logged-in user's filters.
19
20 Returns a list of :ref:`filter dicts <filter dicts>`. Not paginated.
21 """
22 return self.__api_request('GET', '/api/v1/filters')
23
24 @api_version("2.4.3", "2.4.3", _DICT_VERSION_FILTER)
25 def filter(self, id):
26 """
27 Fetches information about the filter with the specified `id`.
28
29 Returns a :ref:`filter dict <filter dict>`.
30 """
31 id = self.__unpack_id(id)
32 url = '/api/v1/filters/{0}'.format(str(id))
33 return self.__api_request('GET', url)
34
35 @api_version("2.4.3", "2.4.3", _DICT_VERSION_FILTER)
36 def filters_apply(self, objects, filters, context):
37 """
38 Helper function: Applies a list of filters to a list of either statuses
39 or notifications and returns only those matched by none. This function will
40 apply all filters that match the context provided in `context`, i.e.
41 if you want to apply only notification-relevant filters, specify
42 'notifications'. Valid contexts are 'home', 'notifications', 'public' and 'thread'.
43 """
44
45 # Build filter regex
46 filter_strings = []
47 for keyword_filter in filters:
48 if not context in keyword_filter["context"]:
49 continue
50
51 filter_string = re.escape(keyword_filter["phrase"])
52 if keyword_filter["whole_word"]:
53 filter_string = "\\b" + filter_string + "\\b"
54 filter_strings.append(filter_string)
55 filter_re = re.compile("|".join(filter_strings), flags=re.IGNORECASE)
56
57 # Apply
58 filter_results = []
59 for filter_object in objects:
60 filter_status = filter_object
61 if "status" in filter_object:
62 filter_status = filter_object["status"]
63 filter_text = filter_status["content"]
64 filter_text = re.sub(r"<.*?>", " ", filter_text)
65 filter_text = re.sub(r"\s+", " ", filter_text).strip()
66 if not filter_re.search(filter_text):
67 filter_results.append(filter_object)
68 return filter_results
69
70 ###
71 # Writing data: Keyword filters
72 ###
73 @api_version("2.4.3", "2.4.3", _DICT_VERSION_FILTER)
74 def filter_create(self, phrase, context, irreversible=False, whole_word=True, expires_in=None):
75 """
76 Creates a new keyword filter. `phrase` is the phrase that should be
77 filtered out, `context` specifies from where to filter the keywords.
78 Valid contexts are 'home', 'notifications', 'public' and 'thread'.
79
80 Set `irreversible` to True if you want the filter to just delete statuses
81 server side. This works only for the 'home' and 'notifications' contexts.
82
83 Set `whole_word` to False if you want to allow filter matches to
84 start or end within a word, not only at word boundaries.
85
86 Set `expires_in` to specify for how many seconds the filter should be
87 kept around.
88
89 Returns the :ref:`filter dict <filter dict>` of the newly created filter.
90 """
91 params = self.__generate_params(locals())
92
93 for context_val in context:
94 if not context_val in ['home', 'notifications', 'public', 'thread']:
95 raise MastodonIllegalArgumentError('Invalid filter context.')
96
97 return self.__api_request('POST', '/api/v1/filters', params)
98
99 @api_version("2.4.3", "2.4.3", _DICT_VERSION_FILTER)
100 def filter_update(self, id, phrase=None, context=None, irreversible=None, whole_word=None, expires_in=None):
101 """
102 Updates the filter with the given `id`. Parameters are the same
103 as in `filter_create()`.
104
105 Returns the :ref:`filter dict <filter dict>` of the updated filter.
106 """
107 id = self.__unpack_id(id)
108 params = self.__generate_params(locals(), ['id'])
109 url = '/api/v1/filters/{0}'.format(str(id))
110 return self.__api_request('PUT', url, params)
111
112 @api_version("2.4.3", "2.4.3", "2.4.3")
113 def filter_delete(self, id):
114 """
115 Deletes the filter with the given `id`.
116 """
117 id = self.__unpack_id(id)
118 url = '/api/v1/filters/{0}'.format(str(id))
119 self.__api_request('DELETE', url) \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)