Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. import praw
  2. import time
  3. import sys
  4. import sqlite3
  5. import re
  6. import datetime
  7. from selenium import webdriver
  8. from selenium.webdriver.common.keys import Keys
  9.  
  10. data = sqlite3.connect('MMA.db')
  11. cur = data.cursor()
  12. cur.execute('CREATE TABLE IF NOT EXISTS checked (id)')
  13. data.commit()
  14. reddit = praw.Reddit(username='MMAMirrorBot',password='123456',client_id='ezW4oD2FczPp4w',client_secret='625wK17XwEZgN0DmP-lI8XFrgeI',user_agent='A rehosting agent for /r/MMA')
  15. sites = ['streamable','twitter','oddshot','yout']
  16.  
  17. class Rehost(object):
  18.  
  19.     def __init__(self, submission):
  20.         self.submission = submission
  21.         self.url = submission.url
  22.         self.id = submission.id
  23.  
  24.     def check_url(self):
  25.         print(self.url)
  26.         if any(site in str(self.url) for site in sites):
  27.             print('Matched a site!')
  28.             return True
  29.         else:
  30.             return False
  31.  
  32.     def check_database(self):
  33.         cur.execute('SELECT * FROM checked WHERE id=?',[self.id])
  34.         if not cur.fetchone():
  35.             return True
  36.         else:
  37.             return False
  38.  
  39.     def submit_to_database(self):
  40.         cur.execute('INSERT INTO checked VALUES(?)',[self.id])
  41.         data.commit()
  42.         print('Saved to database!')
  43.  
  44.     def upload_video(self):
  45.         if self.check_url() is True and self.check_database() is True:
  46.             driver = webdriver.Chrome()
  47.             driver.get('https://vid.me/')
  48.             driver.find_element_by_xpath('//*[@id="index"]/div[1]/div/div/div/a[1]').click()
  49.             time.sleep(2)
  50.             driver.find_element_by_xpath('//*[@id="uploader-modal"]/form/div/div/div[2]/div[2]/div[1]/input').send_keys(self.url)
  51.             time.sleep(2)
  52.             driver.find_element_by_xpath('//*[@id="uploader-modal"]/form/div/div/div[3]/button[1]').click()
  53.             time.sleep(3)
  54.             driver.find_element_by_xpath('/html/body/div/div[3]/div/div/div/div/ul/li/div[1]/div[1]/a').click()
  55.             time.sleep(20)
  56.             url = str(driver.current_url)
  57.             driver.quit()
  58.             return url
  59.  
  60. class botResponse(object):
  61.  
  62.     def __init__(self, submission):
  63.         self.submission = submission
  64.         self.created = submission.created_utc
  65.         self.no_response = time.mktime(datetime.datetime.now().timetuple()) - (24*60*60)
  66.         self.title = submission.title
  67.         self.permalink = submission.permalink
  68.         self.author = submission.author.name
  69.  
  70.     def return_format(self):
  71.         try:
  72.             post_url = Rehost(self.submission).upload_video()
  73.             if post_url is None:
  74.                 raise Exception('No url')
  75.             preface = 'In case this video is removed or lost, I have mirrored it\n\n'
  76.             body = '[{} - Mirrored]({})'.format(re.sub('\s+', ' ',str(self.title)), post_url)
  77.             disclaimer = '\n\n---\n\n^^I ^^am ^^a ^^bot ^^created ^^by ^^[/u/iNeverQuiteWas](/u/iNeverQuiteWas), ^^please ^^contact ^^him ^^if ^^you ^^have ^^any ^^questions ^^or ^^would ^^like ^^a ^^bot ^^of ^^your ^^own'
  78.             response = preface+body+disclaimer
  79.             return response
  80.         except:
  81.             raise Exception
  82.  
  83.     def check_no_response(self):
  84.         if self.created - int(self.no_response) > 0:
  85.             return True
  86.         else:
  87.             return False
  88.  
  89.     def reply_to_submission(self):
  90.         print(self.author)
  91.         if self.check_no_response() is True:
  92.             print("Replying...")
  93.             response = self.return_format()
  94.             comment = self.submission.reply(response)
  95.             comment.mod.distinguish()
  96.             print('Replied to /u/{}'.format(self.author))
  97.         else:
  98.             print('Passing because later than a day old')
  99.  
  100.     def run(self):
  101.         self.reply_to_submission()
  102.         self.set_sticky()
  103.  
  104. def main():
  105.     while True:
  106.         print('Starting set...')
  107.         for submission in reddit.subreddit('MMA').new(limit=100):
  108.             try:
  109.                 botResponse(submission).run()
  110.             except Exception as e:
  111.                 time.sleep(2)
  112.                 if e is KeyboardInterrupt:
  113.                     sys.exit()
  114.                 else:
  115.                     print(e)
  116.                     pass
  117.         print('Finished set...')
  118.         time.sleep(60*2)
  119.  
  120. if __name__ == '__main__':
  121.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement