diff options
author | halcy <halcy@ARARAGI-KUN> | 2022-11-30 23:47:46 +0200 |
---|---|---|
committer | halcy <halcy@ARARAGI-KUN> | 2022-11-30 23:47:46 +0200 |
commit | d9cd7547fd0919a46b79ede084910b7df66f8fd7 (patch) | |
tree | d6679e8cd0ad6f3489b0ec5f55fd579de45290e1 /mastodon/instance.py | |
parent | 48f1d31c72de4ce93f19e1235ca06c2b8f9058fa (diff) | |
download | mastodon.py-d9cd7547fd0919a46b79ede084910b7df66f8fd7.tar.gz |
Move some more methods, fix crypto heckup
Diffstat (limited to 'mastodon/instance.py')
-rw-r--r-- | mastodon/instance.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/mastodon/instance.py b/mastodon/instance.py new file mode 100644 index 0000000..88445d1 --- /dev/null +++ b/mastodon/instance.py | |||
@@ -0,0 +1,96 @@ | |||
1 | from .versions import _DICT_VERSION_INSTANCE, _DICT_VERSION_ACTIVITY | ||
2 | from .error import MastodonIllegalArgumentError, MastodonNotFoundError | ||
3 | from .utility import api_version | ||
4 | from .compat import urlparse | ||
5 | |||
6 | from .internals import Mastodon as Internals | ||
7 | |||
8 | class Mastodon(Internals): | ||
9 | ### | ||
10 | # Reading data: Instances | ||
11 | ### | ||
12 | @api_version("1.1.0", "2.3.0", _DICT_VERSION_INSTANCE) | ||
13 | def instance(self): | ||
14 | """ | ||
15 | Retrieve basic information about the instance, including the URI and administrative contact email. | ||
16 | |||
17 | Does not require authentication unless locked down by the administrator. | ||
18 | |||
19 | Returns an :ref:`instance dict <instance dict>`. | ||
20 | """ | ||
21 | return self.__instance() | ||
22 | |||
23 | def __instance(self): | ||
24 | """ | ||
25 | Internal, non-version-checking helper that does the same as instance() | ||
26 | """ | ||
27 | instance = self.__api_request('GET', '/api/v1/instance/') | ||
28 | return instance | ||
29 | |||
30 | @api_version("2.1.2", "2.1.2", _DICT_VERSION_ACTIVITY) | ||
31 | def instance_activity(self): | ||
32 | """ | ||
33 | Retrieve activity stats about the instance. May be disabled by the instance administrator - throws | ||
34 | a MastodonNotFoundError in that case. | ||
35 | |||
36 | Activity is returned for 12 weeks going back from the current week. | ||
37 | |||
38 | Returns a list of :ref:`activity dicts <activity dicts>`. | ||
39 | """ | ||
40 | return self.__api_request('GET', '/api/v1/instance/activity') | ||
41 | |||
42 | @api_version("2.1.2", "2.1.2", "2.1.2") | ||
43 | def instance_peers(self): | ||
44 | """ | ||
45 | Retrieve the instances that this instance knows about. May be disabled by the instance administrator - throws | ||
46 | a MastodonNotFoundError in that case. | ||
47 | |||
48 | Returns a list of URL strings. | ||
49 | """ | ||
50 | return self.__api_request('GET', '/api/v1/instance/peers') | ||
51 | |||
52 | @api_version("3.0.0", "3.0.0", "3.0.0") | ||
53 | def instance_health(self): | ||
54 | """ | ||
55 | Basic health check. Returns True if healthy, False if not. | ||
56 | """ | ||
57 | status = self.__api_request('GET', '/health', parse=False).decode("utf-8") | ||
58 | return status in ["OK", "success"] | ||
59 | |||
60 | @api_version("3.0.0", "3.0.0", "3.0.0") | ||
61 | def instance_nodeinfo(self, schema="http://nodeinfo.diaspora.software/ns/schema/2.0"): | ||
62 | """ | ||
63 | Retrieves the instance's nodeinfo information. | ||
64 | |||
65 | For information on what the nodeinfo can contain, see the nodeinfo | ||
66 | specification: https://github.com/jhass/nodeinfo . By default, | ||
67 | Mastodon.py will try to retrieve the version 2.0 schema nodeinfo. | ||
68 | |||
69 | To override the schema, specify the desired schema with the `schema` | ||
70 | parameter. | ||
71 | """ | ||
72 | links = self.__api_request('GET', '/.well-known/nodeinfo')["links"] | ||
73 | |||
74 | schema_url = None | ||
75 | for available_schema in links: | ||
76 | if available_schema.rel == schema: | ||
77 | schema_url = available_schema.href | ||
78 | |||
79 | if schema_url is None: | ||
80 | raise MastodonIllegalArgumentError( | ||
81 | "Requested nodeinfo schema is not available.") | ||
82 | |||
83 | try: | ||
84 | return self.__api_request('GET', schema_url, base_url_override="") | ||
85 | except MastodonNotFoundError: | ||
86 | parse = urlparse(schema_url) | ||
87 | return self.__api_request('GET', parse.path + parse.params + parse.query + parse.fragment) | ||
88 | |||
89 | @api_version("3.4.0", "3.4.0", _DICT_VERSION_INSTANCE) | ||
90 | def instance_rules(self): | ||
91 | """ | ||
92 | Retrieve instance rules. | ||
93 | |||
94 | Returns a list of `id` + `text` dicts, same as the `rules` field in the :ref:`instance dicts <instance dicts>`. | ||
95 | """ | ||
96 | return self.__api_request('GET', '/api/v1/instance/rules') | ||