-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
59 lines (50 loc) · 1.72 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import praw
from llmGoogle import humoroize
from twitter import create_post
from logger import log_activity
import os
from dotenv import load_dotenv
from pathlib import Path
import json
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
username = os.environ.get("REDDIT_USERNAME")
password = os.environ.get("REDDIT_PASSWORD")
client_id = os.environ.get("REDDIT_CLIENT_ID")
client_secret = os.environ.get("REDDIT_CLIENT_SECRET")
reddit_instance = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
username=username,
password=password,
user_agent="A news feed Bot made by u/Theinit01"
)
subreddit = reddit_instance.subreddit("notthepyaaz")
top_posts = subreddit.rising(limit=10)
# Load posted post IDs from JSON file
json_file = 'posted_posts.json'
try:
with open(json_file, 'r') as f:
posted_posts = set(json.load(f))
except FileNotFoundError:
posted_posts = set()
new_added = set()
for post in top_posts:
if not post.is_self:
if post.id in posted_posts:
continue
try:
url = post.url
title = post.title
funny_title = humoroize(title)
tweet = funny_title + "\n#Notthepyaaz #nottheonion" + "\n\n" + url
create_post(tweet)
#log_activity(f"Posted tweet with ID {post.id} - '{funny_title}'")
posted_posts.add(post.id)
new_added.add(post.id)
except Exception as e:
log_activity(f"Failed to post tweet with ID {post.id} - {e}")
# Save updated posted_posts set to JSON file
with open(json_file, 'w') as f:
json.dump(list(posted_posts), f)
log_activity(f"Script executed. {len(new_added)} tweets were posted. - {new_added}" )