Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. import praw
  2. import os
  3. import sys
  4. import time
  5. import re
  6. import tkinter
  7. import sqlite3
  8.  
  9. data = sqlite3.connect('checked.db')
  10. cur = data.cursor()
  11. cur.execute('CREATE TABLE IF NOT EXISTS checked(id, created, username)')
  12. data.commit()
  13.  
  14. os.system('@echo off')
  15. os.system('chcp 65001')
  16.  
  17. patterns = ['testing']
  18. word = re.compile(r'\w+')
  19.  
  20. def sign_in(*args):
  21.     reddit = praw.Reddit(username=args[0],password=args[1],client_id=args[2],client_secret=args[3], user_agent=args[4])
  22.     return reddit
  23.  
  24. class search_submission(object):
  25.  
  26.     def __init__(self, submission):
  27.         self.id = submission.id
  28.         self.created = submission.created_utc
  29.         self.submission = submission
  30.         self.title = submission.title
  31.         self.permalink = submission.permalink
  32.         self.author = submission.author.name
  33.         self.run()
  34.  
  35.     def submit_to_database(self):
  36.         cur.execute('INSERT INTO checked VALUES(?,?,?)',[self.id,self.created,self.author])
  37.         data.commit()
  38.  
  39.     def check_database(self):
  40.         cur.execute('SELECT * FROM checked WHERE id=?',[self.id])
  41.         if cur.fetchone():
  42.             return False
  43.         else:
  44.             return True
  45.  
  46.     def submission_message(self):
  47.         if self.check_database() is True:
  48.             reddit.redditor('iNeverQuiteWas').message('{}'.format(self.title),'[{}]({})'.format(self.title,self.permalink))
  49.             print('Sending message concerning {}'.format(self.author))
  50.             self.submit_to_database()
  51.  
  52.     def comment_message(self,author, __permalink__, body,pattern_matched):
  53.         if self.check_database() is True:
  54.             reddit.redditor('iNeverQuiteWas').message('Comment Match','Found a match for "{}" by /u/{}. Check the following link:\n\n[{}]({})'.format(pattern_matched,author,body,__permalink__))
  55.             print('Message sent')
  56.             self.submit_to_database()
  57.  
  58.     def check_regex(self):
  59.         title_text = word.findall(self.title)
  60.         next((self.message() for thing in title_text if any(pattern in thing for pattern in patterns)),None)
  61.  
  62.     def check_comments(self):
  63.         for comment in self.submission.comments:
  64.             try:
  65.                 comment_text = word.findall(comment.body.lower())
  66.                 next((self.comment_message(comment.author.name,comment.permalink(), comment.body, thing) for thing in comment_text if any(pattern in thing for pattern in patterns)),None)
  67.             except Exception as e:
  68.                 if e == KeyboardInterrupt:
  69.                     sys.exit()
  70.                 else:
  71.                     time.sleep(1)
  72.                     print('Unable to parse because {}'.format(e))
  73.  
  74.     def run(self):
  75.         self.check_comments()
  76.  
  77.  
  78. def main():
  79.     while True:
  80.         for submission in reddit.subreddit('all').hot(limit=1000):
  81.             try:
  82.                 search_submission(submission)
  83.             except Exception as e:
  84.                 if e == KeyboardInterrupt:
  85.                     sys.exit()
  86.                 else:
  87.                     pass
  88.  
  89.  
  90. if __name__ == '__main__':
  91.     reddit = sign_in('',
  92.         '',
  93.         '',
  94.         '',
  95.         '')
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement