Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Rekt-Developer authored Dec 3, 2024
1 parent ce834b4 commit be67e80
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Config:
BOT_TOKEN: str = os.getenv("BOT_TOKEN", "")
CHANNEL_USERNAME: str = os.getenv("CHANNEL_USERNAME", "")
POSTS_FILE: str = os.getenv("POSTS_FILE", "public/data/posts.json")
MEDIA_DIR: str = os.getenv("MEDIA_DIR", "public/media")
FETCH_HOURS_BACK: int = int(os.getenv("FETCH_HOURS_BACK", "48"))
MAX_POSTS: int = int(os.getenv("MAX_POSTS", "10"))

Expand Down Expand Up @@ -142,8 +141,8 @@ def save_posts(posts: List[Dict[str, str]]) -> None:
# Ensure directory exists with parents
os.makedirs(os.path.dirname(Config.POSTS_FILE), exist_ok=True)

# Create a dictionary with post_id as key for easier lookup
posts_dict = {post['post_id']: post for post in posts}
# Always create a valid JSON file, even if empty
posts_dict = {post['post_id']: post for post in posts} if posts else {}

with open(Config.POSTS_FILE, "w", encoding="utf-8") as file:
json.dump(posts_dict, file, indent=4, ensure_ascii=False)
Expand Down Expand Up @@ -171,9 +170,10 @@ async def main() -> None:
bot = application.bot
posts = await fetch_channel_posts(bot)

if posts:
save_posts(posts)
else:
# Always save posts (will create an empty JSON if no posts found)
save_posts(posts)

if not posts:
logger.warning("No new posts found.")

except Exception as e:
Expand Down

0 comments on commit be67e80

Please sign in to comment.