aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhalcy <halcy@ARARAGI-KUN>2022-11-17 23:25:41 +0200
committerhalcy <halcy@ARARAGI-KUN>2022-11-17 23:25:41 +0200
commit897b3a23cf4177675f0769de68f08ad8c4faf335 (patch)
treee7eb1c75028792509e7f64f1a5eee3eb6e5643ae /mastodon
parent725f79466de65743df0c3498a1bb08e106b5209e (diff)
downloadmastodon.py-897b3a23cf4177675f0769de68f08ad8c4faf335.tar.gz
add server datetime retriever, fix tests some more
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py35
1 files changed, 25 insertions, 10 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 05ebaaa..44e3b52 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -481,8 +481,7 @@ class Mastodon:
481 # instance() was added in 1.1.0, so our best guess is 1.0.0. 481 # instance() was added in 1.1.0, so our best guess is 1.0.0.
482 version_str = "1.0.0" 482 version_str = "1.0.0"
483 483
484 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string( 484 self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version_str)
485 version_str)
486 return version_str 485 return version_str
487 486
488 def verify_minimum_version(self, version_str, cached=False): 487 def verify_minimum_version(self, version_str, cached=False):
@@ -503,7 +502,24 @@ class Mastodon:
503 elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch: 502 elif major == self.mastodon_major and minor == self.mastodon_minor and patch > self.mastodon_patch:
504 return False 503 return False
505 return True 504 return True
505
506 def get_approx_server_time(self):
507 """
508 Retrieve the approximate server time
509
510 We parse this from the hopefully present "Date" header, but make no effort to compensate for latency.
511 """
512 response = self.__api_request("HEAD", "/", return_response_object=True)
513 print(response.headers)
514 if 'Date' in response.headers:
515 server_time_datetime = dateutil.parser.parse(response.headers['Date'])
506 516
517 # Make sure we're in local time
518 epoch_time = self.__datetime_to_epoch(server_time_datetime)
519 return datetime.datetime.fromtimestamp(epoch_time)
520 else:
521 raise MastodonAPIError("No server time in response.")
522
507 @staticmethod 523 @staticmethod
508 def get_supported_version(): 524 def get_supported_version():
509 """ 525 """
@@ -937,7 +953,7 @@ class Mastodon:
937 953
938 Returns a `card dict`_. 954 Returns a `card dict`_.
939 """ 955 """
940 if self.verify_minimum_version("3.0.0"): 956 if self.verify_minimum_version("3.0.0", cached=True):
941 return self.status(id).card 957 return self.status(id).card
942 else: 958 else:
943 id = self.__unpack_id(id) 959 id = self.__unpack_id(id)
@@ -2139,7 +2155,7 @@ class Mastodon:
2139 """ 2155 """
2140 id = self.__unpack_id(id) 2156 id = self.__unpack_id(id)
2141 2157
2142 if self.verify_minimum_version("2.9.2"): 2158 if self.verify_minimum_version("2.9.2", cached=True):
2143 url = '/api/v1/notifications/{0}/dismiss'.format(str(id)) 2159 url = '/api/v1/notifications/{0}/dismiss'.format(str(id))
2144 self.__api_request('POST', url) 2160 self.__api_request('POST', url)
2145 else: 2161 else:
@@ -2590,14 +2606,14 @@ class Mastodon:
2590 focus = str(focus[0]) + "," + str(focus[1]) 2606 focus = str(focus[0]) + "," + str(focus[1])
2591 2607
2592 if not thumbnail is None: 2608 if not thumbnail is None:
2593 if not self.verify_minimum_version("3.2.0"): 2609 if not self.verify_minimum_version("3.2.0", cached=True):
2594 raise MastodonVersionError( 2610 raise MastodonVersionError(
2595 'Thumbnail requires version > 3.2.0') 2611 'Thumbnail requires version > 3.2.0')
2596 files["thumbnail"] = self.__load_media_file( 2612 files["thumbnail"] = self.__load_media_file(
2597 thumbnail, thumbnail_mime_type) 2613 thumbnail, thumbnail_mime_type)
2598 2614
2599 # Disambiguate URL by version 2615 # Disambiguate URL by version
2600 if self.verify_minimum_version("3.1.4"): 2616 if self.verify_minimum_version("3.1.4", cached=True):
2601 ret_dict = self.__api_request( 2617 ret_dict = self.__api_request(
2602 'POST', '/api/v2/media', files=files, params={'description': description, 'focus': focus}) 2618 'POST', '/api/v2/media', files=files, params={'description': description, 'focus': focus})
2603 else: 2619 else:
@@ -2637,7 +2653,7 @@ class Mastodon:
2637 locals(), ['id', 'thumbnail', 'thumbnail_mime_type']) 2653 locals(), ['id', 'thumbnail', 'thumbnail_mime_type'])
2638 2654
2639 if not thumbnail is None: 2655 if not thumbnail is None:
2640 if not self.verify_minimum_version("3.2.0"): 2656 if not self.verify_minimum_version("3.2.0", cached=True):
2641 raise MastodonVersionError( 2657 raise MastodonVersionError(
2642 'Thumbnail requires version > 3.2.0') 2658 'Thumbnail requires version > 3.2.0')
2643 files = {"thumbnail": self.__load_media_file( 2659 files = {"thumbnail": self.__load_media_file(
@@ -3359,8 +3375,7 @@ class Mastodon:
3359 else: 3375 else:
3360 date_time_utc = date_time.astimezone(pytz.utc) 3376 date_time_utc = date_time.astimezone(pytz.utc)
3361 3377
3362 epoch_utc = datetime.datetime.utcfromtimestamp( 3378 epoch_utc = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
3363 0).replace(tzinfo=pytz.utc)
3364 3379
3365 return (date_time_utc - epoch_utc).total_seconds() 3380 return (date_time_utc - epoch_utc).total_seconds()
3366 3381
@@ -3632,7 +3647,7 @@ class Mastodon:
3632 response_object.status_code, 3647 response_object.status_code,
3633 response_object.reason, 3648 response_object.reason,
3634 error_msg) 3649 error_msg)
3635 3650
3636 if return_response_object: 3651 if return_response_object:
3637 return response_object 3652 return response_object
3638 3653
Powered by cgit v1.2.3 (git 2.41.0)