From 37ad50b51322bb06940209d56c66952de48c843a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org>
Date: Thu, 13 Jul 2023 22:36:10 +0900
Subject: [PATCH] cut off after 255 characters, remove last settings reference

---
 .../management/commands/update_instagram.py     |  5 +----
 instagram_utils/services.py                     | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/instagram_utils/management/commands/update_instagram.py b/instagram_utils/management/commands/update_instagram.py
index 11c2c130..fe532e24 100644
--- a/instagram_utils/management/commands/update_instagram.py
+++ b/instagram_utils/management/commands/update_instagram.py
@@ -6,10 +6,7 @@ from ...services import InstagramDownloadService
 
 class Command(BaseCommand):
     def handle(self, *args, **options):
-        service = InstagramDownloadService(
-            app_id=settings.INSTAGRAM_APP_ID,
-            app_secret=settings.INSTAGRAM_APP_SECRET,
-        )
+        service = InstagramDownloadService()
         service.perform_update()
 
         self.stdout.write("\nInstagram post update finished.")
diff --git a/instagram_utils/services.py b/instagram_utils/services.py
index f7bdb0f8..2ed40210 100644
--- a/instagram_utils/services.py
+++ b/instagram_utils/services.py
@@ -46,7 +46,15 @@ class InstagramDownloadService:
 
         profile = instaloader.Profile.from_username(loader.context, username)
 
+        post_position = 0
+
         for remote_post in profile.get_posts():
+            if post_position == 64:
+                # Don't go past 64 saved posts
+                return
+
+            post_position += 1
+
             if remote_post.is_video:
                 logger.info(
                     "Instagram post ID %s is a video, skipping.", remote_post.shortcode
@@ -61,12 +69,17 @@ class InstagramDownloadService:
 
                 continue
 
+            caption = remote_post.caption
+
+            if len(caption) > 255:
+                caption = caption[:255] + "..."
+
             local_post_instance = InstagramPost(
                 remote_id=remote_post.shortcode,
                 author_name=profile.full_name,
                 author_username=profile.username,
                 timestamp=remote_post.date_local,
-                caption=remote_post.caption,
+                caption=caption,
                 url=f"https://instagram.com/p/{remote_post.shortcode}",
             )
 
@@ -82,8 +95,6 @@ class InstagramDownloadService:
                 remote_post.shortcode,
             )
 
-            post_position += 1
-
     def perform_update(self) -> None:
         for username in self.get_usernames():
             self.parse_media_for_user(username)
-- 
GitLab