· 7 years ago · Oct 12, 2018, 01:00 PM
1#!/usr/bin/python
2import praw
3import pdb
4import re
5import os
6import time
7import psycopg2
8from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
9dir_path = os.path.dirname(os.path.realpath(__file__))
10DATABASE_URL = os.environ['DATABASE_URL']
11
12# Create the Reddit instance
13def bot_login():
14 print ("Logging in...")
15 reddit = praw.Reddit(client_id=os.environ['CLIENTID'],
16 client_secret=os.environ['CLIENTSECRET'],
17 password=os.environ['PASSWORD'],
18 user_agent='LokiBot v0.1',
19 username='LokiBot')
20 print ("Logged in.")
21 return reddit
22
23
24def bot_run(reddit):
25 #reddit.login(REDDIT_USERNAME, REDDIT_PASS)
26 # Get the top 25 values from our subreddit
27 print ("Getting 25 submissions")
28 subreddit = reddit.subreddit('lokicsstest')
29 for submission in subreddit.new(limit=25):
30 # If we haven't replied to this post before
31 print ("Checking if we have stored " + submission.title)
32 print (getids)
33 if submission.id not in getids:
34 search = submission.title.lower() + submission.selftext.lower()
35 if ('loki' in search and 'rework' in search) or ('loki' in search and 'broken' in search) or ('loki' in search and 'overpowered' in search) or ('loki' in search and 'unfun' in search):
36 reddit.redditor('TheServantofHelix').message('Another Post:' + submission.title, 'Link:' + submission.url)
37 print("Messaging /u/TheServantofHelix:", submission.title.lower())
38 # Store the current id into our list
39 print ("Storing " + submission.id + "in the database")
40 subid = submission.id
41 cur.execute(f"UPDATE posts_replied_to SET ids = (concat(ids,'{subid}'))")
42 print ("Sleeping for 10 seconds...")
43 time.sleep(10)
44
45reddit = bot_login()
46print ("Connecting to SQL Database")
47conn = psycopg2.connect(DATABASE_URL, sslmode='require')
48conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
49cur = conn.cursor()
50cur.execute("CREATE TABLE IF NOT EXISTS posts_replied_to (ids text, PRIMARY KEY(ids));")
51getids = cur.execute("SELECT ids FROM posts_replied_to;")
52getids = cur.fetchall()
53if getids is None:
54 cur.execute("UPDATE posts_replied_to SET ids = '-Start-'")
55while True:
56 bot_run(reddit)