edgi-govdata-archiving / wayback

A Python API to the Internet Archive Wayback Machine
https://wayback.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
61 stars 12 forks source link

Support `datetime.timedelta` for `target_window` in `get_memento()` #55

Open Mr0grog opened 3 years ago

Mr0grog commented 3 years ago

The WaybackClient.get_memento() method has a target_window parameter, which is used when a memento does not exist at the requested time. If the parameter exact=False, get_memento() returns the nearest-in-time memento, but only if it is within the number of seconds specified in target_window.

https://github.com/edgi-govdata-archiving/wayback/blob/c066e04c1ba11c26febac574b37ee739cc5796e2/wayback/_client.py#L717-L719

Since Python has a built in datetime.timedelta type to represent exactly this kind of concept, we should also accept it instead of an integer for target_window. (Really, I’m not sure why we didn’t do this originally!)

Mr0grog commented 3 years ago

This is useful because it provides a more readable way to set timeframes. For example:

# 4 hour window as an integer:
get_memento(url, exact=False, target_window=4 * 60 * 60)

# 4 hour window as a timedelta:
get_memento(url, exact=False, target_window=timedelta(hours=4))