Skip to content

Commit 07523b7

Browse files
committed
Add catchup argument
Signed-off-by: Ryan Luu <[email protected]>
1 parent 3e81851 commit 07523b7

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/instawebhooks/__main__.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ def closure_check_regex(arg_value):
9494
action="store_true",
9595
)
9696
parser.add_argument("--version", action="version", version="%(prog)s " + version)
97+
parser.add_argument(
98+
"-p",
99+
"--catchup",
100+
help="send the last latest posts on startup regardless of time",
101+
type=int,
102+
default=0,
103+
)
97104
args = parser.parse_args()
98105

99106
# Set the logger to debug if verbose is enabled
@@ -211,7 +218,7 @@ async def send_to_discord(post: Post):
211218
logger.info("New post sent to Discord successfully.")
212219

213220

214-
async def check_for_new_posts():
221+
async def check_for_new_posts(catchup: int = args.catchup):
215222
"""Check for new Instagram posts and send them to Discord"""
216223

217224
logger.info("Checking for new posts")
@@ -225,12 +232,27 @@ async def check_for_new_posts():
225232

226233
new_posts_found = False
227234

235+
async def send_post(post: Post):
236+
logger.info("New post found: https://www.instagram.com/p/%s", post.shortcode)
237+
await send_to_discord(post)
238+
239+
if catchup > 0:
240+
logger.info("Sending last %s posts on startup...", catchup)
241+
posts_to_send = []
242+
for post in takewhile(lambda _: catchup > 0, posts):
243+
posts_to_send.append(post)
244+
catchup -= 1
245+
246+
# Reverse the posts to send oldest first
247+
for post in reversed(posts_to_send):
248+
await send_post(post)
249+
sleep(2) # Avoid 30 requests per minute rate limit
250+
228251
for post in takewhile(
229252
lambda p: p.date > until, dropwhile(lambda p: p.date > since, posts)
230253
):
231254
new_posts_found = True
232-
logger.info("New post found: https://www.instagram.com/p/%s", post.shortcode)
233-
await send_to_discord(post)
255+
await send_post(post)
234256
sleep(2) # Avoid 30 requests per minute rate limit
235257

236258
if not new_posts_found:

0 commit comments

Comments
 (0)