Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add catchup argument #19

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/instawebhooks/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime, timedelta
from itertools import dropwhile, takewhile
from time import sleep
from typing import Dict
from typing import Dict, List

from .parser import parser

Expand All @@ -32,6 +32,7 @@
level=logging.INFO,
)


args = parser.parse_args()

# Set the logger to debug if verbose is enabled
Expand Down Expand Up @@ -148,7 +149,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")
Expand All @@ -162,12 +163,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: List[Post] = []
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:
Expand Down
7 changes: 7 additions & 0 deletions src/instawebhooks/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading