aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2018-07-30 17:29:37 +0200
committerLorenz Diener <[email protected]>2018-07-30 17:29:37 +0200
commitde0d5df86152ab5f4a748fecd0d2089e701820cd (patch)
tree50830eb14f39f8c690d8181dab315db283a2d8a4
parentcf2d0ebc8246cea8c5e1dbb48687b625affe53c8 (diff)
downloadmastodon.py-de0d5df86152ab5f4a748fecd0d2089e701820cd.tar.gz
Add keyword filters
-rw-r--r--docs/index.rst34
-rw-r--r--mastodon/Mastodon.py80
2 files changed, 112 insertions, 2 deletions
diff --git a/docs/index.rst b/docs/index.rst
index 6cb782e..3921356 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -362,6 +362,25 @@ Relationship dicts
362 # logged-in users Timeline 362 # logged-in users Timeline
363 } 363 }
364 364
365Filter dicts
366~~~~~~~~~~~~
367.. _filter dict:
368
369.. code-block:: python
370
371 mastodon.account_follow(<numerical id>)
372 # Returns the following dictionary:
373 {
374 'id': # Numerical id of the filter
375 'phrase': # Filtered keyword or phrase
376 'context': # List of places where the filters are applied ('home', 'notifications', 'public', 'thread')
377 'expires_at': # Expiry date for the filter
378 'irreversible': # Boolean denoting if this filter is executed server-side
379 # or if it should be ran client-side (Note that Mastodon.py does
380 # not run client-side filters for you).
381 'whole_word': # Boolean denoting whether this filter can match partial words
382 }
383
365Notification dicts 384Notification dicts
366~~~~~~~~~~~~~~~~~~ 385~~~~~~~~~~~~~~~~~~
367.. _notification dict: 386.. _notification dict:
@@ -677,6 +696,13 @@ their relationships.
677.. automethod:: Mastodon.account_relationships 696.. automethod:: Mastodon.account_relationships
678.. automethod:: Mastodon.account_search 697.. automethod:: Mastodon.account_search
679 698
699Writing data: Keyword filters
700-----------------------------
701These functions allow you to get information about keyword filters.
702
703.. automethod:: Mastodon.filters
704.. automethod:: Mastodon.filter
705
680Reading data: Follow suggestions 706Reading data: Follow suggestions
681-------------------------------- 707--------------------------------
682 708
@@ -779,6 +805,14 @@ These functions allow you to interact with other accounts: To (un)follow and
779.. automethod:: Mastodon.account_unmute 805.. automethod:: Mastodon.account_unmute
780.. automethod:: Mastodon.account_update_credentials 806.. automethod:: Mastodon.account_update_credentials
781 807
808Writing data: Keyword filters
809-----------------------------
810These functions allow you to manipulate keyword filters.
811
812.. automethod:: Mastodon.filter_create
813.. automethod:: Mastodon.filter_update
814.. automethod:: Mastodon.filter_delete
815
782Writing data: Follow suggestions 816Writing data: Follow suggestions
783-------------------------------- 817--------------------------------
784 818
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 5e73622..0020710 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -173,6 +173,7 @@ class Mastodon:
173 __DICT_VERSION_REPORT = "1.1.0" 173 __DICT_VERSION_REPORT = "1.1.0"
174 __DICT_VERSION_PUSH = "2.4.0" 174 __DICT_VERSION_PUSH = "2.4.0"
175 __DICT_VERSION_PUSH_NOTIF = "2.4.0" 175 __DICT_VERSION_PUSH_NOTIF = "2.4.0"
176 __DICT_VERSION_FILTER = "2.4.3"
176 177
177 ### 178 ###
178 # Registering apps 179 # Registering apps
@@ -833,6 +834,29 @@ class Mastodon:
833 return self.__api_request('GET', url, params) 834 return self.__api_request('GET', url, params)
834 835
835 ### 836 ###
837 # Reading data: Keyword filters
838 ###
839 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
840 def filters():
841 """
842 Fetch all of the logged-in users filters.
843
844 Returns a list of `filter dicts`_. Not paginated.
845 """
846 return self.__api_request('GET', '/api/v1/filters')
847
848 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
849 def filter(id):
850 """
851 Fetches information about the filter with the specified `id`.
852
853 Returns a `filter dict`_.
854 """
855 id = self.__unpack_id(id)
856 url = '/api/v1/filters/{0}'.format(str(id))
857 return self.__api_request('GET', url)
858
859 ###
836 # Reading data: Follow suggestions 860 # Reading data: Follow suggestions
837 ### 861 ###
838 @api_version("2.4.3", "2.4.3", __DICT_VERSION_ACCOUNT) 862 @api_version("2.4.3", "2.4.3", __DICT_VERSION_ACCOUNT)
@@ -1470,12 +1494,64 @@ class Mastodon:
1470 1494
1471 1495
1472 ### 1496 ###
1497 # Writing data: Keyword filters
1498 ###
1499 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
1500 def filter_create(phrase, context, irreversible = True, whole_word = True, expires_in = None):
1501 """
1502 Creates a new keyword filter. `phrase` is the phrase that should be
1503 filtered out, `context` specifies from where to filter the keywords.
1504 Valid contexts are 'home', 'notifications', 'public' and 'thread'.
1505
1506 Set `irreversible` to False if you want the filter to merely be applied
1507 at client side. Note that Mastodon.py doesn't do any client-side
1508 filtering for you.
1509
1510 Set `whole_word` to False if you want to allow filter matches to
1511 start or end within a word, not only at word boundaries.
1512
1513 Set `expires_in` to specify for how many seconds the filter should be
1514 kept around.
1515
1516 Returns the `filter dict`_ of the newly created filter.
1517 """
1518 params = self.__generate_params(locals())
1519
1520 for context_val in context:
1521 if not context_val in ['home', 'notifications', 'public', 'thread']:
1522 raise MastodonIllegalArgumentError('Invalid filter context.')
1523
1524 return self.__api_request('POST', '/api/v1/filters', params)
1525
1526 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
1527 def filter_update(id, phrase = None, context = None, irreversible = None, whole_word = None, expires_in = None):
1528 """
1529 Updates the filter with the given `id`. Parameters are the same
1530 as in `filter_create()`.
1531
1532 Returns the `filter dict`_ of the updated filter.
1533 """
1534 id = self.__unpack_id(id)
1535 params = self.__generate_params(locals(), ['id'])
1536 url = '/api/v1/filters/{0}'.format(str(id))
1537 return self.__api_request('PUT', url, params)
1538
1539 @api_version("2.4.3", "2.4.3", "2.4.3")
1540 def filter_delete(id):
1541 """
1542 Deletes the filter with the given `id`.
1543 """
1544 id = self.__unpack_id(id)
1545 url = '/api/v1/filters/{0}'.format(str(id))
1546 self.__api_request('DELETE', url)
1547
1548 ###
1473 # Writing data: Follow suggestions 1549 # Writing data: Follow suggestions
1474 ### 1550 ###
1475 @api_version("2.4.3", "2.4.3", __DICT_VERSION_ACCOUNT) 1551 @api_version("2.4.3", "2.4.3", __DICT_VERSION_ACCOUNT)
1476 def suggestion_delete(self, account_id): 1552 def suggestion_delete(self, account_id):
1477 """ 1553 """
1478 Remove a single user from the follow suggestions. 1554 Remove the user with the given `account_id` from the follow suggestions.
1479 """ 1555 """
1480 account_id = self.__unpack_id(account_id) 1556 account_id = self.__unpack_id(account_id)
1481 url = '/api/v1/suggestions/{0}'.format(str(account_id)) 1557 url = '/api/v1/suggestions/{0}'.format(str(account_id))
@@ -1947,7 +2023,7 @@ class Mastodon:
1947 """ 2023 """
1948 Parse dates in certain known json fields, if possible. 2024 Parse dates in certain known json fields, if possible.
1949 """ 2025 """
1950 known_date_fields = ["created_at", "week", "day"] 2026 known_date_fields = ["created_at", "week", "day", "expires_at"]
1951 for k, v in json_object.items(): 2027 for k, v in json_object.items():
1952 if k in known_date_fields: 2028 if k in known_date_fields:
1953 try: 2029 try:
Powered by cgit v1.2.3 (git 2.41.0)