From e9a829830aec64d9051d09521235277ed45bd2f7 Mon Sep 17 00:00:00 2001 From: Harmon Date: Thu, 28 Oct 2021 10:17:50 -0500 Subject: [PATCH] Add and use parse_datetime utility function for API v2 models --- tweepy/poll.py | 9 +++------ tweepy/space.py | 19 +++++-------------- tweepy/tweet.py | 7 ++----- tweepy/user.py | 7 ++----- tweepy/utils.py | 8 ++++++++ 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/tweepy/poll.py b/tweepy/poll.py index 77137db..affd49b 100644 --- a/tweepy/poll.py +++ b/tweepy/poll.py @@ -2,9 +2,8 @@ # Copyright 2009-2021 Joshua Roesslein # See LICENSE for details. -import datetime - from tweepy.mixins import DataMapping, HashableID +from tweepy.utils import parse_datetime class Poll(HashableID, DataMapping): @@ -23,10 +22,8 @@ class Poll(HashableID, DataMapping): self.end_datetime = data.get("end_datetime") if self.end_datetime is not None: - self.end_datetime = datetime.datetime.strptime( - self.end_datetime, "%Y-%m-%dT%H:%M:%S.%f%z" - ) - + self.end_datetime = parse_datetime(self.end_datetime) + self.voting_status = data.get("voting_status") def __iter__(self): diff --git a/tweepy/space.py b/tweepy/space.py index aaa72d1..f1fbcd9 100644 --- a/tweepy/space.py +++ b/tweepy/space.py @@ -2,9 +2,8 @@ # Copyright 2009-2021 Joshua Roesslein # See LICENSE for details. -import datetime - from tweepy.mixins import DataMapping, HashableID +from tweepy.utils import parse_datetime class Space(HashableID, DataMapping): @@ -22,9 +21,7 @@ class Space(HashableID, DataMapping): self.created_at = data.get("created_at") if self.created_at is not None: - self.created_at = datetime.datetime.strptime( - self.created_at, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + self.created_at = parse_datetime(self.created_at) self.host_ids = data.get("host_ids", []) self.lang = data.get("lang") @@ -34,25 +31,19 @@ class Space(HashableID, DataMapping): self.scheduled_start = data.get("scheduled_start") if self.scheduled_start is not None: - self.scheduled_start = datetime.datetime.strptime( - self.scheduled_start, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + self.scheduled_start = parse_datetime(self.scheduled_start) self.speaker_ids = data.get("speaker_ids", []) self.started_at = data.get("started_at") if self.started_at is not None: - self.started_at = datetime.datetime.strptime( - self.started_at, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + self.started_at = parse_datetime(self.started_at) self.title = data.get("title") self.updated_at = data.get("updated_at") if self.updated_at is not None: - self.updated_at = datetime.datetime.strptime( - self.updated_at, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + self.updated_at = parse_datetime(self.updated_at) def __repr__(self): return f"" diff --git a/tweepy/tweet.py b/tweepy/tweet.py index d4aa568..e3d5b48 100644 --- a/tweepy/tweet.py +++ b/tweepy/tweet.py @@ -2,9 +2,8 @@ # Copyright 2009-2021 Joshua Roesslein # See LICENSE for details. -import datetime - from tweepy.mixins import DataMapping, HashableID +from tweepy.utils import parse_datetime class Tweet(HashableID, DataMapping): @@ -37,9 +36,7 @@ class Tweet(HashableID, DataMapping): self.created_at = data.get("created_at") if self.created_at is not None: - self.created_at = datetime.datetime.strptime( - self.created_at, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + self.created_at = parse_datetime(self.created_at) self.entities = data.get("entities") self.geo = data.get("geo") diff --git a/tweepy/user.py b/tweepy/user.py index 9d80cff..20d4776 100644 --- a/tweepy/user.py +++ b/tweepy/user.py @@ -2,9 +2,8 @@ # Copyright 2009-2021 Joshua Roesslein # See LICENSE for details. -import datetime - from tweepy.mixins import DataMapping, HashableID +from tweepy.utils import parse_datetime class User(HashableID, DataMapping): @@ -23,9 +22,7 @@ class User(HashableID, DataMapping): self.created_at = data.get("created_at") if self.created_at is not None: - self.created_at = datetime.datetime.strptime( - self.created_at, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + self.created_at = parse_datetime(self.created_at) self.description = data.get("description") self.entities = data.get("entities") diff --git a/tweepy/utils.py b/tweepy/utils.py index f93d14e..f11bcd0 100644 --- a/tweepy/utils.py +++ b/tweepy/utils.py @@ -2,7 +2,15 @@ # Copyright 2010-2021 Joshua Roesslein # See LICENSE for details. +import datetime + def list_to_csv(item_list): if item_list: return ','.join(map(str, item_list)) + + +def parse_datetime(datetime_string): + return datetime.datetime.strptime( + datetime_string, "%Y-%m-%dT%H:%M:%S.%f%z" + ) -- 2.25.1