Guest User

Untitled

a guest
Mar 30th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import os
  2. import praw
  3. import sqlite3
  4. import config
  5. import time
  6. import urllib.request as request
  7. from PIL import Image
  8.  
  9.  
  10. reddit = praw.Reddit(username=config.USERNAME, password=config.PASSWORD, client_id=config.CLIENT_ID, client_secret=config.CLIENT_SECRET,user_agent=config.USER_AGENT)
  11.  
  12. data = sqlite3.connect('pics.db')
  13. cur = data.cursor()
  14. cur.execute('CREATE TABLE IF NOT EXISTS hasRemoved(id, author, image)')
  15. data.commit()
  16.  
  17. class Cancer(object):
  18.  
  19. def __init__(self, submission, size):
  20. self.submission = submission
  21. self.size = size
  22. self.url = submission.url
  23. self.id = submission.id
  24. self.author = str(submission.author)
  25. self.query = 'SELECT * FROM hasRemoved WHERE id=?'
  26. self.entry = 'INSERT INTO hasRemoved VALUES(?,?,?)'
  27. self.outfile = ['.jpg','.png']
  28. self.cancer()
  29.  
  30. def checkDatabase(self):
  31. cur.execute(self.query, [self.id])
  32. if cur.fetchone():
  33. return True
  34. else:
  35. return False
  36.  
  37. def submitToDatabase(self):
  38. cur.execute(self.entry, [self.id, self.author, self.url])
  39. data.commit()
  40. return True
  41.  
  42. def checkSize(self):
  43. try:
  44. if '/a/' in self.url:
  45. self.submission.mod.remove()
  46. pass
  47. try:
  48. image = request.urlretrieve(self.url, config.PATH+self.outfile[0])
  49. image = Image.open(config.PATH+self.outfile[0])
  50. image_size = image.size
  51. except Exception as e:
  52. print('Exception:',e)
  53. image = request.urlretrieve(self.url, config.PATH+self.outfile[0])
  54. image = Image.open(config.PATH+self.outfile[1])
  55. image_size = image.size
  56. print(image_size)
  57. if self.size[0] < image_size[0] or self.size[1] < image_size[1]:
  58. print(True)
  59. return True
  60. else:
  61. return False
  62. except Exception as e:
  63. print(e)
  64. #self.submission.mod.remove()
  65. return None
  66.  
  67. def cancer(self):
  68. try:
  69. if self.checkDatabase() is False and self.checkSize() is True:
  70. self.submission.reply(config.REMOVAL_MESSAGE).mod.distinguish(sticky=True)
  71. self.submission.mod.remove()
  72. self.submitToDatabase()
  73. return True
  74. except Exception as e:
  75. print(e)
  76. self.submitToDatabase()
  77.  
  78.  
  79. def main():
  80. while True:
  81. for submission in reddit.subreddit(config.SUBREDDIT).new(limit=config.LIMIT):
  82. current_day = int(time.strftime('%d')); current_month = int(time.strftime('%m'))
  83. if int(time.strftime('%d'), time.localtime(submission.created_utc)) in range(current_day-1, current_day+1) and int(time.strftime('%m'), time.localtime(submission.created_utc)) == current_month:
  84. Cancer(submission, config.SIZE)
  85.  
  86. if __name__ == '__main__':
  87. main()
Add Comment
Please, Sign In to add comment