diff options
-rw-r--r-- | mastodon/Mastodon.py | 113 |
1 files changed, 112 insertions, 1 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 3d707ed..d2d634e 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -244,6 +244,12 @@ class Mastodon: | |||
244 | 244 | ||
245 | Returns a list of toot dicts. | 245 | Returns a list of toot dicts. |
246 | """ | 246 | """ |
247 | if max_id != None: | ||
248 | max_id = self.__unpack_id(max_id) | ||
249 | |||
250 | if since_id != None: | ||
251 | since_id = self.__unpack_id(since_id) | ||
252 | |||
247 | params_initial = locals() | 253 | params_initial = locals() |
248 | 254 | ||
249 | if timeline == "local": | 255 | if timeline == "local": |
@@ -289,6 +295,12 @@ class Mastodon: | |||
289 | 295 | ||
290 | Returns a list of toot dicts. | 296 | Returns a list of toot dicts. |
291 | """ | 297 | """ |
298 | if max_id != None: | ||
299 | max_id = self.__unpack_id(max_id) | ||
300 | |||
301 | if since_id != None: | ||
302 | since_id = self.__unpack_id(since_id) | ||
303 | |||
292 | params_initial = locals() | 304 | params_initial = locals() |
293 | 305 | ||
294 | if local == False: | 306 | if local == False: |
@@ -308,6 +320,7 @@ class Mastodon: | |||
308 | 320 | ||
309 | Returns a toot dict. | 321 | Returns a toot dict. |
310 | """ | 322 | """ |
323 | id = self.__unpack_id(id) | ||
311 | url = '/api/v1/statuses/{0}'.format(str(id)) | 324 | url = '/api/v1/statuses/{0}'.format(str(id)) |
312 | return self.__api_request('GET', url) | 325 | return self.__api_request('GET', url) |
313 | 326 | ||
@@ -318,6 +331,7 @@ class Mastodon: | |||
318 | 331 | ||
319 | Returns a card dict. | 332 | Returns a card dict. |
320 | """ | 333 | """ |
334 | id = self.__unpack_id(id) | ||
321 | url = '/api/v1/statuses/{0}/card'.format(str(id)) | 335 | url = '/api/v1/statuses/{0}/card'.format(str(id)) |
322 | return self.__api_request('GET', url) | 336 | return self.__api_request('GET', url) |
323 | 337 | ||
@@ -327,6 +341,7 @@ class Mastodon: | |||
327 | 341 | ||
328 | Returns a context dict. | 342 | Returns a context dict. |
329 | """ | 343 | """ |
344 | id = self.__unpack_id(id) | ||
330 | url = '/api/v1/statuses/{0}/context'.format(str(id)) | 345 | url = '/api/v1/statuses/{0}/context'.format(str(id)) |
331 | return self.__api_request('GET', url) | 346 | return self.__api_request('GET', url) |
332 | 347 | ||
@@ -336,6 +351,7 @@ class Mastodon: | |||
336 | 351 | ||
337 | Returns a list of user dicts. | 352 | Returns a list of user dicts. |
338 | """ | 353 | """ |
354 | id = self.__unpack_id(id) | ||
339 | url = '/api/v1/statuses/{0}/reblogged_by'.format(str(id)) | 355 | url = '/api/v1/statuses/{0}/reblogged_by'.format(str(id)) |
340 | return self.__api_request('GET', url) | 356 | return self.__api_request('GET', url) |
341 | 357 | ||
@@ -345,6 +361,7 @@ class Mastodon: | |||
345 | 361 | ||
346 | Returns a list of user dicts. | 362 | Returns a list of user dicts. |
347 | """ | 363 | """ |
364 | id = self.__unpack_id(id) | ||
348 | url = '/api/v1/statuses/{0}/favourited_by'.format(str(id)) | 365 | url = '/api/v1/statuses/{0}/favourited_by'.format(str(id)) |
349 | return self.__api_request('GET', url) | 366 | return self.__api_request('GET', url) |
350 | 367 | ||
@@ -360,10 +377,17 @@ class Mastodon: | |||
360 | 377 | ||
361 | Returns a list of notification dicts. | 378 | Returns a list of notification dicts. |
362 | """ | 379 | """ |
380 | if max_id != None: | ||
381 | max_id = self.__unpack_id(max_id) | ||
382 | |||
383 | if since_id != None: | ||
384 | since_id = self.__unpack_id(since_id) | ||
385 | |||
363 | if id is None: | 386 | if id is None: |
364 | params = self.__generate_params(locals(), ['id']) | 387 | params = self.__generate_params(locals(), ['id']) |
365 | return self.__api_request('GET', '/api/v1/notifications', params) | 388 | return self.__api_request('GET', '/api/v1/notifications', params) |
366 | else: | 389 | else: |
390 | id = self.__unpack_id(id) | ||
367 | url = '/api/v1/notifications/{0}'.format(str(id)) | 391 | url = '/api/v1/notifications/{0}'.format(str(id)) |
368 | return self.__api_request('GET', url) | 392 | return self.__api_request('GET', url) |
369 | 393 | ||
@@ -376,6 +400,7 @@ class Mastodon: | |||
376 | 400 | ||
377 | Returns a user dict. | 401 | Returns a user dict. |
378 | """ | 402 | """ |
403 | id = self.__unpack_id(id) | ||
379 | url = '/api/v1/accounts/{0}'.format(str(id)) | 404 | url = '/api/v1/accounts/{0}'.format(str(id)) |
380 | return self.__api_request('GET', url) | 405 | return self.__api_request('GET', url) |
381 | 406 | ||
@@ -393,6 +418,13 @@ class Mastodon: | |||
393 | 418 | ||
394 | Returns a list of toot dicts. | 419 | Returns a list of toot dicts. |
395 | """ | 420 | """ |
421 | id = self.__unpack_id(id) | ||
422 | if max_id != None: | ||
423 | max_id = self.__unpack_id(max_id) | ||
424 | |||
425 | if since_id != None: | ||
426 | since_id = self.__unpack_id(since_id) | ||
427 | |||
396 | params = self.__generate_params(locals(), ['id']) | 428 | params = self.__generate_params(locals(), ['id']) |
397 | url = '/api/v1/accounts/{0}/statuses'.format(str(id)) | 429 | url = '/api/v1/accounts/{0}/statuses'.format(str(id)) |
398 | return self.__api_request('GET', url, params) | 430 | return self.__api_request('GET', url, params) |
@@ -403,6 +435,13 @@ class Mastodon: | |||
403 | 435 | ||
404 | Returns a list of user dicts. | 436 | Returns a list of user dicts. |
405 | """ | 437 | """ |
438 | id = self.__unpack_id(id) | ||
439 | if max_id != None: | ||
440 | max_id = self.__unpack_id(max_id) | ||
441 | |||
442 | if since_id != None: | ||
443 | since_id = self.__unpack_id(since_id) | ||
444 | |||
406 | params = self.__generate_params(locals(), ['id']) | 445 | params = self.__generate_params(locals(), ['id']) |
407 | url = '/api/v1/accounts/{0}/following'.format(str(id)) | 446 | url = '/api/v1/accounts/{0}/following'.format(str(id)) |
408 | return self.__api_request('GET', url, params) | 447 | return self.__api_request('GET', url, params) |
@@ -413,6 +452,13 @@ class Mastodon: | |||
413 | 452 | ||
414 | Returns a list of user dicts. | 453 | Returns a list of user dicts. |
415 | """ | 454 | """ |
455 | id = self.__unpack_id(id) | ||
456 | if max_id != None: | ||
457 | max_id = self.__unpack_id(max_id) | ||
458 | |||
459 | if since_id != None: | ||
460 | since_id = self.__unpack_id(since_id) | ||
461 | |||
416 | params = self.__generate_params(locals(), ['id']) | 462 | params = self.__generate_params(locals(), ['id']) |
417 | url = '/api/v1/accounts/{0}/followers'.format(str(id)) | 463 | url = '/api/v1/accounts/{0}/followers'.format(str(id)) |
418 | return self.__api_request('GET', url, params) | 464 | return self.__api_request('GET', url, params) |
@@ -424,6 +470,7 @@ class Mastodon: | |||
424 | 470 | ||
425 | Returns a list of relationship dicts. | 471 | Returns a list of relationship dicts. |
426 | """ | 472 | """ |
473 | id = self.__unpack_id(id) | ||
427 | params = self.__generate_params(locals()) | 474 | params = self.__generate_params(locals()) |
428 | return self.__api_request('GET', '/api/v1/accounts/relationships', | 475 | return self.__api_request('GET', '/api/v1/accounts/relationships', |
429 | params) | 476 | params) |
@@ -460,6 +507,12 @@ class Mastodon: | |||
460 | 507 | ||
461 | Returns a list of user dicts. | 508 | Returns a list of user dicts. |
462 | """ | 509 | """ |
510 | if max_id != None: | ||
511 | max_id = self.__unpack_id(max_id) | ||
512 | |||
513 | if since_id != None: | ||
514 | since_id = self.__unpack_id(since_id) | ||
515 | |||
463 | params = self.__generate_params(locals()) | 516 | params = self.__generate_params(locals()) |
464 | return self.__api_request('GET', '/api/v1/mutes', params) | 517 | return self.__api_request('GET', '/api/v1/mutes', params) |
465 | 518 | ||
@@ -469,6 +522,12 @@ class Mastodon: | |||
469 | 522 | ||
470 | Returns a list of user dicts. | 523 | Returns a list of user dicts. |
471 | """ | 524 | """ |
525 | if max_id != None: | ||
526 | max_id = self.__unpack_id(max_id) | ||
527 | |||
528 | if since_id != None: | ||
529 | since_id = self.__unpack_id(since_id) | ||
530 | |||
472 | params = self.__generate_params(locals()) | 531 | params = self.__generate_params(locals()) |
473 | return self.__api_request('GET', '/api/v1/blocks', params) | 532 | return self.__api_request('GET', '/api/v1/blocks', params) |
474 | 533 | ||
@@ -495,6 +554,12 @@ class Mastodon: | |||
495 | 554 | ||
496 | Returns a list of toot dicts. | 555 | Returns a list of toot dicts. |
497 | """ | 556 | """ |
557 | if max_id != None: | ||
558 | max_id = self.__unpack_id(max_id) | ||
559 | |||
560 | if since_id != None: | ||
561 | since_id = self.__unpack_id(since_id) | ||
562 | |||
498 | params = self.__generate_params(locals()) | 563 | params = self.__generate_params(locals()) |
499 | return self.__api_request('GET', '/api/v1/favourites', params) | 564 | return self.__api_request('GET', '/api/v1/favourites', params) |
500 | 565 | ||
@@ -507,6 +572,12 @@ class Mastodon: | |||
507 | 572 | ||
508 | Returns a list of user dicts. | 573 | Returns a list of user dicts. |
509 | """ | 574 | """ |
575 | if max_id != None: | ||
576 | max_id = self.__unpack_id(max_id) | ||
577 | |||
578 | if since_id != None: | ||
579 | since_id = self.__unpack_id(since_id) | ||
580 | |||
510 | params = self.__generate_params(locals()) | 581 | params = self.__generate_params(locals()) |
511 | return self.__api_request('GET', '/api/v1/follow_requests', params) | 582 | return self.__api_request('GET', '/api/v1/follow_requests', params) |
512 | 583 | ||
@@ -519,6 +590,12 @@ class Mastodon: | |||
519 | 590 | ||
520 | Returns a list of blocked domain URLs (as strings, without protocol specifier). | 591 | Returns a list of blocked domain URLs (as strings, without protocol specifier). |
521 | """ | 592 | """ |
593 | if max_id != None: | ||
594 | max_id = self.__unpack_id(max_id) | ||
595 | |||
596 | if since_id != None: | ||
597 | since_id = self.__unpack_id(since_id) | ||
598 | |||
522 | params = self.__generate_params(locals()) | 599 | params = self.__generate_params(locals()) |
523 | return self.__api_request('GET', '/api/v1/domain_blocks', params) | 600 | return self.__api_request('GET', '/api/v1/domain_blocks', params) |
524 | 601 | ||
@@ -555,6 +632,9 @@ class Mastodon: | |||
555 | 632 | ||
556 | Returns a toot dict with the new status. | 633 | Returns a toot dict with the new status. |
557 | """ | 634 | """ |
635 | if in_reply_to_id != None: | ||
636 | in_reply_to_id = self.__unpack_id(in_reply_to_id) | ||
637 | |||
558 | params_initial = locals() | 638 | params_initial = locals() |
559 | 639 | ||
560 | # Validate visibility parameter | 640 | # Validate visibility parameter |
@@ -599,6 +679,7 @@ class Mastodon: | |||
599 | 679 | ||
600 | Returns an empty dict for good measure. | 680 | Returns an empty dict for good measure. |
601 | """ | 681 | """ |
682 | id = self.__unpack_id(id) | ||
602 | url = '/api/v1/statuses/{0}'.format(str(id)) | 683 | url = '/api/v1/statuses/{0}'.format(str(id)) |
603 | return self.__api_request('DELETE', url) | 684 | return self.__api_request('DELETE', url) |
604 | 685 | ||
@@ -608,6 +689,7 @@ class Mastodon: | |||
608 | 689 | ||
609 | Returns a toot dict with a new status that wraps around the reblogged one. | 690 | Returns a toot dict with a new status that wraps around the reblogged one. |
610 | """ | 691 | """ |
692 | id = self.__unpack_id(id) | ||
611 | url = '/api/v1/statuses/{0}/reblog'.format(str(id)) | 693 | url = '/api/v1/statuses/{0}/reblog'.format(str(id)) |
612 | return self.__api_request('POST', url) | 694 | return self.__api_request('POST', url) |
613 | 695 | ||
@@ -617,6 +699,7 @@ class Mastodon: | |||
617 | 699 | ||
618 | Returns a toot dict with the status that used to be reblogged. | 700 | Returns a toot dict with the status that used to be reblogged. |
619 | """ | 701 | """ |
702 | id = self.__unpack_id(id) | ||
620 | url = '/api/v1/statuses/{0}/unreblog'.format(str(id)) | 703 | url = '/api/v1/statuses/{0}/unreblog'.format(str(id)) |
621 | return self.__api_request('POST', url) | 704 | return self.__api_request('POST', url) |
622 | 705 | ||
@@ -626,6 +709,7 @@ class Mastodon: | |||
626 | 709 | ||
627 | Returns a toot dict with the favourited status. | 710 | Returns a toot dict with the favourited status. |
628 | """ | 711 | """ |
712 | id = self.__unpack_id(id) | ||
629 | url = '/api/v1/statuses/{0}/favourite'.format(str(id)) | 713 | url = '/api/v1/statuses/{0}/favourite'.format(str(id)) |
630 | return self.__api_request('POST', url) | 714 | return self.__api_request('POST', url) |
631 | 715 | ||
@@ -635,6 +719,7 @@ class Mastodon: | |||
635 | 719 | ||
636 | Returns a toot dict with the un-favourited status. | 720 | Returns a toot dict with the un-favourited status. |
637 | """ | 721 | """ |
722 | id = self.__unpack_id(id) | ||
638 | url = '/api/v1/statuses/{0}/unfavourite'.format(str(id)) | 723 | url = '/api/v1/statuses/{0}/unfavourite'.format(str(id)) |
639 | return self.__api_request('POST', url) | 724 | return self.__api_request('POST', url) |
640 | 725 | ||
@@ -644,6 +729,7 @@ class Mastodon: | |||
644 | 729 | ||
645 | Returns a toot dict with the now muted status | 730 | Returns a toot dict with the now muted status |
646 | """ | 731 | """ |
732 | id = self.__unpack_id(id) | ||
647 | url = '/api/v1/statuses/{0}/mute'.format(str(id)) | 733 | url = '/api/v1/statuses/{0}/mute'.format(str(id)) |
648 | return self.__api_request('POST', url) | 734 | return self.__api_request('POST', url) |
649 | 735 | ||
@@ -653,6 +739,7 @@ class Mastodon: | |||
653 | 739 | ||
654 | Returns a toot dict with the status that used to be muted. | 740 | Returns a toot dict with the status that used to be muted. |
655 | """ | 741 | """ |
742 | id = self.__unpack_id(id) | ||
656 | url = '/api/v1/statuses/{0}/unmute'.format(str(id)) | 743 | url = '/api/v1/statuses/{0}/unmute'.format(str(id)) |
657 | return self.__api_request('POST', url) | 744 | return self.__api_request('POST', url) |
658 | 745 | ||
@@ -670,6 +757,7 @@ class Mastodon: | |||
670 | """ | 757 | """ |
671 | Deletes a single notification | 758 | Deletes a single notification |
672 | """ | 759 | """ |
760 | id = self.__unpack_id(id) | ||
673 | params = self.__generate_params(locals()) | 761 | params = self.__generate_params(locals()) |
674 | return self.__api_request('POST', '/api/v1/notifications/dismiss', params) | 762 | return self.__api_request('POST', '/api/v1/notifications/dismiss', params) |
675 | 763 | ||
@@ -682,6 +770,7 @@ class Mastodon: | |||
682 | 770 | ||
683 | Returns a relationship dict containing the updated relationship to the user. | 771 | Returns a relationship dict containing the updated relationship to the user. |
684 | """ | 772 | """ |
773 | id = self.__unpack_id(id) | ||
685 | url = '/api/v1/accounts/{0}/follow'.format(str(id)) | 774 | url = '/api/v1/accounts/{0}/follow'.format(str(id)) |
686 | return self.__api_request('POST', url) | 775 | return self.__api_request('POST', url) |
687 | 776 | ||
@@ -700,6 +789,7 @@ class Mastodon: | |||
700 | 789 | ||
701 | Returns a relationship dict containing the updated relationship to the user. | 790 | Returns a relationship dict containing the updated relationship to the user. |
702 | """ | 791 | """ |
792 | id = self.__unpack_id(id) | ||
703 | url = '/api/v1/accounts/{0}/unfollow'.format(str(id)) | 793 | url = '/api/v1/accounts/{0}/unfollow'.format(str(id)) |
704 | return self.__api_request('POST', url) | 794 | return self.__api_request('POST', url) |
705 | 795 | ||
@@ -709,6 +799,7 @@ class Mastodon: | |||
709 | 799 | ||
710 | Returns a relationship dict containing the updated relationship to the user. | 800 | Returns a relationship dict containing the updated relationship to the user. |
711 | """ | 801 | """ |
802 | id = self.__unpack_id(id) | ||
712 | url = '/api/v1/accounts/{0}/block'.format(str(id)) | 803 | url = '/api/v1/accounts/{0}/block'.format(str(id)) |
713 | return self.__api_request('POST', url) | 804 | return self.__api_request('POST', url) |
714 | 805 | ||
@@ -718,6 +809,7 @@ class Mastodon: | |||
718 | 809 | ||
719 | Returns a relationship dict containing the updated relationship to the user. | 810 | Returns a relationship dict containing the updated relationship to the user. |
720 | """ | 811 | """ |
812 | id = self.__unpack_id(id) | ||
721 | url = '/api/v1/accounts/{0}/unblock'.format(str(id)) | 813 | url = '/api/v1/accounts/{0}/unblock'.format(str(id)) |
722 | return self.__api_request('POST', url) | 814 | return self.__api_request('POST', url) |
723 | 815 | ||
@@ -727,6 +819,7 @@ class Mastodon: | |||
727 | 819 | ||
728 | Returns a relationship dict containing the updated relationship to the user. | 820 | Returns a relationship dict containing the updated relationship to the user. |
729 | """ | 821 | """ |
822 | id = self.__unpack_id(id) | ||
730 | url = '/api/v1/accounts/{0}/mute'.format(str(id)) | 823 | url = '/api/v1/accounts/{0}/mute'.format(str(id)) |
731 | return self.__api_request('POST', url) | 824 | return self.__api_request('POST', url) |
732 | 825 | ||
@@ -736,6 +829,7 @@ class Mastodon: | |||
736 | 829 | ||
737 | Returns a relationship dict containing the updated relationship to the user. | 830 | Returns a relationship dict containing the updated relationship to the user. |
738 | """ | 831 | """ |
832 | id = self.__unpack_id(id) | ||
739 | url = '/api/v1/accounts/{0}/unmute'.format(str(id)) | 833 | url = '/api/v1/accounts/{0}/unmute'.format(str(id)) |
740 | return self.__api_request('POST', url) | 834 | return self.__api_request('POST', url) |
741 | 835 | ||
@@ -763,6 +857,8 @@ class Mastodon: | |||
763 | 857 | ||
764 | Returns a report dict. | 858 | Returns a report dict. |
765 | """ | 859 | """ |
860 | account_id = self.__unpack_id(account_id) | ||
861 | status_ids = map(lambda x: self.__unpack_id(x), status_ids) | ||
766 | params = self.__generate_params(locals()) | 862 | params = self.__generate_params(locals()) |
767 | return self.__api_request('POST', '/api/v1/reports/', params) | 863 | return self.__api_request('POST', '/api/v1/reports/', params) |
768 | 864 | ||
@@ -775,6 +871,7 @@ class Mastodon: | |||
775 | 871 | ||
776 | Returns an empty dict. | 872 | Returns an empty dict. |
777 | """ | 873 | """ |
874 | id = self.__unpack_id(id) | ||
778 | url = '/api/v1/follow_requests/{0}/authorize'.format(str(id)) | 875 | url = '/api/v1/follow_requests/{0}/authorize'.format(str(id)) |
779 | return self.__api_request('POST', url) | 876 | return self.__api_request('POST', url) |
780 | 877 | ||
@@ -784,6 +881,7 @@ class Mastodon: | |||
784 | 881 | ||
785 | Returns an empty dict. | 882 | Returns an empty dict. |
786 | """ | 883 | """ |
884 | id = self.__unpack_id(id) | ||
787 | url = '/api/v1/follow_requests/{0}/reject'.format(str(id)) | 885 | url = '/api/v1/follow_requests/{0}/reject'.format(str(id)) |
788 | return self.__api_request('POST', url) | 886 | return self.__api_request('POST', url) |
789 | 887 | ||
@@ -1275,7 +1373,20 @@ class Mastodon: | |||
1275 | del params[key] | 1373 | del params[key] |
1276 | 1374 | ||
1277 | return params | 1375 | return params |
1278 | 1376 | ||
1377 | def __unpack_id(self, id): | ||
1378 | """ | ||
1379 | Internal object-to-id converter | ||
1380 | |||
1381 | Checks if id is a dict that contains id and | ||
1382 | returns the id inside, otherwise just returns | ||
1383 | the id straight. | ||
1384 | """ | ||
1385 | if isinstance(id, dict) and "id" in id: | ||
1386 | return id["id"] | ||
1387 | else: | ||
1388 | return id | ||
1389 | |||
1279 | def __get_token_expired(self): | 1390 | def __get_token_expired(self): |
1280 | """Internal helper for oauth code""" | 1391 | """Internal helper for oauth code""" |
1281 | return self._token_expired < datetime.datetime.now() | 1392 | return self._token_expired < datetime.datetime.now() |