From 10f19b075b6c5c874e352178ca6be7cc84ee9def Mon Sep 17 00:00:00 2001 From: jfm Date: Thu, 2 May 2024 20:23:06 +0200 Subject: [PATCH] Added support for favouriting and boosting --- .coverage | Bin 53248 -> 53248 bytes src/mastodon/application.py | 2 + src/mastodon/statuses.py | 32 +++++- src/mastodon/timelines.py | 5 +- tests/resources/favourite_response.json | 142 ++++++++++++++++++++++++ tests/resources/reblog_response.json | 142 ++++++++++++++++++++++++ tests/test_statuses.py | 42 +++++++ 7 files changed, 363 insertions(+), 2 deletions(-) create mode 100644 tests/resources/favourite_response.json create mode 100644 tests/resources/reblog_response.json create mode 100644 tests/test_statuses.py diff --git a/.coverage b/.coverage index 95518a879450960e0ce9ffb249389cfbb54634ee..dc5cc59567c16f8d2dc80dbd0ca3747b53c54b91 100644 GIT binary patch delta 801 zcmYjPOGs2v7{1s0zUT4ZdkYbwhgRaIum|}V6|E!}=?Y~!thO;>T|NS5T`M!@cn^k7B%8J@r@AEqLhrMSHSUugNujl|hPri~RGLUnfCZaWYij)y;u?uc0In+Ox9PS^# zI64(^6)7E}n_?M);` zCWceE`xX_KIy*FiV=EkIJ5S2CSnq6CEk&9PUbTlt`uYcv+hB^8SfgejS2 zYtcof>xxsNE}|Vu4#ctCPOL)4B$5)$K!hLgRhFj9be|?^HTlLG_!O^)b@rEiATLQ0 zlV~O)-`%WwO-fp9#m@>FC8f*VHWi(3#jl}jLvyaHb4J)@qoxMM8BO$#^`U5fQ$^!Q zCjU({Q5)~j=nWM;Y zF?07Slq8@Zim%P>^i)*Vco31`0CwR!e1>hqmY1Zh(QF+17uuJ zm92ALVb$M1I$wy3P=#Aqm46zB?qI{6%LD=t4)zLr1;ympcs%bLHp8l%Ew1_gX`KdC znzu=Sk-{(E&**l5*|K5b?O69l4Kpar_1~6(#c{`2eCwaG^q|)B+UA7VUZ^TttP{Hn z<%$RM!gx}t!XubRwReCAJlXTY%XM>4pO2kE){-zbk+lp<$eIRQ!LQ8}{K-tLe7yY+ DD1OuiFJdhogr+IPk7A)o6nfN1LfU{*+tQ|_LDDn_ z!CPwQq=(vzpa&rkPzxsY>_NeUXD`V?OIr^HM9BI!G$?a;@BhKf{MNR}+7_v5(U8lU z3e=$pVew166L}F4ea?Gl#kt78^Ot;v53**h->owmafworYT|-?Hp%rUXiAx)JS*2@ zN9C!QsS3Gs^ITV@VitA&%TG{_iuKw&oyp8E&*g>|3OAVi8JnOs##T2@#ZRi1T#b)w zCQ6uyVju^5N4^thx(AoZ4<8s3KNNfmIk4ZL#gt`44B9 zzv5XQatdMv8e&Wk)5q97WyoML>6rR8%cnEWlg(>7Y52b!rQ>~EO z+4c?&&o>>=5^#wLoI%mv@9cMo>7Y2`v^>M+xCQuT-SbZC%&)SRZTp_x`R#cn-SRz4 zHB6kYnO!gpe=!<=EFG6)`sQ0t*c8_QO~@~m<9(l)+^YnL3mei{AHaXzhuX&T#WUzs OrSZXc9)qlYzV#QG^}fXb 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