Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 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 sqlite3
  5. import random
  6.  
  7. '''USER CONFIGURATION'''
  8.  
  9. USERNAME = "Flip-The-Coin"
  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 = "/u/inanegs' First bot"
  14. #This is a short description of what the bot does. For example "/u/GoldenSights' Newsletter bot"
  15. SUBREDDIT = "inaneg"
  16. #This is the sub or list of subs to scan for new posts. For a single sub, use "sub1". For multiple subreddits, use "sub1+sub2+sub3+..."
  17. PARENTSTRING = ["!flip"]
  18. #These are the words you are looking for
  19. #REPLYSTRING = "Hi hungry, I'm dad"
  20. #This is the word you want to put in reply
  21. MAXPOSTS = 100
  22. #This is how many posts you want to retrieve all at once. PRAW can download 100 at a time.
  23. WAIT = 1
  24. #This is how many seconds you will wait between cycles. The bot is completely inactive during this time.
  25.  
  26.  
  27. '''All done!'''
  28.  
  29.  
  30.  
  31.  
  32. WAITS = str(WAIT)
  33. try:
  34. 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
  35. USERNAME = bot.getu()
  36. PASSWORD = bot.getp()
  37. USERAGENT = bot.geta()
  38. except ImportError:
  39. pass
  40.  
  41. sql = sqlite3.connect('sql.db')
  42. print('Loaded SQL Database')
  43. cur = sql.cursor()
  44.  
  45. cur.execute('CREATE TABLE IF NOT EXISTS oldposts(ID TEXT)')
  46. print('Loaded Completed table')
  47.  
  48. sql.commit()
  49.  
  50. r = praw.Reddit(USERAGENT)
  51. r.login(USERNAME, PASSWORD)
  52.  
  53. def scanSub():
  54. print('Searching '+ SUBREDDIT + '.')
  55. subreddit = r.get_subreddit(SUBREDDIT)
  56.  
  57. #Trying to make this to loop
  58. RAND = random.randint(0,1)
  59. if RAND == 0:
  60. REPLYSTRING = "I fliped a coin and it became heads"
  61. else:
  62. REPLYSTRING = "I fliped a coin and it became tails"
  63.  
  64.  
  65.  
  66.  
  67.  
  68. posts = subreddit.get_comments(limit=MAXPOSTS)
  69. for post in posts:
  70. pid = post.id
  71. try:
  72. pauthor = post.author.name
  73. cur.execute('SELECT * FROM oldposts WHERE ID=?', [pid])
  74. if not cur.fetchone():
  75. cur.execute('INSERT INTO oldposts VALUES(?)', [pid])
  76. pbody = post.body.lower()
  77. if any(key.lower() in pbody for key in PARENTSTRING):
  78. if pauthor.lower() != USERNAME.lower():
  79.  
  80. print('Replying to ' + pid + ' by ' + pauthor)
  81. post.reply(REPLYSTRING)
  82. else:
  83. print('Will not reply to self')
  84. except AttributeError:
  85. pauthor = '[DELETED]'
  86. sql.commit()
  87.  
  88.  
  89. while True:
  90. try:
  91. scanSub()
  92. except Exception as e:
  93. print('An error has occured:', e)
  94. print('Running again in ' + WAITS + ' seconds \n')
  95. sql.commit()
  96. time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement