From 0a241818ff0dbcdbe3a7257c795cce484f1bfa3b Mon Sep 17 00:00:00 2001 From: Akash Mahanty Date: Mon, 25 Jan 2021 21:11:49 +0530 Subject: [PATCH] renamed some func/meth to better names and added doc strings + lint --- waybackpy/cli.py | 1 + waybackpy/utils.py | 32 +++++++++++++++++++++----------- waybackpy/wrapper.py | 11 ++++++----- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/waybackpy/cli.py b/waybackpy/cli.py index adbf1aa..45f305a 100644 --- a/waybackpy/cli.py +++ b/waybackpy/cli.py @@ -5,6 +5,7 @@ import json import random import string import argparse + from .wrapper import Url from .exceptions import WaybackError from .__version__ import __version__ diff --git a/waybackpy/utils.py b/waybackpy/utils.py index 033c30b..5ed6886 100644 --- a/waybackpy/utils.py +++ b/waybackpy/utils.py @@ -1,12 +1,13 @@ import re import time import requests -from .exceptions import WaybackError, URLError from datetime import datetime +from .exceptions import WaybackError, URLError +from .__version__ import __version__ + from urllib3.util.retry import Retry from requests.adapters import HTTPAdapter -from .__version__ import __version__ quote = requests.utils.quote default_user_agent = "waybackpy python package - https://github.com/akamhy/waybackpy" @@ -35,7 +36,7 @@ def _latest_version(package_name, headers): return json["info"]["version"] -def _unix_ts_to_wayback_ts(unix_timestamp): +def _unix_timestamp_to_wayback_timestamp(unix_timestamp): """Returns unix timestamp converted to datetime.datetime Parameters @@ -85,18 +86,27 @@ def _add_payload(instance, payload): for i, f in enumerate(instance.collapses): payload["collapse" + str(i)] = f + # Don't need to return anything as it's dictionary. payload["url"] = instance.url -def _ts(timestamp, data): - """ - Get timestamp of last fetched archive. - If used before fetching any archive, will - use whatever self.JSON returns. +def _timestamp_manager(timestamp, data): + """Returns the timestamp. - self.timestamp is None implies that - self.JSON will return any archive's JSON - that wayback machine provides it. + Parameters + ---------- + timestamp : datetime.datetime + datetime object + + data : dict + A python dictionary, which is loaded JSON os the availability API. + + Return type: + datetime.datetime + + If timestamp is not None then sets the value to timestamp itself. + If timestamp is None the returns the value from the last fetched API data. + If not timestamp and can not read the archived_snapshots form data return datetime.max """ if timestamp: diff --git a/waybackpy/wrapper.py b/waybackpy/wrapper.py index eddb3ac..abbd7d2 100644 --- a/waybackpy/wrapper.py +++ b/waybackpy/wrapper.py @@ -1,5 +1,6 @@ import re from datetime import datetime, timedelta + from .exceptions import WaybackError from .cdx import Cdx from .utils import ( @@ -9,8 +10,8 @@ from .utils import ( default_user_agent, _url_check, _cleaned_url, - _ts, - _unix_ts_to_wayback_ts, + _timestamp_manager, + _unix_timestamp_to_wayback_timestamp, _latest_version, ) @@ -121,8 +122,8 @@ class Url: @property def _timestamp(self): - self.timestamp = _ts(self.timestamp, self.JSON) - return self.timestamp + """Sets the value of self.timestamp if still not set.""" + return _timestamp_manager(self.timestamp, self.JSON) def save(self): """ @@ -237,7 +238,7 @@ class Url: """ if unix_timestamp: - timestamp = _unix_ts_to_wayback_ts(unix_timestamp) + timestamp = _unix_timestamp_to_wayback_timestamp(unix_timestamp) else: now = datetime.utcnow().timetuple() timestamp = _wayback_timestamp(