Advertisement
Guest User

Untitled

a guest
Sep 1st, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. import praw
  2. import time
  3. import datetime
  4. import sqlite3
  5.  
  6. '''USER CONFIGURATION'''
  7.  
  8. USERNAME =
  9. #This is the bot's Username. In order to send mail, he must have some amount of Karma.
  10. PASSWORD =
  11. #This is the bot's Password.
  12. USERAGENT =
  13. #This is a short description of what the bot does. For example uGoldenSights' Newsletter Bot
  14. SUBREDDIT = GoldTesting
  15. #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+...
  16. MAXPOSTS = 30
  17. #This is how many posts you want to retreieve all at once. PRAW will download 100 at a time.
  18. WAIT = 20
  19. #This is how many seconds you will wait between cycles. The bot is completely inactive during this time.
  20. TSTRING = [request]
  21. #This is the part of the title that you want to look for
  22. DELAY = 172800
  23. #This is the time limit between a user's posts, IN SECONDS. 1h = 3600 12h = 43200 24h = 86400 144h = 518400
  24.  
  25. '''All done!'''
  26.  
  27.  
  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.getuG()
  36. PASSWORD = bot.getpG()
  37. USERAGENT = bot.getaG()
  38. except ImportError
  39. pass
  40. sql = sqlite3.connect('sql.db')
  41. print('Loaded SQL Database')
  42. cur = sql.cursor()
  43. cur.execute('CREATE TABLE IF NOT EXISTS users(name TEXT, lastpost TEXT)')
  44. print('Loaded Users')
  45. cur.execute('CREATE TABLE IF NOT EXISTS oldposts(id TEXT)')
  46. print('Loaded Oldposts')
  47. sql.commit()
  48.  
  49. r = praw.Reddit(USERAGENT)
  50.  
  51. Trying = True
  52. while Trying
  53. try
  54. r.login(USERNAME, PASSWORD)
  55. print('Successfully logged in')
  56. Trying = False
  57. except praw.errors.InvalidUserPass
  58. print('Wrong Username or Password')
  59. quit()
  60. except Exception as e
  61. print(%s % e)
  62. time.sleep(5)
  63.  
  64. def getTime(bool)
  65. timeNow = datetime.datetime.now(datetime.timezone.utc)
  66. timeUnix = timeNow.timestamp()
  67. if bool == False
  68. return timeNow
  69. else
  70. return timeUnix
  71.  
  72. def scan()
  73. print('Scanning ' + SUBREDDIT)
  74. subreddit = r.get_subreddit(SUBREDDIT)
  75. posts = subreddit.get_new(limit=MAXPOSTS)
  76. for post in posts
  77. try
  78. pauthor = post.author.name
  79. except Exception
  80. pauthor = '[deleted]'
  81. pid = post.id
  82. plink = post.short_link
  83. ptime = post.created_utc
  84. ptitle = post.title.lower()
  85. if TSTRING.lower() in ptitle
  86. cur.execute('SELECT FROM oldposts WHERE id=%s' % pid)
  87. if not cur.fetchone()
  88. cur.execute('SELECT FROM users WHERE name=%s' % pauthor)
  89. if not cur.fetchone()
  90. print('Found new user ' + pauthor)
  91. cur.execute('INSERT INTO users VALUES(%s, %s)' % (pauthor, pid))
  92. r.send_message(pauthor, 'Welcome to rpkmntcgtrades!','Dear ' + pauthor + ',nn Our bot has determined that this is your first time posting in rpkmntcgtrades. Please take the time to read [the guidelines](httpwww.reddit.comrpkmntcgtradeswikiguidelines) to understand how the subreddit works.nnIf you have any questions, feel free to [message the moderators.](httpwww.reddit.commessagecomposeto=%2Fr%2Fpkmntcgtrades) Thanks, and happy trading!', captcha=None)
  93. sql.commit()
  94. print('t' + pauthor + ' has been added to the database.')
  95. time.sleep(5)
  96. else
  97. cur.execute('SELECT FROM users WHERE name=%s' % pauthor)
  98. fetch = cur.fetchone()
  99. print('Found post by known user ' + pauthor)
  100. previousid = fetch[1]
  101. previous = r.get_info(thing_id='t3_'+previousid)
  102. previoustime = previous.created_utc
  103. if ptime previoustime
  104. curtime = getTime(True)
  105. difference = curtime - previoustime
  106. if difference = DELAY
  107. print('tPost complies with timelimit guidelines. Permitting')
  108. cur.execute('DELETE FROM users WHERE name=%s' % pauthor)
  109. cur.execute('INSERT INTO users VALUES(%s, %s)' % (pauthor, pid))
  110. sql.commit()
  111. print('t' + pauthor + 's database info has been reset.)
  112. else
  113. differences = str(DELAY - difference)
  114. print('tPost does not comply with timelimit guidelines. Author must wait ' + differences)
  115. print('t' + pauthor + 's database info remains unchanged)
  116. response = post.add_comment('You are posting here too frequently, so your post has been deleted. Please refer to the rule Submit no more than one post per 7 days in [the guidelines.](httpwww.reddit.comrpkmntcgtradeswikiguidelines#wiki_rules)')
  117. response.distinguish()
  118. post.remove(spam=False)
  119. time.sleep(5)
  120. cur.execute('INSERT INTO oldposts VALUES(%s)' % pid)
  121. sql.commit()
  122.  
  123.  
  124.  
  125.  
  126. while True
  127. try
  128. scan()
  129. except Exception as e
  130. print('An error has occured', e)
  131. print('Running again in ' + WAITS + ' seconds.n')
  132. time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement