aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2018-07-30 23:08:30 +0200
committerLorenz Diener <[email protected]>2018-07-30 23:08:30 +0200
commit2d81e9b2cbd3e3c1abd3a472220c667e89b29f5e (patch)
treed8aceee169716fe834edc379fbee5cef86823976 /mastodon
parentd04b3da7d5a8f130ad723e5f8797f314cdc072ca (diff)
downloadmastodon.py-2d81e9b2cbd3e3c1abd3a472220c667e89b29f5e.tar.gz
Add filter tests
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py38
1 files changed, 22 insertions, 16 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 4173c8c..e6d8268 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -837,7 +837,7 @@ class Mastodon:
837 # Reading data: Keyword filters 837 # Reading data: Keyword filters
838 ### 838 ###
839 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER) 839 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
840 def filters(): 840 def filters(self):
841 """ 841 """
842 Fetch all of the logged-in users filters. 842 Fetch all of the logged-in users filters.
843 843
@@ -846,7 +846,7 @@ class Mastodon:
846 return self.__api_request('GET', '/api/v1/filters') 846 return self.__api_request('GET', '/api/v1/filters')
847 847
848 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER) 848 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
849 def filter(id): 849 def filter(self, id):
850 """ 850 """
851 Fetches information about the filter with the specified `id`. 851 Fetches information about the filter with the specified `id`.
852 852
@@ -876,8 +876,7 @@ class Mastodon:
876 if keyword_filter["whole_word"] == True: 876 if keyword_filter["whole_word"] == True:
877 filter_string = "\\b" + filter_string + "\\b" 877 filter_string = "\\b" + filter_string + "\\b"
878 filter_strings.append(filter_string) 878 filter_strings.append(filter_string)
879 filter_re = re.compile("|".join(filter_strings)) 879 filter_re = re.compile("|".join(filter_strings), flags = re.IGNORECASE)
880 return filter_re
881 880
882 # Apply 881 # Apply
883 filter_results = [] 882 filter_results = []
@@ -888,7 +887,7 @@ class Mastodon:
888 filter_text = filter_status["content"] 887 filter_text = filter_status["content"]
889 filter_text = re.sub("<.*?>", " ", filter_text) 888 filter_text = re.sub("<.*?>", " ", filter_text)
890 filter_text = re.sub('\s+', ' ', filter_text).strip() 889 filter_text = re.sub('\s+', ' ', filter_text).strip()
891 if not filter_re.match(filter_text): 890 if not filter_re.search(filter_text):
892 filter_results.append(filter_object) 891 filter_results.append(filter_object)
893 return filter_results 892 return filter_results
894 893
@@ -1539,7 +1538,7 @@ class Mastodon:
1539 # Writing data: Keyword filters 1538 # Writing data: Keyword filters
1540 ### 1539 ###
1541 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER) 1540 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
1542 def filter_create(phrase, context, irreversible = False, whole_word = True, expires_in = None): 1541 def filter_create(self, phrase, context, irreversible = False, whole_word = True, expires_in = None):
1543 """ 1542 """
1544 Creates a new keyword filter. `phrase` is the phrase that should be 1543 Creates a new keyword filter. `phrase` is the phrase that should be
1545 filtered out, `context` specifies from where to filter the keywords. 1544 filtered out, `context` specifies from where to filter the keywords.
@@ -1565,7 +1564,7 @@ class Mastodon:
1565 return self.__api_request('POST', '/api/v1/filters', params) 1564 return self.__api_request('POST', '/api/v1/filters', params)
1566 1565
1567 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER) 1566 @api_version("2.4.3", "2.4.3", __DICT_VERSION_FILTER)
1568 def filter_update(id, phrase = None, context = None, irreversible = None, whole_word = None, expires_in = None): 1567 def filter_update(self, id, phrase = None, context = None, irreversible = None, whole_word = None, expires_in = None):
1569 """ 1568 """
1570 Updates the filter with the given `id`. Parameters are the same 1569 Updates the filter with the given `id`. Parameters are the same
1571 as in `filter_create()`. 1570 as in `filter_create()`.
@@ -1578,7 +1577,7 @@ class Mastodon:
1578 return self.__api_request('PUT', url, params) 1577 return self.__api_request('PUT', url, params)
1579 1578
1580 @api_version("2.4.3", "2.4.3", "2.4.3") 1579 @api_version("2.4.3", "2.4.3", "2.4.3")
1581 def filter_delete(id): 1580 def filter_delete(self, id):
1582 """ 1581 """
1583 Deletes the filter with the given `id`. 1582 Deletes the filter with the given `id`.
1584 """ 1583 """
@@ -2067,13 +2066,14 @@ class Mastodon:
2067 known_date_fields = ["created_at", "week", "day", "expires_at"] 2066 known_date_fields = ["created_at", "week", "day", "expires_at"]
2068 for k, v in json_object.items(): 2067 for k, v in json_object.items():
2069 if k in known_date_fields: 2068 if k in known_date_fields:
2070 try: 2069 if v != None:
2071 if isinstance(v, int): 2070 try:
2072 json_object[k] = datetime.datetime.fromtimestamp(v, pytz.utc) 2071 if isinstance(v, int):
2073 else: 2072 json_object[k] = datetime.datetime.fromtimestamp(v, pytz.utc)
2074 json_object[k] = dateutil.parser.parse(v) 2073 else:
2075 except: 2074 json_object[k] = dateutil.parser.parse(v)
2076 raise MastodonAPIError('Encountered invalid date.') 2075 except:
2076 raise MastodonAPIError('Encountered invalid date.')
2077 return json_object 2077 return json_object
2078 2078
2079 @staticmethod 2079 @staticmethod
@@ -2406,6 +2406,12 @@ class Mastodon:
2406 del params['self'] 2406 del params['self']
2407 param_keys = list(params.keys()) 2407 param_keys = list(params.keys())
2408 for key in param_keys: 2408 for key in param_keys:
2409 if isinstance(params[key], bool) and params[key] == False:
2410 params[key] = '0'
2411 if isinstance(params[key], bool) and params[key] == True:
2412 params[key] = '1'
2413
2414 for key in param_keys:
2409 if params[key] is None or key in exclude: 2415 if params[key] is None or key in exclude:
2410 del params[key] 2416 del params[key]
2411 2417
@@ -2414,7 +2420,7 @@ class Mastodon:
2414 if isinstance(params[key], list): 2420 if isinstance(params[key], list):
2415 params[key + "[]"] = params[key] 2421 params[key + "[]"] = params[key]
2416 del params[key] 2422 del params[key]
2417 2423
2418 return params 2424 return params
2419 2425
2420 def __unpack_id(self, id): 2426 def __unpack_id(self, id):
Powered by cgit v1.2.3 (git 2.41.0)