1818
1919import os
2020from dataclasses import dataclass
21- from typing import Any , List , Optional
21+ from typing import Any , Bool , List , Optional
2222
23+ import datetime
2324import structlog
2425import urlcanon
2526
@@ -83,7 +84,24 @@ def _execute_pg_query(self, query_tuple, fetchall=False) -> Optional[Any]:
8384 logger .warn ("postgres query failed: %s" , e )
8485 return None
8586
86- def get_recent_video_capture (self , site = None , containing_page_url = None ) -> List :
87+ def _timestamp4datetime (timestamp ):
88+ """split `timestamp` into a tuple of 6 integers.
89+
90+ :param timestamp: full-length timestamp
91+ """
92+ timestamp = timestamp [:14 ]
93+ return (
94+ int (timestamp [:- 10 ]),
95+ int (timestamp [- 10 :- 8 ]),
96+ int (timestamp [- 8 :- 6 ]),
97+ int (timestamp [- 6 :- 4 ]),
98+ int (timestamp [- 4 :- 2 ]),
99+ int (timestamp [- 2 :]),
100+ )
101+
102+ def recent_video_capture_exists (
103+ self , site = None , containing_page_url = None , recent = 30
104+ ) -> Bool :
87105 # using ait_account_id as postgres partition id
88106 partition_id = (
89107 site ["metadata" ]["ait_account_id" ]
@@ -93,7 +111,7 @@ def get_recent_video_capture(self, site=None, containing_page_url=None) -> List:
93111 seed_id = (
94112 site ["metadata" ]["ait_seed_id" ] if site ["metadata" ]["ait_seed_id" ] else None
95113 )
96- result = None
114+ result = False
97115
98116 if partition_id and seed_id and containing_page_url :
99117 # check for postgres query for most recent record
@@ -105,7 +123,20 @@ def get_recent_video_capture(self, site=None, containing_page_url=None) -> List:
105123 result_tuple = self ._execute_pg_query (pg_query )
106124 if result_tuple :
107125 result = result_tuple [0 ]
108- logger .info ("found most recent video capture record: %s" , result )
126+ logger .info ("found most recent capture timestamp: %s" , result )
127+ capture_timestamp = datetime .datetime (
128+ * self ._timestamp4datetime (result )
129+ )
130+ time_diff = (
131+ datetime .datetime .now (datetime .timezone .utc )()
132+ - capture_timestamp
133+ )
134+ if time_diff < datetime .timedelta (recent ):
135+ logger .info (
136+ "recent video capture from %s exists" ,
137+ containing_page_url ,
138+ )
139+ result = True
109140
110141 except Exception as e :
111142 logger .warn ("postgres query failed: %s" , e )
0 commit comments