@@ -191,6 +191,88 @@ def add_payload(self, payload: Dict[str, str]) -> None:
191
191
192
192
payload ["url" ] = self .url
193
193
194
+ def before (
195
+ self ,
196
+ year : Optional [int ] = None ,
197
+ month : Optional [int ] = None ,
198
+ day : Optional [int ] = None ,
199
+ hour : Optional [int ] = None ,
200
+ minute : Optional [int ] = None ,
201
+ unix_timestamp : Optional [int ] = None ,
202
+ wayback_machine_timestamp : Optional [Union [int , str ]] = None ,
203
+ ) -> CDXSnapshot :
204
+ """
205
+ Gets the nearest archive before the given datetime.
206
+ """
207
+ if unix_timestamp :
208
+ timestamp = unix_timestamp_to_wayback_timestamp (unix_timestamp )
209
+ elif wayback_machine_timestamp :
210
+ timestamp = str (wayback_machine_timestamp )
211
+ else :
212
+ now = datetime .utcnow ().timetuple ()
213
+ timestamp = wayback_timestamp (
214
+ year = now .tm_year if year is None else year ,
215
+ month = now .tm_mon if month is None else month ,
216
+ day = now .tm_mday if day is None else day ,
217
+ hour = now .tm_hour if hour is None else hour ,
218
+ minute = now .tm_min if minute is None else minute ,
219
+ )
220
+ self .closest = timestamp
221
+ self .sort = "closest"
222
+ self .limit = 25000
223
+ for snapshot in self .snapshots ():
224
+ if snapshot .timestamp < timestamp :
225
+ return snapshot
226
+
227
+ # If a snapshot isn't returned, then none were found.
228
+ raise NoCDXRecordFound (
229
+ "No records were found before the given date for the query."
230
+ + "Either there are no archives before the given date,"
231
+ + " the URL may not have any archived, or the URL may have been"
232
+ + " recently archived and is still not available on the CDX server."
233
+ )
234
+
235
+ def after (
236
+ self ,
237
+ year : Optional [int ] = None ,
238
+ month : Optional [int ] = None ,
239
+ day : Optional [int ] = None ,
240
+ hour : Optional [int ] = None ,
241
+ minute : Optional [int ] = None ,
242
+ unix_timestamp : Optional [int ] = None ,
243
+ wayback_machine_timestamp : Optional [Union [int , str ]] = None ,
244
+ ) -> CDXSnapshot :
245
+ """
246
+ Gets the nearest archive after the given datetime.
247
+ """
248
+ if unix_timestamp :
249
+ timestamp = unix_timestamp_to_wayback_timestamp (unix_timestamp )
250
+ elif wayback_machine_timestamp :
251
+ timestamp = str (wayback_machine_timestamp )
252
+ else :
253
+ now = datetime .utcnow ().timetuple ()
254
+ timestamp = wayback_timestamp (
255
+ year = now .tm_year if year is None else year ,
256
+ month = now .tm_mon if month is None else month ,
257
+ day = now .tm_mday if day is None else day ,
258
+ hour = now .tm_hour if hour is None else hour ,
259
+ minute = now .tm_min if minute is None else minute ,
260
+ )
261
+ self .closest = timestamp
262
+ self .sort = "closest"
263
+ self .limit = 25000
264
+ for snapshot in self .snapshots ():
265
+ if snapshot .timestamp > timestamp :
266
+ return snapshot
267
+
268
+ # If a snapshot isn't returned, then none were found.
269
+ raise NoCDXRecordFound (
270
+ "No records were found after the given date for the query."
271
+ + "Either there are no archives after the given date,"
272
+ + " the URL may not have any archives, or the URL may have been"
273
+ + " recently archived and is still not available on the CDX server."
274
+ )
275
+
194
276
def near (
195
277
self ,
196
278
year : Optional [int ] = None ,
0 commit comments