From 07523b7acab63847e8f09c739c044fc97630127c Mon Sep 17 00:00:00 2001 From: Ryan Luu Date: Tue, 3 Dec 2024 09:37:21 -0800 Subject: [PATCH 1/4] Add catchup argument Signed-off-by: Ryan Luu --- src/instawebhooks/__main__.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/instawebhooks/__main__.py b/src/instawebhooks/__main__.py index 56474dc..0978a62 100644 --- a/src/instawebhooks/__main__.py +++ b/src/instawebhooks/__main__.py @@ -94,6 +94,13 @@ def closure_check_regex(arg_value): action="store_true", ) parser.add_argument("--version", action="version", version="%(prog)s " + version) +parser.add_argument( + "-p", + "--catchup", + help="send the last latest posts on startup regardless of time", + type=int, + default=0, +) args = parser.parse_args() # Set the logger to debug if verbose is enabled @@ -211,7 +218,7 @@ async def send_to_discord(post: Post): logger.info("New post sent to Discord successfully.") -async def check_for_new_posts(): +async def check_for_new_posts(catchup: int = args.catchup): """Check for new Instagram posts and send them to Discord""" logger.info("Checking for new posts") @@ -225,12 +232,27 @@ async def check_for_new_posts(): new_posts_found = False + async def send_post(post: Post): + logger.info("New post found: https://www.instagram.com/p/%s", post.shortcode) + await send_to_discord(post) + + if catchup > 0: + logger.info("Sending last %s posts on startup...", catchup) + posts_to_send = [] + for post in takewhile(lambda _: catchup > 0, posts): + posts_to_send.append(post) + catchup -= 1 + + # Reverse the posts to send oldest first + for post in reversed(posts_to_send): + await send_post(post) + sleep(2) # Avoid 30 requests per minute rate limit + for post in takewhile( lambda p: p.date > until, dropwhile(lambda p: p.date > since, posts) ): new_posts_found = True - logger.info("New post found: https://www.instagram.com/p/%s", post.shortcode) - await send_to_discord(post) + await send_post(post) sleep(2) # Avoid 30 requests per minute rate limit if not new_posts_found: From d87504410c3c07322d08fe4fa506645d0bbc715d Mon Sep 17 00:00:00 2001 From: Ryan Luu Date: Tue, 3 Dec 2024 17:45:19 +0000 Subject: [PATCH 2/4] Fix type --- src/instawebhooks/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/instawebhooks/__main__.py b/src/instawebhooks/__main__.py index 314e51d..82fcaa8 100644 --- a/src/instawebhooks/__main__.py +++ b/src/instawebhooks/__main__.py @@ -232,7 +232,7 @@ async def send_post(post: Post): if catchup > 0: logger.info("Sending last %s posts on startup...", catchup) - posts_to_send = [] + posts_to_send: list[Post] = [] for post in takewhile(lambda _: catchup > 0, posts): posts_to_send.append(post) catchup -= 1 From 6613d49b040c2a04ac1fa5f397fb9dd7f32c8bf4 Mon Sep 17 00:00:00 2001 From: Ryan Luu Date: Tue, 3 Dec 2024 17:48:01 +0000 Subject: [PATCH 3/4] Import type --- src/instawebhooks/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/instawebhooks/__main__.py b/src/instawebhooks/__main__.py index 82fcaa8..177690f 100644 --- a/src/instawebhooks/__main__.py +++ b/src/instawebhooks/__main__.py @@ -7,10 +7,10 @@ import re import sys from argparse import ArgumentParser -from typing import Dict from datetime import datetime, timedelta from itertools import dropwhile, takewhile from time import sleep +from typing import Dict, List try: from aiohttp import ClientSession @@ -232,7 +232,7 @@ async def send_post(post: Post): if catchup > 0: logger.info("Sending last %s posts on startup...", catchup) - posts_to_send: list[Post] = [] + posts_to_send: List[Post] = [] for post in takewhile(lambda _: catchup > 0, posts): posts_to_send.append(post) catchup -= 1 From 0f6e0375e9045af7792117c9a6972745f354a001 Mon Sep 17 00:00:00 2001 From: Ryan Luu Date: Fri, 27 Dec 2024 08:24:01 +0000 Subject: [PATCH 4/4] Add back arg Signed-off-by: GitHub --- src/instawebhooks/parser.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/instawebhooks/parser.py b/src/instawebhooks/parser.py index 8753c1a..7f153a2 100644 --- a/src/instawebhooks/parser.py +++ b/src/instawebhooks/parser.py @@ -44,6 +44,13 @@ def closure_check_regex(arg_value: str): ) group.add_argument("-q", "--quiet", help="hide all logging", action="store_true") group.add_argument("-v", "--verbose", help="show debug logging", action="store_true") +parser.add_argument( + "-p", + "--catchup", + help="send the last latest posts on startup regardless of time", + type=int, + default=0, +) parser.add_argument( "-i", "--refresh-interval",