From 569a13ee80304f4d9ef94cc4a116db78a9cb7583 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Mon, 29 Jan 2018 14:14:58 +0100 Subject: Add 2.1.2 functions --- docs/index.rst | 19 ++++++++++++++++++- mastodon/Mastodon.py | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 4e79032..2a64a02 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -454,6 +454,21 @@ Instance dicts # stream websocket address. } +Activity dicts +~~~~~~~~~~~~~~ +.. _activity dict: + +.. code-block:: python + + mastodon.instance_activity()[0] + # Returns the following dictionary + { + 'week': # Date of the first day of the week the stats were collected for + 'logins': # Number of users that logged in that week + 'registrations': # Number of new users that week + 'statuses': # Number of statuses posted that week + } + Report dicts ~~~~~~~~~~~~ .. _report dict: @@ -517,10 +532,12 @@ version or explicitly determine if a specific minimum Version is available. Reading data: Instances ----------------------- -This function allows you to fetch information associated with the +These functions allow you to fetch information associated with the current instance. .. automethod:: Mastodon.instance +.. automethod:: Mastodon.instance_activity +.. automethod:: Mastodon.instance_peers Reading data: Timelines ----------------------- diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 0329c10..80960b9 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -89,7 +89,7 @@ class Mastodon: """ __DEFAULT_BASE_URL = 'https://mastodon.social' __DEFAULT_TIMEOUT = 300 - __SUPPORTED_MASTODON_VERSION = "2.1.0" + __SUPPORTED_MASTODON_VERSION = "2.1.2" ### # Registering apps @@ -360,7 +360,29 @@ class Mastodon: """ Internal, non-version-checking helper that does the same as instance() """ - return self.__api_request('GET', '/api/v1/instance/') + return self.__api_request('GET', '/api/v1/instance') + + @api_version("2.1.2", "2.1.2") + def instance_activity(self): + """ + Retrieve activity stats about the instance. May be disabled by the instance administrator - throws + a MastodonNotFoundError in that case. + + Activity is returned for 12 weeks going back from the current week. + + Returns a list `activity dicts`_. + """ + return self.__api_request('GET', '/api/v1/instance/activity') + + @api_version("2.1.2", "2.1.2") + def instance_peers(self): + """ + Retrieve the instances that this instance knows about. May be disabled by the instance administrator - throws + a MastodonNotFoundError in that case. + + Returns a list of URL strings. + """ + return self.__api_request('GET', '/api/v1/instance/peers') ### # Reading data: Timelines @@ -1434,7 +1456,7 @@ class Mastodon: """ Parse dates in certain known json fields, if possible. """ - known_date_fields = ["created_at"] + known_date_fields = ["created_at", "week"] for k, v in json_object.items(): if k in known_date_fields: try: @@ -1447,13 +1469,12 @@ class Mastodon: return json_object @staticmethod - def __json_id_to_bignum(json_object): + def __json_strnum_to_bignum(json_object): """ - Converts json string IDs to native python bignums. + Converts json string numerals to native python bignums. """ - for key in ('id', 'in_reply_to_id', 'in_reply_to_account_id'): - if (key in json_object and - isinstance(json_object[key], six.text_type)): + for key in ('id', 'week', 'in_reply_to_id', 'in_reply_to_account_id', 'logins', 'registrations', 'statuses'): + if (key in json_object and isinstance(json_object[key], six.text_type)): try: json_object[key] = int(json_object[key]) except ValueError: @@ -1463,8 +1484,8 @@ class Mastodon: @staticmethod def __json_hooks(json_object): + json_object = Mastodon.__json_strnum_to_bignum(json_object) json_object = Mastodon.__json_date_parse(json_object) - json_object = Mastodon.__json_id_to_bignum(json_object) json_object = Mastodon.__json_allow_dict_attrs(json_object) return json_object -- cgit v1.2.3