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 3dd6319 commit bc16d0a
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from datetime import datetime, timedelta
import asyncio
import requests
from telegram import Update
from telegram.ext import Application
from telegram.error import TelegramError

Expand All @@ -24,23 +23,21 @@

def validate_environment_variables():
"""
Validate required environment variables.
Validate required environment variables with comprehensive error checking.
"""
missing_vars = []
if not BOT_TOKEN:
missing_vars.append("BOT_TOKEN")
if not CHANNEL_ID:
missing_vars.append("CHANNEL_ID")
if not CHANNEL_USERNAME:
missing_vars.append("CHANNEL_USERNAME")
if not CHANNEL_ID and not CHANNEL_USERNAME:
missing_vars.append("CHANNEL_ID or CHANNEL_USERNAME")
if missing_vars:
error_msg = f"Missing required environment variables: {', '.join(missing_vars)}"
logger.error(error_msg)
raise ValueError(error_msg)

def sanitize_text(text):
"""
Sanitize and truncate text content.
Sanitize and clean text content for safe storage and display.
"""
if not text:
return ""
Expand All @@ -49,7 +46,7 @@ def sanitize_text(text):

async def download_media(bot, file_id, file_type):
"""
Download media from Telegram.
Download media from Telegram and save locally.
"""
try:
media_dir = "public/media"
Expand All @@ -58,37 +55,49 @@ async def download_media(bot, file_id, file_type):
file_path = os.path.join(media_dir, f"{file_id}.{file_type}")
response = requests.get(file.file_path)
if response.status_code == 200:
with open(file_path, "wb") as f:
with open(file_path, 'wb') as f:
f.write(response.content)
return file_path.replace("public/", "")
logger.warning(f"Failed to download media. Status code: {response.status_code}")
return file_path.replace('public/', '')
return None
except Exception as e:
logger.error(f"Media download error: {e}")
return None

async def fetch_posts(hours_back=48):
"""
Fetch recent Telegram posts.
Fetch recent posts from a Telegram channel.
"""
validate_environment_variables()
posts = []
try:
application = Application.builder().token(BOT_TOKEN).build()
bot = application.bot
app = Application.builder().token(BOT_TOKEN).build()
bot = app.bot
cutoff_time = datetime.now() - timedelta(hours=hours_back)
recent_messages = await bot.get_chat(CHANNEL_ID).get_history(limit=50)
for message in recent_messages:

# Attempt to fetch chat messages using ID or username
chat_id_or_username = CHANNEL_ID if CHANNEL_ID else CHANNEL_USERNAME
logger.info(f"Fetching messages from {chat_id_or_username}...")

# Fetch messages
try:
messages = await bot.get_chat_history(chat_id=chat_id_or_username, limit=50)
except TelegramError as e:
logger.error(f"Failed to fetch messages: {e}")
return []

for message in messages:
if message.date < cutoff_time:
continue

post_content = sanitize_text(message.text or message.caption or "")
post_image = None
try:
if message.photo:
file_id = message.photo[-1].file_id
post_image = await download_media(bot, file_id, "jpg")
post_image = await download_media(bot, file_id, 'jpg')
except Exception as media_error:
logger.error(f"Media error: {media_error}")
logger.error(f"Media processing error: {media_error}")

post = {
"title": post_content.split("\n")[0][:50] if post_content else "Untitled Post",
"content": post_content,
Expand All @@ -97,30 +106,28 @@ async def fetch_posts(hours_back=48):
"timestamp": message.date.isoformat(),
}
posts.append(post)

posts.sort(key=lambda x: x["timestamp"], reverse=True)
return posts[:10]
except TelegramError as te:
logger.error(f"Telegram API error: {te}")
return []
except Exception as e:
logger.error(f"Unexpected error: {e}")
logger.error(f"Error in fetch_posts: {e}")
return []

def save_posts(posts):
"""
Save posts to a JSON file.
Save processed posts to a JSON file.
"""
try:
os.makedirs(os.path.dirname(POSTS_FILE), exist_ok=True)
with open(POSTS_FILE, "w", encoding="utf-8") as file:
json.dump(posts, file, indent=4, ensure_ascii=False)
logger.info(f"Saved {len(posts)} posts to {POSTS_FILE}")
logger.info(f"Successfully saved {len(posts)} posts to {POSTS_FILE}")
except Exception as e:
logger.error(f"Error saving posts: {e}")
logger.error(f"Error saving posts to file: {e}")

async def main():
"""
Main function to fetch and save Telegram posts.
Main async function to fetch and save Telegram channel posts.
"""
logger.info("Starting Telegram channel post collection process...")
try:
Expand All @@ -130,7 +137,7 @@ async def main():
else:
logger.warning("No new posts found.")
except Exception as e:
logger.error(f"Critical error: {e}")
logger.error(f"Critical error in main process: {e}")
raise

if __name__ == "__main__":
Expand Down

0 comments on commit bc16d0a

Please sign in to comment.