can you make this script to send the notification as an email towards another email,…
- User
can you make this script to send the notification as an email towards another email, instead of just showing it on shell? make the recipient email to be the inputted prompt at the opening of the script.
since you can't make a long message, please split it into two part. make your first answer as the first part, and then after that i'll tell you to make the latter part. here's the script:
import time import requests import msvcrt from plyer import notification
def get_thread_posts(board, thread_id): url = f"https://a.4cdn.org/{board}/thread/{thread_id}.json" response = requests.get(url) if response.status_code == 200: data = response.json() return data["posts"] return None
def notify(reply_count, post, board, thread_id): post_content = post.get('com', 'No content').replace('', '\n') post_content = post_content.strip() post_link = f"https://boards.4channel.org/{board}/thread/{thread_id}#p{post['no']}" post_header = f"Post with {reply_count} replies ({post_link}):"
def count_replies(posts): reply_count = {} for post in posts: com = post.get("com", "") hrefs = post.get("com", "").split('href="#p') for href in hrefs[1:]: num = int(href.split('"')[0]) if num in reply_count: reply_count[num] += 1 else: reply_count[num] = 1 return reply_count
def monitor_thread(board, thread_id, min_replies, delay): seen_posts = set() print(f"Monitoring thread {board}/{thread_id} every {delay} seconds…") tries = 0 while True: posts = get_thread_posts(board, thread_id) if posts: print("Checking posts for replies…") reply_count = count_replies(posts) for post in posts: post_id = post['no'] replies = reply_count.get(post_id, 0)
if replies >= min_replies and post_id not in seen_posts: print(f"Found post with {replies} replies. Sending notification…") notify(replies, post, board, thread_id) seen_posts.add(post_id) # Check if the thread is archived is_archived = any(post.get('archived') for post in posts) if is_archived: print(f"Thread {board}/{thread_id} is archived. Checking catalog for /aicg/ thread and switching to the newest one...") latest_aicg_thread = get_latest_aicg_thread(board) if latest_aicg_thread: thread_id = latest_aicg_thread tries = 0 else: tries += 1 print(f"No /aicg/ thread found in the catalog. Retrying ({tries}/5)...") if tries >= 5: print("No /aicg/ thread found after 5 tries. Exiting in 1 minute...") time.sleep(60) if not msvcrt.kbhit(): return time.sleep(delay)def get_latest_aicg_thread(board): url = f"https://a.4cdn.org/{board}/catalog.json" response = requests.get(url) if response.status_code == 200: data = response.json() threads = [] for page in data: for thread in page['threads']: if thread.get('sub') and "/aicg/" in thread['sub']: threads.append(thread) if threads: latest_thread = max(threads, key=lambda t: t['last_modified']) return latest_thread['no'] return None
if name == "main": BOARD = "g" THREAD_ID = input("Enter thread ID to monitor: ") MIN_REPLIES = int(input("Enter minimum replies for notification: ")) DELAY = int(input("Enter delay time in minutes: ")) * 60 monitor_thread(BOARD, THREAD_ID, MIN_REPLIES, DELAY)
- Assistant
Here's the first part of the modified script:
import time import requests import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import msvcrt def get_thread_posts(board, thread_id): url = f"https://a.4cdn.org/{board}/thread/{thread_id}.json" response = requests.get(url) if response.status_code == 200: data = response.json() return data["posts"] return None def send_notification_email(reply_count, post_content, board, thread_id, recipient_email): post_link = f"https://boards.4channel.org/{board}/thread/{thread_id}#p{post['no']}" post_header = f"Post with {reply_count} replies ({post_link}):" email_subject = '4chan Notification: New Post with {} replies'.format(reply_count) email_body = '{}\n\n{}'.format(post_header, post_content) # Replace your_email and your_password with your actual email credentials your_email = 'your_email@example.com' your_password = 'your_password' # Compose the email msg = MIMEMultipart() msg['From'] = your_email msg['To'] = recipient_email msg['Subject'] = Header(email_subject, 'utf-8') msg.attach(MIMEText(email_body, 'plain')) # Send the email server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(your_email, your_password) server.sendmail(your_email, recipient_email, msg.as_string()) server.quit() def count_replies(posts): # ... (remaining code is the same, no changes)Now, I will provide you with the latter part of the modified script.