Advertisement
Guest User

Untitled

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