Guest User

Untitled

a guest
Jul 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. import time
  2.  
  3. #Define Constants
  4.  
  5. REMOVAL_MINS = 10
  6. QUESTION_MINS = 15
  7. FIX_MINS = 1440
  8.  
  9.  
  10. #Login
  11.  
  12. import praw
  13. from termcolor import colored
  14. def login():
  15. reddit = praw.Reddit(client_id='NFo0dg1y1rloWA',
  16. client_secret="1Mc9h1owZF315bQQs3JfzvZTcnc",
  17. refresh_token='71674502-FeBdtCizvkwP7BExAVFVLLNBFKM',
  18. user_agent='Helps moderators')
  19. print(colored('Logged in as: ' + str(reddit.user.me()), 'green'))
  20. return reddit
  21. r = login()
  22.  
  23. #Initialize Database
  24. #how to fix error like this: require(...) is not a function
  25. import sqlite3
  26. conn = sqlite3.connect('posts.db');
  27. c = conn.cursor()
  28.  
  29. #rposts --> id, created, isremoved
  30.  
  31.  
  32. #get id, time created, and removal status from post if it exists
  33. #otherwise, return False
  34. def getInfo(ident):
  35. t = (ident,)
  36. for row in c.execute('''SELECT * FROM rposts WHERE id=?''', t):
  37. postId = row[0]
  38. time_created = row[1]
  39. isRemoved = row[2]
  40. return [postId, time_created, isRemoved]
  41. return False
  42.  
  43. #list all posts in the database
  44. def listAll():
  45. for row in c.execute('''SELECT * FROM rposts'''):
  46. print(row)
  47.  
  48. #add a post for the first time when removing it
  49. def addPost(ident, timec, parId, isRem):
  50. ident = "'" + ident + "'"
  51. timec = "'" + timec + "'"
  52. parId = "'" + parId + "'"
  53. isRem = "'" + isRem + "'"
  54. c.execute('''INSERT INTO rposts VALUES ({}, {}, {}, {})'''.format(ident, timec, parId ,isRem))
  55.  
  56.  
  57. #delete all posts (only used for bugfixing)
  58. def deleteAll():
  59. c.execute('''DELETE FROM rposts''')
  60.  
  61. #delete post with specific id from database
  62. def deleteSpec(ident):
  63. t = (ident,)
  64. c.execute('''DELETE FROM rposts WHERE id=?''', t)
  65.  
  66.  
  67. #return the age of a post in minutes
  68. def checkAge(post):
  69. ctime = time.time() + 28800
  70. difference = float(ctime) - post.created
  71. return difference/60
  72.  
  73. #first main function; naive check for old questions
  74. def checkQuestion(post):
  75. if checkAge(post) > QUESTION_MINS and str(post.link_flair_text).lower() == 'quick question' and getInfo(post.id) == False:
  76. myComment = post.reply('''This post has been removed because it was flaired as "Quick Question" and has been up for 15 minutes. If you do not feel that your question was answered to your satisfaction, you may either:\n\n1.) Submit the question again in an hour\n\n2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).\n\n If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
  77. myComment.mod.distinguish(how='yes', sticky=True)
  78. post.mod.remove()
  79.  
  80. #second main function; check for old, unflaired posts
  81.  
  82. def checkForOld(post):
  83. if checkAge(post)>REMOVAL_MINS and post.link_flair_text == None and getInfo(post.id) == False:
  84. myComment = post.reply('''This post has automatically been removed for not being flaired within 10 minutes. When the post receives a flair it will automatically be restored. Reply to your thread with one of the following commands and automod will flair it accordingly.\n\n
  85. Flair | Command
  86. ---|---
  87. Quick Question | -question
  88. Discussion | -discussion
  89. Analysis | -analysis
  90. News | -news
  91. Fan Art | -fanart
  92. Chat | -chat
  93. Humor | -humor
  94. \n\n
  95. If you believe that this removal was a mistake, please contact the moderators.''')
  96. myComment.mod.distinguish(how='yes', sticky=True)
  97. post.mod.remove()
  98. addPost(post.id, str(post.created), myComment.id, 'true')
  99.  
  100. #third main function; database checker/complex question checking
  101.  
  102. def checkDatabase():
  103. for row in c.execute('''SELECT * FROM rposts'''):
  104. postId = row[0]
  105. postCreated = row[1]
  106. parId = row[2]
  107. postRemoved = row[3]
  108. actualPost = r.submission(id=postId)
  109. if str(actualPost.author) == 'None':
  110. deleteSpec(actualPost.id)
  111. elif postRemoved == 'true':
  112. if checkAge(actualPost) > FIX_MINS:
  113. r.comment(id=parId).edit('This post is older than 24 hours and can no longer be reapproved. Please resubmit and flair in time.')
  114. deleteSpec(actualPost.id)
  115. elif str(actualPost.link_flair_text).lower() == 'quick question':
  116. r.comment(id=parId).delete()
  117. actualPost.mod.approve()
  118. deleteSpec(actualPost.id)
  119. addPost(postId, str((time.time() + 28800)), 'None', 'false')
  120.  
  121. elif actualPost.link_flair_text != None:
  122. r.comment(id=parId).delete()
  123. actualPost.mod.approve()
  124. deleteSpec(actualPost.id)
  125. elif ((float(time.time()) + 28800) - float(postCreated))/60 > QUESTION_MINS:
  126. myComment = actualPost.reply('''This post has been removed because it was flaired as "Quick Question" and has been up for 15 minutes. If you do not feel that your question was answered to your satisfaction, you may either:\n\n1.) Submit the question again in an hour\n\n2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).\n\n If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
  127. deleteSpec(actualPost.id)
  128. myComment.mod.distinguish(how='yes', sticky=True)
  129. actualPost.mod.remove()
  130.  
  131. def runTimeBot():
  132. for post in r.subreddit('FireEmblemHeroes').new(limit=20):
  133. if str(post.author).lower() != "automoderator":
  134. checkQuestion(post)
  135. checkForOld(post)
  136. checkDatabase()
  137. conn.commit()
  138.  
  139.  
  140. while True:
  141. try:
  142. runTimeBot()
  143. except:
  144. pass
  145.  
  146.  
  147. conn.close()
Add Comment
Please, Sign In to add comment