diff options
-rw-r--r-- | Mastodon.py | 110 |
1 files changed, 55 insertions, 55 deletions
diff --git a/Mastodon.py b/Mastodon.py index 2519bc8..6b83a6e 100644 --- a/Mastodon.py +++ b/Mastodon.py | |||
@@ -6,7 +6,7 @@ import os.path | |||
6 | 6 | ||
7 | class Mastodon: | 7 | class Mastodon: |
8 | """ | 8 | """ |
9 | Super basic but thorough and easy to use mastodon.social | 9 | Super basic but thorough and easy to use mastodon.social |
10 | api wrapper in python. | 10 | api wrapper in python. |
11 | 11 | ||
12 | If anything is unclear, check the official API docs at | 12 | If anything is unclear, check the official API docs at |
@@ -25,7 +25,7 @@ class Mastodon: | |||
25 | @staticmethod | 25 | @staticmethod |
26 | def create_app(client_name, scopes = ['read', 'write', 'follow'], redirect_uris = None, to_file = None, api_base_url = __DEFAULT_BASE_URL): | 26 | def create_app(client_name, scopes = ['read', 'write', 'follow'], redirect_uris = None, to_file = None, api_base_url = __DEFAULT_BASE_URL): |
27 | """ | 27 | """ |
28 | Creates a new app with given client_name and scopes (read, write, follow) | 28 | Creates a new app with given client_name and scopes (read, write, follow) |
29 | 29 | ||
30 | Specify redirect_uris if you want users to be redirected to a certain page after authenticating. | 30 | Specify redirect_uris if you want users to be redirected to a certain page after authenticating. |
31 | Specify to_file to persist your apps info to a file so you can use them in the constructor. | 31 | Specify to_file to persist your apps info to a file so you can use them in the constructor. |
@@ -57,7 +57,7 @@ class Mastodon: | |||
57 | ### | 57 | ### |
58 | def __init__(self, client_id, client_secret = None, access_token = None, api_base_url = __DEFAULT_BASE_URL): | 58 | def __init__(self, client_id, client_secret = None, access_token = None, api_base_url = __DEFAULT_BASE_URL): |
59 | """ | 59 | """ |
60 | Creates a new API wrapper instance based on the given client_secret and client_id. If you | 60 | Creates a new API wrapper instance based on the given client_secret and client_id. If you |
61 | give a client_id and it is not a file, you must also give a secret. | 61 | give a client_id and it is not a file, you must also give a secret. |
62 | 62 | ||
63 | You can also directly specify an access_token, directly or as a file. | 63 | You can also directly specify an access_token, directly or as a file. |
@@ -84,7 +84,7 @@ class Mastodon: | |||
84 | 84 | ||
85 | def log_in(self, username, password, scopes = ['read', 'write', 'follow'], to_file = None): | 85 | def log_in(self, username, password, scopes = ['read', 'write', 'follow'], to_file = None): |
86 | """ | 86 | """ |
87 | Logs in and sets access_token to what was returned. | 87 | Logs in and sets access_token to what was returned. |
88 | Can persist access token to file. | 88 | Can persist access token to file. |
89 | 89 | ||
90 | Returns the access_token, as well. | 90 | Returns the access_token, as well. |
@@ -109,9 +109,9 @@ class Mastodon: | |||
109 | ## | 109 | ## |
110 | def timeline(self, timeline = 'home', max_id = None, since_id = None, limit = None): | 110 | def timeline(self, timeline = 'home', max_id = None, since_id = None, limit = None): |
111 | """ | 111 | """ |
112 | Returns statuses, most recent ones first. Timeline can be home, mentions, public | 112 | Returns statuses, most recent ones first. Timeline can be home, mentions, public |
113 | or tag/:hashtag | 113 | or tag/:hashtag |
114 | """ | 114 | """ |
115 | params = self.__generate_params(locals(), ['timeline']) | 115 | params = self.__generate_params(locals(), ['timeline']) |
116 | return self.__api_request('GET', '/api/v1/timelines/' + timeline, params) | 116 | return self.__api_request('GET', '/api/v1/timelines/' + timeline, params) |
117 | 117 | ||
@@ -120,26 +120,26 @@ class Mastodon: | |||
120 | ### | 120 | ### |
121 | def status(self, id): | 121 | def status(self, id): |
122 | """ | 122 | """ |
123 | Returns a status. | 123 | Returns a status. |
124 | """ | 124 | """ |
125 | return self.__api_request('GET', '/api/v1/statuses/' + str(id)) | 125 | return self.__api_request('GET', '/api/v1/statuses/' + str(id)) |
126 | 126 | ||
127 | def status_context(self, id): | 127 | def status_context(self, id): |
128 | """ | 128 | """ |
129 | Returns ancestors and descendants of the status. | 129 | Returns ancestors and descendants of the status. |
130 | """ | 130 | """ |
131 | return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/context') | 131 | return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/context') |
132 | 132 | ||
133 | def status_reblogged_by(self, id): | 133 | def status_reblogged_by(self, id): |
134 | """ | 134 | """ |
135 | Returns a list of users that have reblogged a status. | 135 | Returns a list of users that have reblogged a status. |
136 | """ | 136 | """ |
137 | return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/reblogged_by') | 137 | return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/reblogged_by') |
138 | 138 | ||
139 | def status_favourited_by(self, id): | 139 | def status_favourited_by(self, id): |
140 | """ | 140 | """ |
141 | Returns a list of users that have favourited a status. | 141 | Returns a list of users that have favourited a status. |
142 | """ | 142 | """ |
143 | return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/favourited_by') | 143 | return self.__api_request('GET', '/api/v1/statuses/' + str(id) + '/favourited_by') |
144 | 144 | ||
145 | ### | 145 | ### |
@@ -147,54 +147,54 @@ class Mastodon: | |||
147 | ### | 147 | ### |
148 | def account(self, id): | 148 | def account(self, id): |
149 | """ | 149 | """ |
150 | Returns account. | 150 | Returns account. |
151 | """ | 151 | """ |
152 | return self.__api_request('GET', '/api/v1/accounts/' + str(id)) | 152 | return self.__api_request('GET', '/api/v1/accounts/' + str(id)) |
153 | 153 | ||
154 | def account_verify_credentials(self): | 154 | def account_verify_credentials(self): |
155 | """ | 155 | """ |
156 | Returns authenticated user's account. | 156 | Returns authenticated user's account. |
157 | """ | 157 | """ |
158 | return self.__api_request('GET', '/api/v1/accounts/verify_credentials') | 158 | return self.__api_request('GET', '/api/v1/accounts/verify_credentials') |
159 | 159 | ||
160 | def account_statuses(self, id, max_id = None, since_id = None, limit = None): | 160 | def account_statuses(self, id, max_id = None, since_id = None, limit = None): |
161 | """ | 161 | """ |
162 | Returns statuses by user. Same options as timeline are permitted. | 162 | Returns statuses by user. Same options as timeline are permitted. |
163 | """ | 163 | """ |
164 | params = self.__generate_params(locals(), ['id']) | 164 | params = self.__generate_params(locals(), ['id']) |
165 | return self.__api_request('GET', '/api/v1/accounts/' + str(id) + '/statuses') | 165 | return self.__api_request('GET', '/api/v1/accounts/' + str(id) + '/statuses') |
166 | 166 | ||
167 | def account_following(self, id): | 167 | def account_following(self, id): |
168 | """ | 168 | """ |
169 | Returns users the given user is following. | 169 | Returns users the given user is following. |
170 | """ | 170 | """ |
171 | return self.__api_request('GET', '/api/v1/accounts/' + str(id) + '/following') | 171 | return self.__api_request('GET', '/api/v1/accounts/' + str(id) + '/following') |
172 | 172 | ||
173 | def account_followers(self, id): | 173 | def account_followers(self, id): |
174 | """ | 174 | """ |
175 | Returns users the given user is followed by. | 175 | Returns users the given user is followed by. |
176 | """ | 176 | """ |
177 | return self.__api_request('GET', '/api/v1/accounts/' + str(id) + '/followers') | 177 | return self.__api_request('GET', '/api/v1/accounts/' + str(id) + '/followers') |
178 | 178 | ||
179 | def account_relationships(self, id): | 179 | def account_relationships(self, id): |
180 | """ | 180 | """ |
181 | Returns relationships (following, followed_by, blocking) of the logged in user to | 181 | Returns relationships (following, followed_by, blocking) of the logged in user to |
182 | a given account. id can be a list. | 182 | a given account. id can be a list. |
183 | """ | 183 | """ |
184 | params = self.__generate_params(locals()) | 184 | params = self.__generate_params(locals()) |
185 | return self.__api_request('GET', '/api/v1/accounts/relationships', params) | 185 | return self.__api_request('GET', '/api/v1/accounts/relationships', params) |
186 | 186 | ||
187 | def account_suggestions(self): | 187 | def account_suggestions(self): |
188 | """ | 188 | """ |
189 | Returns accounts that the system suggests the authenticated user to follow. | 189 | Returns accounts that the system suggests the authenticated user to follow. |
190 | """ | 190 | """ |
191 | return self.__api_request('GET', '/api/v1/accounts/suggestions') | 191 | return self.__api_request('GET', '/api/v1/accounts/suggestions') |
192 | 192 | ||
193 | def account_search(self, q, limit = None): | 193 | def account_search(self, q, limit = None): |
194 | """ | 194 | """ |
195 | Returns matching accounts. Will lookup an account remotely if the search term is | 195 | Returns matching accounts. Will lookup an account remotely if the search term is |
196 | in the username@domain format and not yet in the database. | 196 | in the username@domain format and not yet in the database. |
197 | """ | 197 | """ |
198 | params = self.__generate_params(locals()) | 198 | params = self.__generate_params(locals()) |
199 | return self.__api_request('GET', '/api/v1/accounts/search', params) | 199 | return self.__api_request('GET', '/api/v1/accounts/search', params) |
200 | 200 | ||
@@ -203,24 +203,24 @@ class Mastodon: | |||
203 | ### | 203 | ### |
204 | def status_post(self, status, in_reply_to_id = None, media_ids = None): | 204 | def status_post(self, status, in_reply_to_id = None, media_ids = None): |
205 | """ | 205 | """ |
206 | Posts a status. Can optionally be in reply to another status and contain | 206 | Posts a status. Can optionally be in reply to another status and contain |
207 | up to four pieces of media (Uploaded via media_post()). | 207 | up to four pieces of media (Uploaded via media_post()). |
208 | 208 | ||
209 | Returns the new status. | 209 | Returns the new status. |
210 | """ | 210 | """ |
211 | params = self.__generate_params(locals()) | 211 | params = self.__generate_params(locals()) |
212 | return self.__api_request('POST', '/api/v1/statuses', params) | 212 | return self.__api_request('POST', '/api/v1/statuses', params) |
213 | 213 | ||
214 | def toot(self, status): | 214 | def toot(self, status): |
215 | """ | 215 | """ |
216 | Synonym for status_post that only takes the status text as input. | 216 | Synonym for status_post that only takes the status text as input. |
217 | """ | 217 | """ |
218 | return self.status_post(status) | 218 | return self.status_post(status) |
219 | 219 | ||
220 | def status_delete(self, id): | 220 | def status_delete(self, id): |
221 | """ | 221 | """ |
222 | Deletes a status | 222 | Deletes a status |
223 | """ | 223 | """ |
224 | return self.__api_request('DELETE', '/api/v1/statuses/' + str(id)) | 224 | return self.__api_request('DELETE', '/api/v1/statuses/' + str(id)) |
225 | 225 | ||
226 | def status_reblog(self, id): | 226 | def status_reblog(self, id): |
@@ -231,25 +231,25 @@ class Mastodon: | |||
231 | 231 | ||
232 | def status_unreblog(self, id): | 232 | def status_unreblog(self, id): |
233 | """ | 233 | """ |
234 | Un-reblogs a status. | 234 | Un-reblogs a status. |
235 | 235 | ||
236 | Returns the status that used to be reblogged. | 236 | Returns the status that used to be reblogged. |
237 | """ | 237 | """ |
238 | return self.__api_request('POST', '/api/v1/statuses/' + str(id) + "/unreblog") | 238 | return self.__api_request('POST', '/api/v1/statuses/' + str(id) + "/unreblog") |
239 | 239 | ||
240 | def status_favourite(self, id): | 240 | def status_favourite(self, id): |
241 | """ | 241 | """ |
242 | Favourites a status. | 242 | Favourites a status. |
243 | 243 | ||
244 | Returns the favourited status. | 244 | Returns the favourited status. |
245 | """ | 245 | """ |
246 | return self.__api_request('POST', '/api/v1/statuses/' + str(id) + "/favourite") | 246 | return self.__api_request('POST', '/api/v1/statuses/' + str(id) + "/favourite") |
247 | 247 | ||
248 | def status_unfavourite(self, id): | 248 | def status_unfavourite(self, id): |
249 | """Favourites a status. | 249 | """Favourites a status. |
250 | 250 | ||
251 | Returns the un-favourited status. | 251 | Returns the un-favourited status. |
252 | """ | 252 | """ |
253 | return self.__api_request('POST', '/api/v1/statuses/' + str(id) + "/unfavourite") | 253 | return self.__api_request('POST', '/api/v1/statuses/' + str(id) + "/unfavourite") |
254 | 254 | ||
255 | ### | 255 | ### |
@@ -257,34 +257,34 @@ class Mastodon: | |||
257 | ### | 257 | ### |
258 | def account_follow(self, id): | 258 | def account_follow(self, id): |
259 | """ | 259 | """ |
260 | Follows a user. | 260 | Follows a user. |
261 | 261 | ||
262 | Returns the updated relationship to the user. | 262 | Returns the updated relationship to the user. |
263 | """ | 263 | """ |
264 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/follow") | 264 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/follow") |
265 | 265 | ||
266 | def account_unfollow(self, id): | 266 | def account_unfollow(self, id): |
267 | """ | 267 | """ |
268 | Unfollows a user. | 268 | Unfollows a user. |
269 | 269 | ||
270 | Returns the updated relationship to the user. | 270 | Returns the updated relationship to the user. |
271 | """ | 271 | """ |
272 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/unfollow") | 272 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/unfollow") |
273 | 273 | ||
274 | def account_block(self, id): | 274 | def account_block(self, id): |
275 | """ | 275 | """ |
276 | Blocks a user. | 276 | Blocks a user. |
277 | 277 | ||
278 | Returns the updated relationship to the user. | 278 | Returns the updated relationship to the user. |
279 | """ | 279 | """ |
280 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/block") | 280 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/block") |
281 | 281 | ||
282 | def account_unblock(self, id): | 282 | def account_unblock(self, id): |
283 | """ | 283 | """ |
284 | Unblocks a user. | 284 | Unblocks a user. |
285 | 285 | ||
286 | Returns the updated relationship to the user. | 286 | Returns the updated relationship to the user. |
287 | """ | 287 | """ |
288 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/unblock") | 288 | return self.__api_request('POST', '/api/v1/accounts/' + str(id) + "/unblock") |
289 | 289 | ||
290 | ### | 290 | ### |
@@ -292,11 +292,11 @@ class Mastodon: | |||
292 | ### | 292 | ### |
293 | def media_post(self, media_file): | 293 | def media_post(self, media_file): |
294 | """ | 294 | """ |
295 | Posts an image. media_file can either be image data or | 295 | Posts an image. media_file can either be image data or |
296 | a file name. | 296 | a file name. |
297 | 297 | ||
298 | Returns the ID of the media that can then be used in status_post(). | 298 | Returns the ID of the media that can then be used in status_post(). |
299 | """ | 299 | """ |
300 | if os.path.isfile(media_file): | 300 | if os.path.isfile(media_file): |
301 | media_file = open(media_file, 'rb') | 301 | media_file = open(media_file, 'rb') |
302 | 302 | ||
@@ -307,8 +307,8 @@ class Mastodon: | |||
307 | ### | 307 | ### |
308 | def __api_request(self, method, endpoint, params = {}, files = {}): | 308 | def __api_request(self, method, endpoint, params = {}, files = {}): |
309 | """ | 309 | """ |
310 | Internal API request helper. | 310 | Internal API request helper. |
311 | """ | 311 | """ |
312 | response = None | 312 | response = None |
313 | headers = None | 313 | headers = None |
314 | 314 | ||
@@ -334,8 +334,8 @@ class Mastodon: | |||
334 | 334 | ||
335 | def __generate_params(self, params, exclude = []): | 335 | def __generate_params(self, params, exclude = []): |
336 | """ | 336 | """ |
337 | Internal named-parameters-to-dict helper. | 337 | Internal named-parameters-to-dict helper. |
338 | """ | 338 | """ |
339 | params = dict(params) | 339 | params = dict(params) |
340 | 340 | ||
341 | del params['self'] | 341 | del params['self'] |