diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 97 |
1 files changed, 82 insertions, 15 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 6b3de9a..27acbaa 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -161,8 +161,9 @@ class Mastodon: | |||
161 | __DICT_VERSION_MENTION = "1.0.0" | 161 | __DICT_VERSION_MENTION = "1.0.0" |
162 | __DICT_VERSION_MEDIA = "2.3.0" | 162 | __DICT_VERSION_MEDIA = "2.3.0" |
163 | __DICT_VERSION_ACCOUNT = "2.4.0" | 163 | __DICT_VERSION_ACCOUNT = "2.4.0" |
164 | __DICT_VERSION_STATUS = bigger_version(bigger_version(bigger_version(bigger_version("2.5.0", | 164 | __DICT_VERSION_POLL = "2.8.0" |
165 | __DICT_VERSION_MEDIA), __DICT_VERSION_ACCOUNT), __DICT_VERSION_APPLICATION), __DICT_VERSION_MENTION) | 165 | __DICT_VERSION_STATUS = bigger_version(bigger_version(bigger_version(bigger_version(bigger_version("2.8.0", |
166 | __DICT_VERSION_MEDIA), __DICT_VERSION_ACCOUNT), __DICT_VERSION_APPLICATION), __DICT_VERSION_MENTION), __DICT_VERSION_POLL) | ||
166 | __DICT_VERSION_INSTANCE = bigger_version("2.7.2", __DICT_VERSION_ACCOUNT) | 167 | __DICT_VERSION_INSTANCE = bigger_version("2.7.2", __DICT_VERSION_ACCOUNT) |
167 | __DICT_VERSION_HASHTAG = "2.3.4" | 168 | __DICT_VERSION_HASHTAG = "2.3.4" |
168 | __DICT_VERSION_EMOJI = "2.1.0" | 169 | __DICT_VERSION_EMOJI = "2.1.0" |
@@ -827,6 +828,20 @@ class Mastodon: | |||
827 | return self.__api_request('GET', url) | 828 | return self.__api_request('GET', url) |
828 | 829 | ||
829 | ### | 830 | ### |
831 | # Reading data: Polls | ||
832 | ### | ||
833 | @api_version("2.8.0", "2.8.0", __DICT_VERSION_POLL) | ||
834 | def poll(self, id): | ||
835 | """ | ||
836 | Fetch information about the poll with the given id | ||
837 | |||
838 | Returns a `poll dict`_. | ||
839 | """ | ||
840 | id = self.__unpack_id(id) | ||
841 | url = '/api/v1/polls/{0}'.format(str(id)) | ||
842 | return self.__api_request('GET', url) | ||
843 | |||
844 | ### | ||
830 | # Reading data: Notifications | 845 | # Reading data: Notifications |
831 | ### | 846 | ### |
832 | @api_version("1.0.0", "2.6.0", __DICT_VERSION_NOTIFICATION) | 847 | @api_version("1.0.0", "2.6.0", __DICT_VERSION_NOTIFICATION) |
@@ -1367,11 +1382,11 @@ class Mastodon: | |||
1367 | ### | 1382 | ### |
1368 | # Writing data: Statuses | 1383 | # Writing data: Statuses |
1369 | ### | 1384 | ### |
1370 | @api_version("1.0.0", "2.7.0", __DICT_VERSION_STATUS) | 1385 | @api_version("1.0.0", "2.8.0", __DICT_VERSION_STATUS) |
1371 | def status_post(self, status, in_reply_to_id=None, media_ids=None, | 1386 | def status_post(self, status, in_reply_to_id=None, media_ids=None, |
1372 | sensitive=False, visibility=None, spoiler_text=None, | 1387 | sensitive=False, visibility=None, spoiler_text=None, |
1373 | language=None, idempotency_key=None, content_type=None, | 1388 | language=None, idempotency_key=None, content_type=None, |
1374 | scheduled_at=None): | 1389 | scheduled_at=None, poll=None): |
1375 | """ | 1390 | """ |
1376 | Post a status. Can optionally be in reply to another status and contain | 1391 | Post a status. Can optionally be in reply to another status and contain |
1377 | media. | 1392 | media. |
@@ -1412,6 +1427,9 @@ class Mastodon: | |||
1412 | (the time must be at least 5 minutes into the future). If this is passed, | 1427 | (the time must be at least 5 minutes into the future). If this is passed, |
1413 | status_post returns a `scheduled toot dict`_ instead. | 1428 | status_post returns a `scheduled toot dict`_ instead. |
1414 | 1429 | ||
1430 | Pass `poll` to attach a poll to the status. An appropriate object can be | ||
1431 | constructed using `make_poll()`_ | ||
1432 | |||
1415 | Specify `content_type` to set the content type of your post on Pleroma. | 1433 | Specify `content_type` to set the content type of your post on Pleroma. |
1416 | It accepts 'text/plain' (default), 'text/markdown', and 'text/html'. | 1434 | It accepts 'text/plain' (default), 'text/markdown', and 'text/html'. |
1417 | This parameter is not supported on Mastodon servers, but will be | 1435 | This parameter is not supported on Mastodon servers, but will be |
@@ -1466,10 +1484,14 @@ class Mastodon: | |||
1466 | if params_initial['content_type'] == None: | 1484 | if params_initial['content_type'] == None: |
1467 | del params_initial['content_type'] | 1485 | del params_initial['content_type'] |
1468 | 1486 | ||
1487 | use_json = False | ||
1488 | if not poll is None: | ||
1489 | use_json = True | ||
1490 | |||
1469 | params = self.__generate_params(params_initial, ['idempotency_key']) | 1491 | params = self.__generate_params(params_initial, ['idempotency_key']) |
1470 | return self.__api_request('POST', '/api/v1/statuses', params, headers = headers) | 1492 | return self.__api_request('POST', '/api/v1/statuses', params, headers = headers, use_json = use_json) |
1471 | 1493 | ||
1472 | @api_version("1.0.0", "2.7.0", __DICT_VERSION_STATUS) | 1494 | @api_version("1.0.0", "2.8.0", __DICT_VERSION_STATUS) |
1473 | def toot(self, status): | 1495 | def toot(self, status): |
1474 | """ | 1496 | """ |
1475 | Synonym for `status_post()`_ that only takes the status text as input. | 1497 | Synonym for `status_post()`_ that only takes the status text as input. |
@@ -1480,10 +1502,10 @@ class Mastodon: | |||
1480 | """ | 1502 | """ |
1481 | return self.status_post(status) | 1503 | return self.status_post(status) |
1482 | 1504 | ||
1483 | @api_version("1.0.0", "2.7.0", __DICT_VERSION_STATUS) | 1505 | @api_version("1.0.0", "2.8.0", __DICT_VERSION_STATUS) |
1484 | def status_reply(self, to_status, status, media_ids=None, sensitive=False, visibility=None, | 1506 | def status_reply(self, to_status, status, media_ids=None, sensitive=False, visibility=None, |
1485 | spoiler_text=None, language=None, idempotency_key=None, content_type=None, | 1507 | spoiler_text=None, language=None, idempotency_key=None, content_type=None, |
1486 | scheduled_at=None, untag=False): | 1508 | scheduled_at=None, poll=None, untag=False): |
1487 | """ | 1509 | """ |
1488 | Helper function - acts like status_post, but prepends the name of all | 1510 | Helper function - acts like status_post, but prepends the name of all |
1489 | the users that are being replied to to the status text and retains | 1511 | the users that are being replied to to the status text and retains |
@@ -1516,8 +1538,22 @@ class Mastodon: | |||
1516 | return self.status_post(status, in_reply_to_id = to_status.id, media_ids = media_ids, sensitive = sensitive, | 1538 | return self.status_post(status, in_reply_to_id = to_status.id, media_ids = media_ids, sensitive = sensitive, |
1517 | visibility = visibility, spoiler_text = spoiler_text, language = language, | 1539 | visibility = visibility, spoiler_text = spoiler_text, language = language, |
1518 | idempotency_key = idempotency_key, content_type = content_type, | 1540 | idempotency_key = idempotency_key, content_type = content_type, |
1519 | scheduled_at = scheduled_at) | 1541 | scheduled_at = scheduled_at, poll = poll) |
1520 | 1542 | ||
1543 | @api_version("2.8.0", "2.8.0", __DICT_VERSION_POLL) | ||
1544 | def make_poll(self, options, expires_in, multiple=False, hide_totals=False): | ||
1545 | """ | ||
1546 | Generate a poll object that can be passed as the `poll` option when posting a status. | ||
1547 | |||
1548 | options is an array of strings with the poll options (Maximum, by default: 4), | ||
1549 | expires_in is the time in seconds for which the poll should be open. | ||
1550 | Set multiple to True to allow people to choose more than one answer. Set | ||
1551 | hide_totals to True to hide the results of the poll until it has expired. | ||
1552 | """ | ||
1553 | poll_params = locals() | ||
1554 | del poll_params["self"] | ||
1555 | return poll_params | ||
1556 | |||
1521 | @api_version("1.0.0", "1.0.0", "1.0.0") | 1557 | @api_version("1.0.0", "1.0.0", "1.0.0") |
1522 | def status_delete(self, id): | 1558 | def status_delete(self, id): |
1523 | """ | 1559 | """ |
@@ -1654,6 +1690,34 @@ class Mastodon: | |||
1654 | self.__api_request('DELETE', url) | 1690 | self.__api_request('DELETE', url) |
1655 | 1691 | ||
1656 | ### | 1692 | ### |
1693 | # Writing data: Polls | ||
1694 | ### | ||
1695 | @api_version("2.8.0", "2.8.0", __DICT_VERSION_POLL) | ||
1696 | def poll_vote(self, id, choices): | ||
1697 | """ | ||
1698 | Vote in the given poll. | ||
1699 | |||
1700 | `choices` is the index of the choice you wish to register a vote for | ||
1701 | (i.e. its index in the corresponding polls `options` field. In case | ||
1702 | of a poll that allows selection of more than one option, a list of | ||
1703 | indices can be passed. | ||
1704 | |||
1705 | You can only submit choices for any given poll once in case of | ||
1706 | single-option polls, or only once per option in case of multi-option | ||
1707 | polls. | ||
1708 | |||
1709 | Returns the updated `poll dict`_ | ||
1710 | """ | ||
1711 | id = self.__unpack_id(id) | ||
1712 | if not isinstance(choices, list): | ||
1713 | choices = [choices] | ||
1714 | params = self.__generate_params(locals(), ['id']) | ||
1715 | |||
1716 | url = '/api/v1/polls/{0}/votes'.format(id) | ||
1717 | self.__api_request('POST', url, params) | ||
1718 | |||
1719 | |||
1720 | ### | ||
1657 | # Writing data: Notifications | 1721 | # Writing data: Notifications |
1658 | ### | 1722 | ### |
1659 | @api_version("1.0.0", "1.0.0", "1.0.0") | 1723 | @api_version("1.0.0", "1.0.0", "1.0.0") |
@@ -2480,7 +2544,7 @@ class Mastodon: | |||
2480 | isotime = isotime[:-2] + ":" + isotime[-2:] | 2544 | isotime = isotime[:-2] + ":" + isotime[-2:] |
2481 | return isotime | 2545 | return isotime |
2482 | 2546 | ||
2483 | def __api_request(self, method, endpoint, params={}, files={}, headers={}, access_token_override=None, do_ratelimiting=True): | 2547 | def __api_request(self, method, endpoint, params={}, files={}, headers={}, access_token_override=None, do_ratelimiting=True, use_json = False): |
2484 | """ | 2548 | """ |
2485 | Internal API request helper. | 2549 | Internal API request helper. |
2486 | """ | 2550 | """ |
@@ -2527,11 +2591,14 @@ class Mastodon: | |||
2527 | try: | 2591 | try: |
2528 | kwargs = dict(headers=headers, files=files, | 2592 | kwargs = dict(headers=headers, files=files, |
2529 | timeout=self.request_timeout) | 2593 | timeout=self.request_timeout) |
2530 | if method == 'GET': | 2594 | if use_json == False: |
2531 | kwargs['params'] = params | 2595 | if method == 'GET': |
2596 | kwargs['params'] = params | ||
2597 | else: | ||
2598 | kwargs['data'] = params | ||
2532 | else: | 2599 | else: |
2533 | kwargs['data'] = params | 2600 | kwargs['json'] = params |
2534 | 2601 | ||
2535 | response_object = self.session.request( | 2602 | response_object = self.session.request( |
2536 | method, self.api_base_url + endpoint, **kwargs) | 2603 | method, self.api_base_url + endpoint, **kwargs) |
2537 | except Exception as e: | 2604 | except Exception as e: |