Guest User

Untitled

a guest
Jul 6th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. #/u/GoldenSights
  2. import praw # simple interface to the reddit API, also handles rate limiting of requests
  3. import time
  4. import os
  5. import sys
  6. import sqlite3
  7.  
  8. '''USER CONFIGURATION'''
  9. USERNAME  = ""
  10. #This is the bot's Username. In order to send mail, he must have some amount of Karma.
  11. PASSWORD  = ""
  12. #This is the bot's Password.
  13. USERAGENT = ""
  14. #This is a short description of what the bot does. For example "/u/GoldenSights' Newsletter bot"
  15. SUBRESTRICT = ["GoldTesting"]
  16. #This is the subreddit where the bot is allowed to post.
  17. #If a user inputs a permalink that does not lead to this subreddit, the post will fail.
  18. #To allow any, delete everything between the brackets.
  19. COMHEADER = "This is at the top of the comment\n\n"
  20. #Comment Header
  21. COMFOOTER = "\n\nThis is at the bottom of the comment"
  22. #Comment Footer
  23. PMHEADER = "This is at the top of the return PM\n\n"
  24. #PM Header
  25. PMFOOTER = "\n\nThis is at the bottom of the return PM"
  26. #PM Footer
  27. PMSUCCESS = "Your comment has successfully been created: _permalink_"
  28. #This will be sent to the user when his post succeeds
  29. #_permalink_ will be replaced by the successful comment's permalink. You may move this around as you please.
  30. PMFAILURE = "Your comment has been rejected for the following reason(s):\n\n"
  31. #This will be sent to the user when his post fails. Error messages will be displayed
  32.  
  33. ERRBANNED = "- You have been banned from using this service"
  34. #If an admin has placed this user on the banlist, return this error
  35. ERRFETCH = "- Failed to fetch comment object given that permalink. You should use the permalink exactly as it appears when you cut / copy from your address bar. The last 7 characters are the comment's id number."
  36. #If praw fails in fetching the object from ID, return this error
  37. ERRNOPERMA = "- The first line of your PM must be a permalink to a comment."
  38. #If the first line does not contain any visible permalink, return this error
  39.  
  40. BANCOMMAND = "banuser"
  41. UNBANCOMMAND = "unbanuser"
  42. #The ADMIN may use these commands to ban / unban a username
  43.  
  44. DISTINGUISHCOMMENT = False
  45. #If your bot is going to be operating in a sub where it is a moderator, you may choose to distinguish the comment
  46. #Use True or False (With Capitals! No quotation marks!)
  47. WAIT = 30
  48. #This is how many seconds you will wait between cycles. The bot is completely inactive during this time.
  49. ADMIN = "GoldenSights"
  50. '''All done!'''
  51.  
  52.  
  53.  
  54.  
  55.  
  56. try:
  57.     import bot #This is a file in my python library which contains my Bot's username and password. I can push code to Git without showing credentials
  58.     USERNAME = bot.getuG()
  59.     PASSWORD = bot.getpG()
  60.     USERAGENT = bot.getaG()
  61. except ImportError:
  62.     pass
  63. WAITS = str(WAIT)
  64. banlist = []
  65.  
  66.  
  67. sql = sqlite3.connect('sql.db')
  68. print('Loaded SQL Database')
  69. cur = sql.cursor()
  70.  
  71. cur.execute('CREATE TABLE IF NOT EXISTS oldposts(ID TEXT)')
  72. print('Loaded old comments')
  73. cur.execute('CREATE TABLE IF NOT EXISTS banned(name TEXT)')
  74. print('Loaded banned users')
  75.  
  76. sql.commit()
  77.  
  78.  
  79. r = praw.Reddit(USERAGENT)
  80. r.login(USERNAME, PASSWORD)
  81.  
  82. def scanPM():
  83.     cur.execute('SELECT * FROM banned')
  84.     fetched = cur.fetchall()
  85.     for m in fetched:
  86.         banlist.append(m[0])
  87.     print(str(len(banlist)) + ' banned users.\n')
  88.     print('Searhing Inbox.')
  89.     pms = r.get_unread(unset_has_mail=True, update_user=True)
  90.     for pm in pms:
  91.         result = []
  92.         failresult = []
  93.         failoverride = False
  94.         cobj = ''
  95.         print(pm.id)
  96.  
  97.         try:
  98.             author = pm.author.name
  99.             if author.lower() == ADMIN.lower():
  100.                 line = pm.body.lower().split()[0]
  101.                 if BANCOMMAND.lower() in line:
  102.                     user = line[-1]
  103.                     print('\t[   ] ADMIN has banned ' + user)
  104.                     cur.execute('INSERT INTO banned VALUES(?)', [user])
  105.                     failoverride =  True
  106.                 if UNBANCOMMAND.lower() in line:
  107.                     user = line[-1]
  108.                     print('\t[   ] ADMIN has unbanned ' + user)
  109.                     cur.execute('DELETE FROM banned WHERE name=?', [user])
  110.                     failoverride =  True
  111.  
  112.  
  113.             bodysplit = pm.body.lower().split()
  114.  
  115.             if '://reddit.com/r/' in bodysplit[0]:
  116.                 for word in bodysplit[0].split():
  117.                     if '://reddit.com/r/' in word
  118.                     link = word
  119.                 if '/' == link[-8]:
  120.                     link = link[-7:]
  121.                 elif = '/' == link[-1] and '/' == link[-9]:
  122.                     link = link[-8:-1]
  123.                 try:
  124.                     cobj = r.get_info(thing_id='t1_' + link,fetch=True)
  125.                     print('\t[   ] Found comment object')
  126.                     if SUBRESTRICT == [] or any(cobj.subreddit.display_name.lower() == sub.lower() for sub in SUBRESTRICT):
  127.                         print('\t[   ] Passed sub restriction')
  128.                        
  129.                 except:
  130.                     print('\t[ERR] Fetching comment object failed')
  131.                     failresult.append(ERRFETCH)
  132.             else:
  133.                 print('\t[ERR] No permalink on first line')
  134.                 failresult.append(ERRNOPERMA)
  135.  
  136.  
  137.         except AttributeError:
  138.             print('\t[ERR] Author unavailable.')
  139.         sql.commit()
  140.  
  141.  
  142. while True:
  143.     try:
  144.         scanPM()
  145.     except Exception as e:
  146.         print('ERROR: ' + str(e))
  147.     print('Running again in ' + WAITS + ' seconds \n_________\n')
  148.     sql.commit()
  149.     time.sleep(WAIT)
Add Comment
Please, Sign In to add comment