Add and use parse_datetime utility function for API v2 models
authorHarmon <Harmon758@gmail.com>
Thu, 28 Oct 2021 15:17:50 +0000 (10:17 -0500)
committerHarmon <Harmon758@gmail.com>
Thu, 28 Oct 2021 15:17:50 +0000 (10:17 -0500)
tweepy/poll.py
tweepy/space.py
tweepy/tweet.py
tweepy/user.py
tweepy/utils.py

index 77137dba79606277fcc6c5b42e9cb2b9877ec976..affd49b359e0d40b1cab146edcd3464393eff4a2 100644 (file)
@@ -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):
index aaa72d117c1755181a42b63314aa9374192f878f..f1fbcd9b7b473072cb4baae40e42c3fe2079b603 100644 (file)
@@ -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"<Space id={self.id} state={self.state}>"
index d4aa568fdc05329d17f0f830cbe1c0556716482e..e3d5b4895df0181e95a45dd6769454d948d84ad3 100644 (file)
@@ -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")
index 9d80cff8fd948e55c60a2cb4f98c84c27111090c..20d4776c406d989af1dc41aebdb10a1173f52a5e 100644 (file)
@@ -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")
index f93d14ec9cf2f97f49b08c5f140aba20b2914eb2..f11bcd006a97c64e37ac6351bd634d08886dad26 100644 (file)
@@ -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"
+    )