Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  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(f"UPDATE posts_replied_to SET ids = (concat('{getids}','{subid}'))")
  42.                 conn.commit()
  43.     print ("Sleeping for 10 seconds...")
  44.     time.sleep(10)
  45.  
  46. reddit = bot_login()
  47. print ("Connecting to SQL Database")
  48. conn = psycopg2.connect(DATABASE_URL, sslmode='require')
  49. conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  50. cur = conn.cursor()
  51. cur.execute("CREATE TABLE IF NOT EXISTS posts_replied_to (ids text, PRIMARY KEY(ids));")
  52. cur.execute("SELECT ids FROM posts_replied_to;")
  53. getids = str(cur.fetchall())
  54. if getids is None:
  55.     cur.execute("UPDATE posts_replied_to SET ids = '-Start-: '")
  56. print ("Current IDs: " + getids)
  57. conn.commit()
  58. while True:
  59.     bot_run(reddit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement