diff options
author | Lorenz Diener <[email protected]> | 2017-11-21 13:57:21 +0100 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2017-11-21 13:57:21 +0100 |
commit | 39548ffe98af942054ee494ad058a31696ad8abe (patch) | |
tree | 0da4d21aa14eeb4262dffd2023ef46a48d03bc58 /mastodon | |
parent | d303afefc815af6207d8800ef4929a9a9693e0eb (diff) | |
parent | 56ec90f17c2d5b51c46375761379d9ae4e8a0034 (diff) | |
download | mastodon.py-39548ffe98af942054ee494ad058a31696ad8abe.tar.gz |
Merge branch 'master' of https://github.com/halcy/Mastodon.py
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index da600cd..4ffdde1 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -16,6 +16,7 @@ import dateutil.parser | |||
16 | import re | 16 | import re |
17 | import copy | 17 | import copy |
18 | import threading | 18 | import threading |
19 | import sys | ||
19 | try: | 20 | try: |
20 | from urllib.parse import urlparse | 21 | from urllib.parse import urlparse |
21 | except ImportError: | 22 | except ImportError: |
@@ -776,7 +777,7 @@ class Mastodon: | |||
776 | ### | 777 | ### |
777 | # Writing data: Media | 778 | # Writing data: Media |
778 | ### | 779 | ### |
779 | def media_post(self, media_file, mime_type=None): | 780 | def media_post(self, media_file, mime_type=None, description=None): |
780 | """ | 781 | """ |
781 | Post an image. media_file can either be image data or | 782 | Post an image. media_file can either be image data or |
782 | a file name. If image data is passed directly, the mime | 783 | a file name. If image data is passed directly, the mime |
@@ -804,7 +805,8 @@ class Mastodon: | |||
804 | 805 | ||
805 | media_file_description = (file_name, media_file, mime_type) | 806 | media_file_description = (file_name, media_file, mime_type) |
806 | return self.__api_request('POST', '/api/v1/media', | 807 | return self.__api_request('POST', '/api/v1/media', |
807 | files={'file': media_file_description}) | 808 | files={'file': media_file_description}, |
809 | params={'description': description}) | ||
808 | 810 | ||
809 | ### | 811 | ### |
810 | # Writing data: Domain blocks | 812 | # Writing data: Domain blocks |
@@ -970,6 +972,7 @@ class Mastodon: | |||
970 | 972 | ||
971 | return (date_time_utc - epoch_utc).total_seconds() | 973 | return (date_time_utc - epoch_utc).total_seconds() |
972 | 974 | ||
975 | |||
973 | def __json_date_parse(self, json_object): | 976 | def __json_date_parse(self, json_object): |
974 | """ | 977 | """ |
975 | Parse dates in certain known json fields, if possible. | 978 | Parse dates in certain known json fields, if possible. |
@@ -986,6 +989,29 @@ class Mastodon: | |||
986 | raise MastodonAPIError('Encountered invalid date.') | 989 | raise MastodonAPIError('Encountered invalid date.') |
987 | return json_object | 990 | return json_object |
988 | 991 | ||
992 | def __json_id_to_bignum(self, json_object): | ||
993 | """ | ||
994 | Converts json string IDs to native python bignums. | ||
995 | """ | ||
996 | if sys.version_info.major >= 3: | ||
997 | str_type = str | ||
998 | else: | ||
999 | str_type = unicode | ||
1000 | |||
1001 | if ('id' in json_object and | ||
1002 | isinstance(json_object['id'], str_type)): | ||
1003 | try: | ||
1004 | json_object['id'] = int(json_object['id']) | ||
1005 | except ValueError: | ||
1006 | pass | ||
1007 | |||
1008 | return json_object | ||
1009 | |||
1010 | def __json_hooks(self, json_object): | ||
1011 | json_object = self.__json_date_parse(json_object) | ||
1012 | json_object = self.__json_id_to_bignum(json_object) | ||
1013 | return json_object | ||
1014 | |||
989 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): | 1015 | def __api_request(self, method, endpoint, params={}, files={}, do_ratelimiting=True): |
990 | """ | 1016 | """ |
991 | Internal API request helper. | 1017 | Internal API request helper. |
@@ -1098,7 +1124,7 @@ class Mastodon: | |||
1098 | continue | 1124 | continue |
1099 | 1125 | ||
1100 | try: | 1126 | try: |
1101 | response = response_object.json(object_hook=self.__json_date_parse) | 1127 | response = response_object.json(object_hook=self.__json_hooks) |
1102 | except: | 1128 | except: |
1103 | raise MastodonAPIError( | 1129 | raise MastodonAPIError( |
1104 | "Could not parse response as JSON, response code was %s, " | 1130 | "Could not parse response as JSON, response code was %s, " |