Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #requirements: python 3.x and PRAW 5.x
- import praw
- import time
- import re
- import sqlite3
- reddit = praw.Reddit(
- username="",
- password="",
- client_id="",
- client_secret="",
- user_agent="",
- api_request_delay=1,
- )
- class Database:
- """simple class to handle the database"""
- def __init__(self,file=re.sub("\.\w+",".db",__file__)):
- self.file=file
- self.conn=sqlite3.connect(file)
- self.cur=self.conn.cursor()
- def create_tables(self):
- self.cur.execute("CREATE TABLE IF NOT EXISTS comment (id INTEGER)")
- self.conn.commit()
- def insert(self,_id):
- self.cur.execute("INSERT INTO comment VALUES(?)",(_id,))
- self.conn.commit()
- def has_id(self,_id):
- return bool(self.cur.execute("SELECT * FROM comment WHERE id=?",(_id,)).fetchall())
- class Bot:
- """create a bot object"""
- def __init__(self,subreddit,keywords,instance=reddit,account="iNeverQuiteWas"):
- self.reddit=instance
- self.redditor=self.reddit.redditor(account)
- self.subreddit=self.reddit.subreddit(subreddit)
- self.keywords=list(keywords)
- self.session=Database()
- def search(self):
- for comment in self.subreddit.stream.comments():
- if (any([bool(re.search(keyword,comment.body,re.IGNORECASE)) for keyword in self.keywords]) and not self.session.has_id(comment.id)):
- self.session.insert(comment.id)
- self.redditor.message("Keyword Notification","[link to comment]({})".format(comment.permalink))
- def main(init=True):
- session=Database()
- if (init):
- session.create_tables()
- Bot("apple",["iphone"]).search()
- if (__name__ == "__main__"):
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement