Advertisement
Guest User

Untitled

a guest
Jul 7th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. import traceback
  2. import praw
  3. import time
  4. import sqlite3
  5.  
  6. # Login info
  7. USERNAME = "FreeFolkStewardBot"
  8. PASSWORD = "qVQauofFb5li"
  9. USERAGENT = "/r/FreeFolk Automaton"
  10. SUBREDDIT = "FreeFolkMods"
  11.  
  12. # Timing
  13. MAXPOSTS = 20
  14. WAIT = 30
  15. CLEANCYCLES = 20
  16. numCycles = 1
  17.  
  18. sql = sqlite3.connect('mailing.db')
  19. print('Loaded SQL Database')
  20. cur = sql.cursor()
  21. cur.execute('CREATE TABLE IF NOT EXISTS users (name TEXT)')
  22. cur.execute('CREATE TABLE IF NOT EXISTS oldposts (id TEXT)')
  23. sql.commit()
  24.  
  25. print('Logging in...')
  26. r = praw.Reddit(USERAGENT)
  27. r.login(USERNAME, PASSWORD,disable_warning=True)
  28.  
  29. def mailingbot():
  30.     print('Cycle #%i' % numCycles)
  31.     print('Searching %s.' % SUBREDDIT)
  32.     subreddit = r.get_subreddit(SUBREDDIT)
  33.     posts = list(subreddit.get_comments(limit=MAXPOSTS))
  34.     posts.reverse()
  35.     for post in posts:
  36.         pid = post.id
  37.         pauthor = post.author.name
  38.         ptop = post.submission.id
  39.         pbody = post.body.lower()
  40.        
  41.         try:
  42.             post.author.name # in case user account is deleted or suspended
  43.         except AttributeError:
  44.             continue # user is not available
  45.            
  46.         cur.execute('SELECT * FROM oldposts WHERE ID=?', [pid])
  47.         if cur.fetchone():
  48.             # already checked this post
  49.                 continue
  50.  
  51.         # record post so we don't act on it multiple times
  52.         cur.execute('INSERT INTO oldposts VALUES(?)', [pid])
  53.         sql.commit()
  54.        
  55.         # check for trigger phrases
  56.         beginning = pbody[:6]
  57.         if beginning == '!addme':
  58.             print('******* ADD triggered by /u/' + pauthor +' in COMMENT ' + pid + ' on POST ' + ptop)
  59.             # add username to mailing list
  60.             cur.execute('SELECT * FROM users WHERE name LIKE \'' + pauthor + '\'')
  61.             if cur.fetchone():
  62.                 # already in database
  63.                 REPLY_SUBJECT = 'Couldn\'t add you'
  64.                 REPLY_MESSAGE = 'I see you tried to join the FreeFolk mailing list, but you were already on it. Were you trying to leave the list? You can say !DELME to be removed.'
  65.                 r.send_message(pauthor, REPLY_SUBJECT, REPLY_MESSAGE)
  66.             else:
  67.                 # add to database
  68.                 cur.execute('INSERT INTO users VALUES (\'' + pauthor + '\')')
  69.                 sql.commit()
  70.                 REPLY_SUBJECT = 'I added you'
  71.                 REPLY_MESSAGE = 'I see you asked to join the FreeFolk mailing list. It was a great success! To leave the list in the future, you can say !DELME to be removed.'
  72.                 r.send_message(pauthor, REPLY_SUBJECT, REPLY_MESSAGE)
  73.         elif beginning == '!delme':
  74.             print('******* DEL triggered by /u/' + pauthor +' in COMMENT ' + pid + ' on POST ' + ptop)
  75.             # remove username from mailing list
  76.             cur.execute('SELECT * FROM users WHERE name LIKE \'' + pauthor + '\'')
  77.             if cur.fetchone():
  78.                 # already in database
  79.                 cur.execute('DELETE FROM users WHERE name LIKE \'' + pauthor + '\'')
  80.                 sql.commit()
  81.                 REPLY_SUBJECT = 'I removed you'
  82.                 REPLY_MESSAGE = 'I see you asked to leave the FreeFolk mailing list. It was a great success! To join again in the future, you can say !ADDME to be put back on.'
  83.                 r.send_message(pauthor, REPLY_SUBJECT, REPLY_MESSAGE)
  84.             else:
  85.                 # remove from database
  86.                 REPLY_SUBJECT = 'Couldn\'t remove you'
  87.                 REPLY_MESSAGE = 'I see you tried to leave the FreeFolk mailing list, but you weren\'t on it. Were you trying to join the list? You can say !ADDME to be included.'
  88.                 r.send_message(pauthor, REPLY_SUBJECT, REPLY_MESSAGE)
  89.                
  90.         elif beginning == '!pmall':
  91.             print('******* PM triggered by /u/' + pauthor +' in COMMENT ' + pid + ' on POST ' + ptop)
  92.             # check user is authorized to send
  93.             if pauthor == 'leafeon123' or pauthor == 'itsCarraldo' or pauthor == 'rwjehs' or pauthor == 'Dunkcity239' or pauthor == 'Deadm1r' or pauthor == 'Jon_targaryen1' or pauthor == 'cgmcnama' or pauthor == '123hooha123' or pauthor == 'ghost-of-harrenhal' or pauthor == 'Darth_Birdperson' or pauthor == 'corduroyblack' or pauthor == '_varamyr_fourskins_' or pauthor == 'BladeofTarth':      
  94.                 # remove first 7 characters of post -- should be "!pmall " -- note space, not case sensitive
  95.                 PM_MESSAGE = post.body[7:]
  96.                 # get list of users to send PM to
  97.                 print('SENDING!')
  98.                 cur.execute('SELECT name FROM users')
  99.                 usernames = cur.fetchall()
  100.                 counter = 0
  101.                 for username in usernames:
  102.                     counter = counter + 1
  103.                     name = username[0]
  104.                     print(name)
  105.                     r.send_message(name, 'A Raven Has Arrived', PM_MESSAGE)
  106.                 print('TOTAL: ' + str(counter) )
  107.         # no triggers
  108.         else:
  109.             print(pid + ' - no trigger')
  110.                
  111. cycles = 0
  112. while True:
  113.     try:
  114.         mailingbot()
  115.         cycles += 1
  116.         numCycles +=1
  117.     except Exception as e:
  118.         traceback.print_exc()
  119.     # clean cycles
  120.     if cycles >= CLEANCYCLES:
  121.         print('Cleaning database')
  122.         cur.execute('DELETE FROM oldposts WHERE id NOT IN (SELECT id FROM oldposts ORDER BY id DESC LIMIT ?)', [MAXPOSTS * 2])
  123.         sql.commit()
  124.         cycles = 0
  125.     print('Running again in %d seconds \n' % WAIT)
  126.     time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement