From: Claromes Date: Sat, 15 Jun 2024 22:48:41 +0000 (-0300) Subject: add WaybackTweets class default values, update cli, docs X-Git-Url: https://git.claromes.com/?a=commitdiff_plain;h=065c7a22cad4a5f02240c5c8f820454e27295496;p=waybacktweets.git add WaybackTweets class default values, update cli, docs --- diff --git a/docs/api.rst b/docs/api.rst index 820cbd8..8220857 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -56,5 +56,4 @@ Utils .. autofunction:: delete_tweet_pathnames .. autofunction:: is_tweet_url .. autofunction:: get_response -.. autofunction:: parse_date .. autofunction:: semicolon_parser diff --git a/waybacktweets/api/request_tweets.py b/waybacktweets/api/request_tweets.py index 83f0405..b00925a 100644 --- a/waybacktweets/api/request_tweets.py +++ b/waybacktweets/api/request_tweets.py @@ -16,18 +16,18 @@ class WaybackTweets: :param timestamp_to: The timestamp to stop retrieving tweets at. :param limit: The maximum number of results to return. :param offset: The number of lines to skip in the results. - :param matchType: Results matching a certain prefix, a certain host or all subdomains. # noqa: E501 - """ + :param matchType: Results matching a certain prefix, a certain host or all subdomains. + """ # noqa: E501 def __init__( self, username: str, - collapse: str, - timestamp_from: str, - timestamp_to: str, - limit: int, - offset: int, - matchtype: str, + collapse: str = None, + timestamp_from: str = None, + timestamp_to: str = None, + limit: int = None, + offset: int = None, + matchtype: str = None, ): self.username = username self.collapse = collapse diff --git a/waybacktweets/cli/main.py b/waybacktweets/cli/main.py index 987bac2..19fbdcb 100644 --- a/waybacktweets/cli/main.py +++ b/waybacktweets/cli/main.py @@ -1,8 +1,9 @@ """ -CLI function for retrieving archived tweets. +CLI functions for retrieving archived tweets. """ -from typing import Optional +from datetime import datetime +from typing import Any, Optional import click from requests import exceptions @@ -11,7 +12,30 @@ from rich import print as rprint from waybacktweets.api.export_tweets import TweetsExporter from waybacktweets.api.parse_tweets import TweetsParser from waybacktweets.api.request_tweets import WaybackTweets -from waybacktweets.utils.utils import parse_date + + +def parse_date( + ctx: Optional[Any] = None, param: Optional[Any] = None, value: Optional[str] = None +) -> Optional[str]: + """ + Parses a date string and returns it in the format "YYYYMMDD". + + :param ctx: Necessary when used with the click package. Defaults to None. + :param param: Necessary when used with the click package. Defaults to None. + :param value: A date string in the "YYYYMMDD" format. Defaults to None. + + :returns: The input date string formatted in the "YYYYMMDD" format, + or None if no date string was provided. + """ + try: + if value is None: + return None + + date = datetime.strptime(value, "%Y%m%d") + + return date.strftime("%Y%m%d") + except ValueError: + raise click.BadParameter("Date must be in format YYYYmmdd") @click.command() @@ -54,7 +78,7 @@ from waybacktweets.utils.utils import parse_date "--matchtype", type=click.Choice(["exact", "prefix", "host", "domain"], case_sensitive=False), default=None, - help="Results matching a certain prefix, a certain host or all subdomains. Default: exact", # noqa: E501 + help="Results matching a certain prefix, a certain host or all subdomains.", # noqa: E501 ) def cli( username: str, diff --git a/waybacktweets/utils/utils.py b/waybacktweets/utils/utils.py index 5163b33..cf5fe93 100644 --- a/waybacktweets/utils/utils.py +++ b/waybacktweets/utils/utils.py @@ -3,10 +3,8 @@ Module containing utility functions for handling HTTP requests and manipulating """ import re -from datetime import datetime -from typing import Any, Optional +from typing import Optional -import click import requests from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry @@ -157,30 +155,6 @@ def semicolon_parser(string: str) -> str: return "".join("%3B" if c == ";" else c for c in string) -def parse_date( - ctx: Optional[Any] = None, param: Optional[Any] = None, value: Optional[str] = None -) -> Optional[str]: - """ - Parses a date string and returns it in the format "YYYYMMDD". - - :param ctx: Necessary when used with the click package. Defaults to None. - :param param: Necessary when used with the click package. Defaults to None. - :param value: A date string in the "YYYYMMDD" format. Defaults to None. - - :returns: The input date string formatted in the "YYYYMMDD" format, - or None if no date string was provided. - """ - try: - if value is None: - return None - - date = datetime.strptime(value, "%Y%m%d") - - return date.strftime("%Y%m%d") - except ValueError: - raise click.BadParameter("Date must be in format YYYYmmdd") - - def is_tweet_url(twitter_url: str) -> bool: """ Checks if the provided URL is a Twitter status URL.