Skip to content

Commit

Permalink
Add catchup argument
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Luu <[email protected]>
  • Loading branch information
RyanLua committed Dec 3, 2024
1 parent 3e81851 commit 07523b7
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/instawebhooks/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit 07523b7

Please sign in to comment.