Guest User

Untitled

a guest
Jan 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 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. import datetime
  9.  
  10. data = sqlite3.connect('checked.db')
  11. cur = data.cursor()
  12. cur.execute('CREATE TABLE IF NOT EXISTS checked(id, created, username)')
  13. data.commit()
  14.  
  15. sub = 'Discgolf'
  16.  
  17. os.system('@echo off')
  18. os.system('chcp 65001')
  19.  
  20. patterns = ['MVP','gyro','overmold','energy','nitro','octane','catalyst','phase','photon','wave','orbital','motion','tesla','inertia','impulse','shock','volt','amp','resistor','servo','switch','relay','signal','matrix','vector','axis','tensor','tangent','ion','anode','spin','atom','axiom','thrill','defy','vanish','fireball','wrath','insanity','virus','clash','crave','inspire','alias','theory','envy','proxy','streamline','streamlined','solomold','lost','portal','pro','basket','voyager']
  21. word = re.compile(r'\w+')
  22.  
  23. def sign_in(*args):
  24.     reddit = praw.Reddit(username=args[0],password=args[1],client_id=args[2],client_secret=args[3], user_agent=args[4])
  25.     return reddit
  26.  
  27. class search_submission(object):
  28.  
  29.     def __init__(self, submission):
  30.         self.id = submission.id
  31.         self.created = submission.created_utc
  32.         self.submission = submission
  33.         self.title = submission.title
  34.         self.permalink = submission.permalink
  35.         self.author = submission.author.name
  36.         self.run()
  37.  
  38.     def submit_to_database(self):
  39.         cur.execute('INSERT INTO checked VALUES(?,?,?)',[self.id,self.created,self.author])
  40.         data.commit()
  41.  
  42.     def check_database(self):
  43.         cur.execute('SELECT * FROM checked WHERE id=?',[self.id])
  44.         if cur.fetchone():
  45.             return False
  46.         else:
  47.             return True
  48.  
  49.     def message(self):
  50.         if self.check_database() is True and time.mktime(datetime.datetime.now().timetuple()) - self.submission.created_utc < (60*60*1.5):
  51.             reddit.redditor('MVP_Steve').message('{}'.format(self.title),'[{}]({})'.format(self.title,self.permalink))
  52.             print('Sending message concerning {}'.format(self.author))
  53.             self.submit_to_database()
  54.  
  55.     def comment_message(self,author, __permalink__, body,pattern_matched):
  56.         if self.check_database() is True:
  57.             reddit.redditor('****').message('Comment Match','Found a match for "{}" by /u/{}. Check the following link:\n\n[{}]({})'.format(pattern_matched,author,body,__permalink__))
  58.             print('Message sent')
  59.             self.submit_to_database()
  60.  
  61.     def check_regex(self):
  62.         title_text = word.findall(self.title)
  63.         next((self.message() for thing in title_text if any(pattern in thing for pattern in patterns)),None)
  64.         if any(pattern in self.title for pattern in patterns if ' ' in pattern):
  65.             self.message()
  66.  
  67.     def check_comments(self):
  68.         for comment in self.submission.comments:
  69.             try:
  70.                 comment_text = word.findall(comment.body.lower())
  71.                 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)
  72.                 if any(pattern in comment.body.lower() for pattern in patterns if ' ' in pattern):
  73.                     self.comment_message(comment.author.name,comment.permalink(), comment.body, pattern)
  74.             except Exception as e:
  75.                 if e == KeyboardInterrupt:
  76.                     sys.exit()
  77.                 else:
  78.                     pass
  79.  
  80.     def run(self):
  81.         self.check_comments()
  82.         self.check_regex()
  83.  
  84.  
  85. def main():
  86.     while True:
  87.         for submission in reddit.subreddit(sub).hot(limit=1000):
  88.             try:
  89.                 search_submission(submission)
  90.             except Exception as e:
  91.                 if e == KeyboardInterrupt:
  92.                     sys.exit()
  93.                 else:
  94.                     pass
  95.         time.sleep(2)
  96.  
  97.  
  98. if __name__ == '__main__':
  99.     reddit = sign_in('DataCollectionBot',
  100.         'w0rd543rch1ng',
  101.         'xIZ1lwxV5P5tVQ',
  102.         'areezNIh8UbUIkBz6icMAf7GFZQ',
  103.         'An agent to search for words on Reddit')
  104.     main()
Add Comment
Please, Sign In to add comment