diff --git a/.coverage b/.coverage index 95518a8..dc5cc59 100644 Binary files a/.coverage and b/.coverage differ diff --git a/src/mastodon/application.py b/src/mastodon/application.py index 4ee548e..326ec19 100644 --- a/src/mastodon/application.py +++ b/src/mastodon/application.py @@ -1,5 +1,6 @@ from mastodon.authorization import MastodonAuthorization from mastodon.config import MastodonConfiguration +from mastodon.statuses import Statuses from mastodon.timelines import Timelines @@ -9,3 +10,4 @@ class MastodonApplication(): self.authorization = MastodonAuthorization(self.config) self.timelines = Timelines(self.config, self.authorization) + self.statuses = Statuses(self.config, self.authorization) diff --git a/src/mastodon/statuses.py b/src/mastodon/statuses.py index 2e7c381..2450c64 100644 --- a/src/mastodon/statuses.py +++ b/src/mastodon/statuses.py @@ -1,2 +1,32 @@ +from loguru import logger +import requests + + class Statuses(): - pass + def __init__(self, configuration, authorization): + self.config = configuration + self.auth = authorization + + def favourite(self, status_id): + server = self.config.get_value("server") + url = f"https://{server}/api/v1/statuses/{status_id}/favourite" + + response = requests.post(url, headers=self.auth.get_auth_headers()) + + logger.trace("JSON: {}".format(str(response.content))) + if response.status_code == 200: + return response.json() + else: + raise RuntimeError("Server returned {} {}".format(response.status_code, response.content)) + + def reblog(self, status_id): + server = self.config.get_value("server") + url = f"https://{server}/api/v1/statuses/{status_id}/reblog" + + response = requests.post(url, headers=self.auth.get_auth_headers()) + + logger.trace("JSON: {}".format(str(response.content))) + if response.status_code == 200: + return response.json() + else: + raise RuntimeError("Server returned {} {}".format(response.status_code, response.content)) diff --git a/src/mastodon/timelines.py b/src/mastodon/timelines.py index 7191f13..be64c2d 100644 --- a/src/mastodon/timelines.py +++ b/src/mastodon/timelines.py @@ -24,4 +24,7 @@ class Timelines(): response = requests.get(url, headers=self.auth.get_auth_headers(), params=parameters) logger.trace("JSON: {}".format(str(response.content))) - return response.json() + if response.status_code == 200: + return response.json() + else: + raise RuntimeError("Server returned {} {}".format(response.status_code, response.content)) diff --git a/tests/resources/favourite_response.json b/tests/resources/favourite_response.json new file mode 100644 index 0000000..c4b58b6 --- /dev/null +++ b/tests/resources/favourite_response.json @@ -0,0 +1,142 @@ +{ + "id": "103270115826048975", + "created_at": "2019-12-08T03:48:33.901Z", + "in_reply_to_id": null, + "in_reply_to_account_id": null, + "sensitive": false, + "spoiler_text": "", + "visibility": "public", + "language": "en", + "uri": "https://mastodon.social/users/Gargron/statuses/103270115826048975", + "url": "https://mastodon.social/@Gargron/103270115826048975", + "replies_count": 5, + "reblogs_count": 6, + "favourites_count": 11, + "favourited": true, + "reblogged": false, + "muted": false, + "bookmarked": false, + "content": "

"I lost my inheritance with one wrong digit on my sort code"

https://www.theguardian.com/money/2019/dec/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code", + "reblog": null, + "application": { + "name": "Web", + "website": null + }, + "account": { + "id": "1", + "username": "Gargron", + "acct": "Gargron", + "display_name": "Eugen", + "locked": false, + "bot": false, + "discoverable": true, + "group": false, + "created_at": "2016-03-16T14:34:26.392Z", + "note": "

Developer of Mastodon and administrator of mastodon.social. I post service announcements, development updates, and personal stuff.

", + "url": "https://mastodon.social/@Gargron", + "avatar": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg", + "avatar_static": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg", + "header": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png", + "header_static": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png", + "followers_count": 322930, + "following_count": 459, + "statuses_count": 61323, + "last_status_at": "2019-12-10T08:14:44.811Z", + "emojis": [], + "fields": [ + { + "name": "Patreon", + "value": "https://www.patreon.com/mastodonhttps://zeonfederated.com"I lost my inheritance with one wrong digit on my sort code"

https://www.theguardian.com/money/2019/dec/07/i-lost-my-193000-inheritance-with-one-wrong-digit-on-my-sort-code", + "reblog": null, + "application": { + "name": "Web", + "website": null + }, + "account": { + "id": "1", + "username": "Gargron", + "acct": "Gargron", + "display_name": "Eugen", + "locked": false, + "bot": false, + "discoverable": true, + "group": false, + "created_at": "2016-03-16T14:34:26.392Z", + "note": "

Developer of Mastodon and administrator of mastodon.social. I post service announcements, development updates, and personal stuff.

", + "url": "https://mastodon.social/@Gargron", + "avatar": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg", + "avatar_static": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg", + "header": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png", + "header_static": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png", + "followers_count": 322930, + "following_count": 459, + "statuses_count": 61323, + "last_status_at": "2019-12-10T08:14:44.811Z", + "emojis": [], + "fields": [ + { + "name": "Patreon", + "value": "https://www.patreon.com/mastodonhttps://zeonfederated.com