forked from Imasaab/royalnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redditbot.py
75 lines (67 loc) · 2.94 KB
/
redditbot.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import praw
import configparser
import db
import logging
# python-telegram-bot has a different name
# noinspection PyPackageRequirements
import telegram
import time
import raven
import os
import sys
import coloredlogs
# Init the config reader
config = configparser.ConfigParser()
config.read("config.ini")
logging.getLogger().disabled = True
logger = logging.getLogger(__name__)
os.environ["COLOREDLOGS_LOG_FORMAT"] = "%(asctime)s %(levelname)s %(name)s %(message)s"
coloredlogs.install(level="DEBUG", logger=logger)
sentry = raven.Client(config["Sentry"]["token"],
release=raven.fetch_git_sha(os.path.dirname(__file__)),
install_logging_hook=False,
hook_libraries=[])
def process():
logger.info("Retrieving parsed posts...")
session = db.Session()
results = session.query(db.ParsedRedditPost).all()
parsed_post_ids = [x.id for x in results]
session.close()
logger.info("Posts retrieved successfully.")
logger.info("Logging in to reddit...")
reddit = praw.Reddit(client_id=config["reddit"]["client_id"],
client_secret=config["reddit"]["client_secret"],
password=config["reddit"]["password"],
user_agent='Royal-Bot/4.1',
username=config["reddit"]["username"])
logger.info(f"Login as {reddit.user.me()} successful!")
logger.info(f"Logging in to Telegram...")
telegram_bot = telegram.Bot(config["Telegram"]["bot_token"])
logger.info(f"Login successful.")
r_royalgames = reddit.subreddit("royalgames")
for submission in r_royalgames.stream.submissions():
try:
if submission.id not in parsed_post_ids:
logger.info(f"New post found: {submission.id}")
logger.debug(f"Creating new db session...")
session = db.Session()
new_post = db.ParsedRedditPost(id=submission.id)
session.add(new_post)
logger.debug(f"Committing...")
session.commit()
session.close()
logger.debug("Sending Telegram notification...")
while True:
try:
telegram_bot.send_message(config["Telegram"]["main_group"],
f'ℹ️ Nuovo post su r/RoyalGames:\n'
f'<a href="https://reddit.com{submission.permalink}">'
f'{submission.title}</a>\n'
f'da <b>u/{submission.author}</b>',
parse_mode="HTML", disable_notification=True)
except telegram.error.TimedOut:
time.sleep(1)
else:
break
except Exception:
sentry.captureException(exc_info=sys.exc_info())