support unix ts as an arg in near

This commit is contained in:
Akash Mahanty 2021-01-11 19:53:37 +05:30
parent 4693dbf9c1
commit 5a7bd73565
2 changed files with 17 additions and 9 deletions

View File

@ -11,6 +11,9 @@ quote = requests.utils.quote
default_user_agent = "waybackpy python package - https://github.com/akamhy/waybackpy" default_user_agent = "waybackpy python package - https://github.com/akamhy/waybackpy"
def _unix_ts_to_wayback_ts(unix_ts):
return datetime.utcfromtimestamp(int(unix_ts)).strftime('%Y%m%d%H%M%S')
def _add_payload(self, payload): def _add_payload(self, payload):
if self.start_timestamp: if self.start_timestamp:
payload["from"] = self.start_timestamp payload["from"] = self.start_timestamp

View File

@ -11,6 +11,7 @@ from .utils import (
_url_check, _url_check,
_cleaned_url, _cleaned_url,
_ts, _ts,
_unix_ts_to_wayback_ts,
) )
@ -165,7 +166,7 @@ class Url:
return response.content.decode(encoding.replace("text/html", "UTF-8", 1)) return response.content.decode(encoding.replace("text/html", "UTF-8", 1))
def near(self, year=None, month=None, day=None, hour=None, minute=None): def near(self, year=None, month=None, day=None, hour=None, minute=None, unix_timestamp=None):
""" """
Wayback Machine can have many archives of a webpage, Wayback Machine can have many archives of a webpage,
sometimes we want archive close to a specific time. sometimes we want archive close to a specific time.
@ -187,14 +188,18 @@ class Url:
And finally return self. And finally return self.
""" """
now = datetime.utcnow().timetuple()
timestamp = _wayback_timestamp( if unix_timestamp:
year=year if year else now.tm_year, timestamp = _unix_ts_to_wayback_ts(unix_timestamp)
month=month if month else now.tm_mon, else:
day=day if day else now.tm_mday, now = datetime.utcnow().timetuple()
hour=hour if hour else now.tm_hour, timestamp = _wayback_timestamp(
minute=minute if minute else now.tm_min, year=year if year else now.tm_year,
) month=month if month else now.tm_mon,
day=day if day else now.tm_mday,
hour=hour if hour else now.tm_hour,
minute=minute if minute else now.tm_min,
)
endpoint = "https://archive.org/wayback/available" endpoint = "https://archive.org/wayback/available"
headers = {"User-Agent": self.user_agent} headers = {"User-Agent": self.user_agent}