Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.79 KB | None | 0 0
  1. import praw
  2. import re
  3. import sqlite3
  4. import time
  5. import threading
  6. from threading import Thread
  7. import datetime
  8. from datetime import timedelta
  9.  
  10.  
  11.     reddit = praw.Reddit(client_id = 'clientid',
  12.                         client_secret = 'clientsecret',
  13.                         username = 'keepdankmemesdank',
  14.                         user_agent = 'Additional help in moderating /r/dankmemes v2.0 (fixed the messy code)',
  15.                         password = 'passwordgoeshere')
  16.  
  17. def new_submissions():
  18.     conn = sqlite3.connect('dankmemes.db')
  19.     c = conn.cursor()
  20.     c.execute('''CREATE TABLE IF NOT EXISTS comments (comment_id, parent_post, date_time, reported)''')
  21.     conn.commit()
  22.     conn.close()
  23.  
  24.     sub = reddit.subreddit('dankmemes')
  25.  
  26.     for submission in sub.new(limit=100):
  27.         process_submission(submission)
  28.         time.sleep(2)
  29.  
  30. def add_comment_db(comment, submission):
  31.     conn = sqlite3.connect('dankmemes.db')
  32.     c = conn.cursor()
  33.     c.execute('''CREATE TABLE IF NOT EXISTS comments(comment_id, parent_post, date_time, reported)''')
  34.     comment_id = comment.id
  35.     post_id = submission.id
  36.     current_time = datetime.datetime.now()
  37.     c.execute('''INSERT INTO comments(comment_id, parent_post, date_time) VALUES(?,?,?)''', (comment_id, post_id, current_time))
  38.     conn.commit()
  39.     conn.close()
  40.  
  41. def check_db(submission):
  42.     conn = sqlite3.connect('dankmemes.db')
  43.     c = conn.cursor()
  44.     post_id = submission.id
  45.     posts_db = c.execute('''SELECT * FROM comments WHERE parent_post=? LIMIT 1''',[post_id])
  46.     conn.commit()
  47.     if posts_db.fetchone():
  48.         conn.close()
  49.         return "found"
  50.     else:
  51.         conn.close()
  52.         return
  53.  
  54. def process_submission(submission):
  55.     check = check_db(submission)
  56.     if(check):
  57.         pass
  58.     else:
  59.         comment = submission.reply("#Upvote this comment if this is a **DANK MEME**. Downvote this comment if this is a **NORMIE MEME**.")
  60.         comment.mod.distinguish(how='yes', sticky=True)
  61.         comment.mod.approve()
  62.         add_comment_db(comment,submission)
  63.  
  64.  
  65. def check_scores():
  66.     time.sleep(1)
  67.     conn = sqlite3.connect('dankmemes.db')
  68.     c = conn.cursor()
  69.     c.execute('''CREATE TABLE IF NOT EXISTS comments (comment_id, parent_post, date_time, reported)''')
  70.     conn.commit()
  71.     conn.close()
  72.  
  73.     user = reddit.redditor('keepdankmemesdank')
  74.     for comment in user.comments.new(limit=400):
  75.         comment_id = comment.id
  76.         conn = sqlite3.connect('dankmemes.db')
  77.         c = conn.cursor()
  78.         posts_db = c.execute('''SELECT * FROM comments WHERE comment_id=?''',[comment_id]).fetchone()
  79.         if(posts_db):
  80.             removed = posts_db[2]
  81.             if(removed == "yes"):
  82.                 conn.close()
  83.                 continue
  84.             else:
  85.                 conn.close()
  86.                 if(comment.score < -6):
  87.                     if(comment.score < -20):
  88.                         remove_post(comment, reddit)
  89.                     report_post(comment, reddit)
  90.  
  91.         time.sleep(2)
  92.  
  93. def report_post(comment, reddit):
  94.     conn = sqlite3.connect('dankmemes.db')
  95.     c = conn.cursor()
  96.     comment_id = comment.id
  97.     posts_db = c.execute('''SELECT * FROM comments WHERE comment_id=?''',[comment_id]).fetchone()
  98.     current = posts_db[1]
  99.     post = reddit.submission(current)
  100.     #msg_sub = reddit.subreddit('dankmemes')
  101.     #post_link = post.shortlink
  102.     #message_send = 'Mods, Please check this post, it reached the downvote threshold.  [Link](%s)' % post_link
  103.     #msg_sub.message('Check Post', message_send)
  104.     post.report("Not Dank.")
  105.     c.execute('''UPDATE comments SET reported="yes" WHERE comment_id =?''', [comment_id])
  106.     conn.commit()
  107.     conn.close()
  108.  
  109. def remove_post(comment, reddit):
  110.     conn = sqlite3.connect('dankmemes.db')
  111.     c = conn.cursor()
  112.     comment_id = comment.id
  113.     posts_db = c.execute('''SELECT * FROM comments WHERE comment_id=?''',[comment_id]).fetchone()
  114.     current = posts_db[1]
  115.     post = reddit.submission(current)
  116.     post.mod.remove()
  117.     c.execute('''UPDATE comments SET reported="yes" WHERE comment_id =?''', [comment_id])
  118.     conn.commit()
  119.     conn.close()
  120.  
  121.  
  122. def clearunmoderated():
  123.  
  124.     sub = reddit.subreddit('dankmemes')
  125.  
  126.     for submission in sub.mod.unmoderated(limit=None):
  127.         if(submission.score < 10):
  128.             ct = datetime.datetime.utcnow() - timedelta(hours=5)
  129.             sub_t = datetime.datetime.fromtimestamp(submission.created_utc)
  130.             diff = ct - sub_t
  131.             hours = round(diff.total_seconds()/3600)
  132.             if(hours > 1):
  133.                 submission.mod.remove()
  134.             time.sleep(3)
  135.         else:
  136.             ct = datetime.datetime.utcnow() - timedelta(hours=5)
  137.             sub_t = datetime.datetime.fromtimestamp(submission.created_utc)
  138.             diff = ct - sub_t
  139.             hours = round(diff.total_seconds()/3600)
  140.             if(hours > 23):
  141.                 submission.mod.approve()
  142.             time.sleep(3)
  143.  
  144.  
  145. def clearcontroversial():
  146.  
  147.     sub = reddit.subreddit('dankmemes')
  148.  
  149.     for submission in sub.controversial('day', limit=None):
  150.  
  151.         if(submission.score < 10):
  152.             ct = datetime.datetime.utcnow() - timedelta(hours=5)
  153.             sub_t = datetime.datetime.fromtimestamp(submission.created_utc)
  154.             diff = ct - sub_t
  155.             hours = round(diff.total_seconds()/3600)
  156.             if(hours > 1):
  157.                 submission.mod.remove()
  158.             time.sleep(3)
  159.  
  160. def clear_db():
  161.     conn = sqlite3.connect('dankmemes.db')
  162.     c = conn.cursor()
  163.     c.execute("DELETE FROM comments WHERE comment NOT in(SELECT * FROM comments ORDER BY date(date_time) DESC LIMIT 500)")
  164.     conn.commit()
  165.     conn.close()
  166.  
  167.  
  168. while True:
  169.     new_submissions()
  170.     clearcontroversial()
  171.     clearunmoderated()
  172.     check_scores()
  173.     clear_db()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement