Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1.  
  2. import praw
  3. import os
  4. import sys
  5. import time
  6. import re
  7. import tkinter
  8. import sqlite3
  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 = 'pics'
  16.  
  17. os.system('@echo off')
  18. os.system('chcp 65001')
  19.  
  20. patterns = ['patterns go here']
  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:
  51. reddit.redditor('').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. time.sleep(1)
  79. print('Unable to parse because {}'.format(e))
  80.  
  81. def run(self):
  82. self.check_comments()
  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.  
  96.  
  97. if __name__ == '__main__':
  98. reddit = sign_in()
  99. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement