daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import praw
  3. import pdb
  4. import re
  5. import os
  6. import time
  7. import psycopg2
  8. from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
  9. dir_path = os.path.dirname(os.path.realpath(__file__))
  10. DATABASE_URL = os.environ['DATABASE_URL']
  11.  
  12. # Create the Reddit instance
  13. def 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.  
  24. def 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("UPDATE posts_replied_to SET ids = (|| subid)")
  42.     print ("Sleeping for 10 seconds...")
  43.     time.sleep(10)
  44.  
  45. reddit = bot_login()
  46. print ("Connecting to SQL Database")
  47. conn = psycopg2.connect(DATABASE_URL, sslmode='require')
  48. conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  49. cur = conn.cursor()
  50. cur.execute("CREATE TABLE IF NOT EXISTS posts_replied_to (ids text, PRIMARY KEY(ids));")
  51. getids = cur.execute("SELECT ids FROM posts_replied_to;")
  52. getids = cur.fetchall()
  53. if getids is None:
  54.     cur.execute("UPDATE posts_replied_to SET ids = '-Start-'")
  55. while True:
  56.     bot_run(reddit)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top