Guest User

Untitled

a guest
Jul 22nd, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.14 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Define Constants
  5.  
  6. REMOVAL_MINS = 10
  7. QUESTION_MINS = 15
  8. FIX_MINS = 1440
  9.  
  10. # Login
  11.  
  12. import praw
  13. from termcolor import colored
  14.  
  15.  
  16. def login():
  17.     reddit = praw.Reddit(client_id='PmexbBBaeW8rVQ',
  18.                          client_secret='uN6kuwueKzp3z9x7oh34gdy__jQ',
  19.                          password='postmodbot',
  20.                          user_agent='Helps moderators',
  21.                          username='FEH_bot')
  22.     print colored('Logged in as: ' + str(reddit.user.me()), 'green')
  23.     return reddit
  24.  
  25.  
  26. r = login()
  27.  
  28. # Initialize Database
  29. # how to fix error like this: require(...) is not a function
  30.  
  31. import sqlite3
  32. conn = sqlite3.connect('posts.db')
  33. c = conn.cursor()
  34.  
  35.  
  36. # rposts --> id, created, isremoved
  37.  
  38. # get id, time created, and removal status from post if it exists
  39. # otherwise, return False
  40.  
  41. def getInfo(ident):
  42.     t = (ident, )
  43.     for row in c.execute('''SELECT * FROM rposts WHERE id=?''', t):
  44.         postId = row[0]
  45.         time_created = row[1]
  46.         isRemoved = row[2]
  47.         return [postId, time_created, isRemoved]
  48.     return False
  49.  
  50.  
  51. # list all posts in the database
  52.  
  53. def listAll():
  54.     for row in c.execute('''SELECT * FROM rposts'''):
  55.         print row
  56.  
  57.  
  58. # add a post for the first time when removing it
  59.  
  60. def addPost(
  61.     ident,
  62.     timec,
  63.     parId,
  64.     isRem,
  65.     ):
  66.  
  67.     ident = "'" + ident + "'"
  68.     timec = "'" + timec + "'"
  69.     parId = "'" + parId + "'"
  70.     isRem = "'" + isRem + "'"
  71.     c.execute('''INSERT INTO rposts VALUES ({}, {}, {}, {})'''.format(ident,
  72.               timec, parId, isRem))
  73.  
  74.  
  75. # delete all posts (only used for bugfixing)
  76.  
  77. def deleteAll():
  78.     c.execute('''DELETE FROM rposts''')
  79.  
  80.  
  81. # delete post with specific id from database
  82.  
  83. def deleteSpec(ident):
  84.     t = (ident, )
  85.     c.execute('''DELETE FROM rposts WHERE id=?''', t)
  86.  
  87.  
  88. # return the age of a post in minutes
  89.  
  90. def checkAge(post):
  91.     ctime = time.time() + 28800
  92.     difference = float(ctime) - post.created
  93.     return difference / 60
  94.  
  95.  
  96. # first main function; naive check for old questions
  97.  
  98. def checkQuestion(post):
  99.     if checkAge(post) > QUESTION_MINS \
  100.         and str(post.link_flair_text).lower() == 'quick question' \
  101.         and getInfo(post.id) == False:
  102.         myComment = \
  103.             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:
  104.  
  105. 1.) Submit the question again in an hour
  106.  
  107. 2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).
  108.  
  109. If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
  110.         myComment.mod.distinguish(how='yes', sticky=True)
  111.         post.mod.remove()
  112.  
  113.  
  114. # second main function; check for old, unflaired posts
  115.  
  116. def checkForOld(post):
  117.     if checkAge(post) > REMOVAL_MINS and post.link_flair_text == None \
  118.         and getInfo(post.id) == False:
  119.         myComment = \
  120.             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.
  121.  
  122.  
  123. Flair | Command
  124. ---|---
  125. Quick Question | -question
  126. Discussion | -discussion
  127. Analysis | -analysis
  128. News | -news
  129. Fan Art | -fanart
  130. Chat | -chat
  131. Humor | -humor
  132.  
  133.  
  134.  
  135. If you believe that this removal was a mistake, please contact the moderators.''')
  136.         myComment.mod.distinguish(how='yes', sticky=True)
  137.         post.mod.remove()
  138.         addPost(post.id, str(post.created), myComment.id, 'true')
  139.  
  140.  
  141. # third main function; database checker/complex question checking
  142.  
  143. def checkDatabase():
  144.     for row in c.execute('''SELECT * FROM rposts'''):
  145.         postId = row[0]
  146.         postCreated = row[1]
  147.         parId = row[2]
  148.         postRemoved = row[3]
  149.         actualPost = r.submission(id=postId)
  150.         if str(actualPost.author) == 'None':
  151.             deleteSpec(actualPost.id)
  152.         elif postRemoved == 'true':
  153.             if checkAge(actualPost) > FIX_MINS:
  154.                 r.comment(id=parId).edit('This post is older than 24 hours and can no longer be reapproved. Please resubmit and flair in time.'
  155.                         )
  156.                 deleteSpec(actualPost.id)
  157.             elif str(actualPost.link_flair_text).lower() \
  158.                 == 'quick question':
  159.                 r.comment(id=parId).delete()
  160.                 actualPost.mod.approve()
  161.                 deleteSpec(actualPost.id)
  162.                 addPost(postId, str(time.time() + 28800), 'None',
  163.                         'false')
  164.             elif actualPost.link_flair_text != None:
  165.  
  166.                 r.comment(id=parId).delete()
  167.                 actualPost.mod.approve()
  168.                 deleteSpec(actualPost.id)
  169.         elif (float(time.time()) + 28800 - float(postCreated)) / 60 \
  170.             > QUESTION_MINS:
  171.             myComment = \
  172.                 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:
  173.  
  174. 1.) Submit the question again in an hour
  175.  
  176. 2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).
  177.  
  178. If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
  179.             deleteSpec(actualPost.id)
  180.             myComment.mod.distinguish(how='yes', sticky=True)
  181.             actualPost.mod.remove()
  182.  
  183.  
  184. def runTimeBot():
  185.     for post in r.subreddit('FireEmblemHeroes').new(limit=20):
  186.         if str(post.author).lower() != 'automoderator':
  187.             checkQuestion(post)
  188.             checkForOld(post)
  189.     checkDatabase()
  190.     conn.commit()
  191.  
  192.  
  193. while True:
  194.     try:
  195.         runTimeBot()
  196.     except:
  197.         pass
  198.  
  199. conn.close()
Add Comment
Please, Sign In to add comment