Skip to content

Commit b1ac762

Browse files
committed
Removed post validation and improved message logging
1 parent 5f15de4 commit b1ac762

2 files changed

Lines changed: 23 additions & 36 deletions

File tree

create_posts.py

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
# Create posts from downloaded posts
22
import os
3-
import psutil
43
import pytesseract
54
from openai import OpenAI
65
from config import DEVELOPER_PROMPT
76
from PIL import Image, ImageDraw, ImageFont
87

8+
9+
def is_mostly_black(image_path, threshold=30, ratio=0.9):
10+
img = Image.open(image_path).convert("L") # Convert to grayscale
11+
pixels = list(img.getdata())
12+
13+
dark_pixels = sum(p < threshold for p in pixels)
14+
total_pixels = len(pixels)
15+
16+
return dark_pixels / total_pixels >= ratio
17+
18+
919
def wrap_text(text, font, max_width, draw):
1020
"""
1121
Wraps text to fit within the max_width.
@@ -112,40 +122,12 @@ def create_posts(downloaded_posts):
112122
# Draw multiline text.
113123
draw.multiline_text((x, y), description, font=font, fill="white", spacing=4)
114124

115-
# Show the image for validation
116-
img.show()
117-
118-
# Validation prompt
119-
print(f"\n--- Post Validation for {post['shortcode']} ---")
120-
print("Original text:", post_text)
121-
122-
while True:
123-
user_choice = input("\nDo you want to save this post? (y/n/q to quit): ").lower().strip()
124-
125-
if user_choice in ['y', 'yes']:
126-
# Save the post
127-
img.save("new_posts/" + post['shortcode'] + '.png')
128-
129-
# Save caption
130-
with open("new_posts/" + post['shortcode'] + '.txt', 'w') as f:
131-
f.write(translated_caption_text)
132-
133-
print(f"✅ Post saved: {post['shortcode']}.png")
134-
break
135-
elif user_choice in ['n', 'no']:
136-
# Skip post
137-
print(f"❌ Post skipped: {post['shortcode']}")
138-
break
139-
elif user_choice in ['q', 'quit']:
140-
print("Exiting post creation...")
141-
return
142-
else:
143-
print("Please enter 'y' for yes, 'n' for no, or 'q' to quit.")
144-
145-
# Close images by PID
146-
for proc in psutil.process_iter():
147-
if proc.name() == r"display-im6.q16":
148-
proc.kill()
149-
break
125+
if is_mostly_black(image_path):
126+
# Save the post
127+
img.save("new_posts/" + post['shortcode'] + '.png')
128+
# Save caption
129+
with open("new_posts/" + post['shortcode'] + '.txt', 'w') as f:
130+
f.write(translated_caption_text)
150131

132+
print(f"Created post for {post['shortcode']}")
151133
print("-" * 50)

download_posts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ def download_posts():
5252

5353
print("Login successful, session saved")
5454

55+
print("-" * 50)
56+
5557
# Get profile
5658
profile = instaloader.Profile.from_username(L.context, TARGET_USERNAME)
5759
print(f"Profile: {profile.username}, Posts: {profile.mediacount}")
5860

61+
print("-" * 50)
5962
# Get posts
6063
posts = profile.get_posts()
6164
new_posts = []
@@ -99,6 +102,7 @@ def download_posts():
99102
except Exception as e:
100103
print(f"Error downloading {post.shortcode}: {e}")
101104
continue
105+
print("-" * 50)
102106

103107
# Update tracking file
104108
tracking_data = {
@@ -111,4 +115,5 @@ def download_posts():
111115
json.dump(tracking_data, f, indent=2)
112116

113117
print(f"Downloaded {len(new_posts)} new posts")
118+
print("-" * 50)
114119
return new_posts

0 commit comments

Comments
 (0)