Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import time
- import sys
- import sqlite3
- import re
- import datetime
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- data = sqlite3.connect('MMA.db')
- cur = data.cursor()
- cur.execute('CREATE TABLE IF NOT EXISTS checked (id)')
- data.commit()
- reddit = praw.Reddit(username='MMAMirrorBot',password='123456',client_id='ezW4oD2FczPp4w',client_secret='625wK17XwEZgN0DmP-lI8XFrgeI',user_agent='A rehosting agent for /r/MMA')
- sites = ['streamable','twitter','oddshot','yout']
- class Rehost(object):
- def __init__(self, submission):
- self.submission = submission
- self.url = submission.url
- self.id = submission.id
- def check_url(self):
- print(self.url)
- if any(site in str(self.url) for site in sites):
- print('Matched a site!')
- return True
- else:
- return False
- def check_database(self):
- cur.execute('SELECT * FROM checked WHERE id=?',[self.id])
- if not cur.fetchone():
- return True
- else:
- return False
- def submit_to_database(self):
- cur.execute('INSERT INTO checked VALUES(?)',[self.id])
- data.commit()
- print('Saved to database!')
- def upload_video(self):
- if self.check_url() is True and self.check_database() is True:
- driver = webdriver.Chrome()
- driver.get('https://vid.me/')
- driver.find_element_by_xpath('//*[@id="index"]/div[1]/div/div/div/a[1]').click()
- time.sleep(2)
- driver.find_element_by_xpath('//*[@id="uploader-modal"]/form/div/div/div[2]/div[2]/div[1]/input').send_keys(self.url)
- time.sleep(2)
- driver.find_element_by_xpath('//*[@id="uploader-modal"]/form/div/div/div[3]/button[1]').click()
- time.sleep(3)
- driver.find_element_by_xpath('/html/body/div/div[3]/div/div/div/div/ul/li/div[1]/div[1]/a').click()
- time.sleep(20)
- url = str(driver.current_url)
- driver.quit()
- return url
- class botResponse(object):
- def __init__(self, submission):
- self.submission = submission
- self.created = submission.created_utc
- self.no_response = time.mktime(datetime.datetime.now().timetuple()) - (24*60*60)
- self.title = submission.title
- self.permalink = submission.permalink
- self.author = submission.author.name
- def return_format(self):
- try:
- post_url = Rehost(self.submission).upload_video()
- if post_url is None:
- raise Exception('No url')
- preface = 'In case this video is removed or lost, I have mirrored it\n\n'
- body = '[{} - Mirrored]({})'.format(re.sub('\s+', ' ',str(self.title)), post_url)
- 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'
- response = preface+body+disclaimer
- return response
- except:
- raise Exception
- def check_no_response(self):
- if self.created - int(self.no_response) > 0:
- return True
- else:
- return False
- def reply_to_submission(self):
- print(self.author)
- if self.check_no_response() is True:
- print("Replying...")
- response = self.return_format()
- comment = self.submission.reply(response)
- comment.mod.distinguish()
- print('Replied to /u/{}'.format(self.author))
- else:
- print('Passing because later than a day old')
- def run(self):
- self.reply_to_submission()
- self.set_sticky()
- def main():
- while True:
- print('Starting set...')
- for submission in reddit.subreddit('MMA').new(limit=100):
- try:
- botResponse(submission).run()
- except Exception as e:
- time.sleep(2)
- if e is KeyboardInterrupt:
- sys.exit()
- else:
- print(e)
- pass
- print('Finished set...')
- time.sleep(60*2)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement