Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- ######################################################################
- ## Reddit Bot that provides downloadable links for v.redd.it videos ##
- ######################################################################
- import praw
- import time
- import re
- import sys
- import random
- import urllib.request
- import os.path
- import os
- from pystreamable import StreamableApi
- import youtube_dl
- import smtplib
- donate = 'https://www.reddit.com/r/vreddit_bot/wiki/index'
- botPath = '/home/pi/bots/vreddit/'
- commentedPath = botPath + 'commented.txt'
- footer = (" \n *** \n ^^I\'m a Bot *bleep* *bloop* | [**Info**](https://np.reddit.com/r/vreddit_bot/comments/9h41sx/info) | [**Donate**](https://www.reddit.com/r/vreddit_bot/wiki/index)")
- format = '.mp4' #Change to .webm for longer processing and lower quality but drastically reduced upload size
- uploadText = ''
- ## Email Variables
- sent_from = 'EMAIL'
- to = ['EMAIL']
- subject = 'Uploading failed'
- body = "There was something wrong with the upload"
- email_text = """\
- From: %s
- To: %s
- Subject: %s
- %s
- """ % (sent_from, ", ".join(to), subject, body)
- gmail_user = 'USERNAME'
- gmail_password = 'PASSWORD'
- ################
- ## SEND EMAIL ##
- ################
- def sendGmail(gmail_user, gmail_password, sent_from, to, email_text):
- try:
- server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- server.ehlo()
- server.login(gmail_user, gmail_password)
- server.sendmail(sent_from, to, email_text)
- server.close()
- print('E-mail sent!')
- except:
- print('Something went wrong when sending E-Mail...')
- ######################
- api = StreamableApi('USERNAME', 'PASSWORD')
- def replying(mention, reply):
- #Replying to comment
- file_obj_r = open(commentedPath,'r')
- if mention.id not in file_obj_r.read().splitlines():
- print('Replying... \n')
- try:
- mention.reply(reply + footer)
- file_obj_r.close()
- file_obj_w = open(commentedPath,'a+')
- file_obj_w.write(mention.id + '\n')
- file_obj_w.close()
- #Send PM if replying went wrong (Should only happen if the bot is banned)
- except:
- sub = str(mention.subreddit)
- pm = reply + footer
- subject = 'I\'m probably banned in r/' + sub + ', so you get a PM instead ;)'
- print('Error when replying, probably banned. Replying per PM.')
- reddit.redditor(author).message(subject, pm)
- file_obj_r.close()
- file_obj_w = open(commentedPath,'a+')
- file_obj_w.write(mention.id + '\n')
- file_obj_w.close()
- #Authenticate via praw.ini file, look at praw documentation for more info
- def authenticate():
- print('Authenticating...\n')
- reddit = praw.Reddit('vreddit', user_agent = 'vreddit')
- print('Authenticated as {}\n'.format(reddit.user.me()))
- return reddit
- #############
- ## RUN BOT ##
- #############
- def run_bot(reddit):
- #Search mentions in inbox
- for mention in reddit.inbox.mentions(limit=10):
- parent = mention.parent()
- submissionURL = mention.submission.url
- submissionID = mention.submission.id
- submissionTitle = mention.submission.title
- author = str(mention.author)
- shouldUpload = False
- downloadTime = 0
- uploadTime = 0
- downloadedPath= botPath + 'downloaded/' + str(submissionID) + format
- uploadedPath = botPath + 'uploaded/' + str(submissionID) + '.txt'
- #Reply condition
- matchVreddit = 'v.redd.it' in submissionURL
- if matchVreddit:
- mention.upvote()
- #Generate downloadable v.redd.it URL, saves uploading bandwidth
- try:
- media_url = mention.submission.media['reddit_video']['fallback_url']
- media_url = str(media_url)
- audio_url = media_url.rpartition('/')[0] + '/audio'
- shouldUpload = False
- #Upload if something doesn't work out (Usually a x-posted link which doesn't get recognized for some reason, fix in the future)
- except Exception as e:
- #print(e) #Uncomment to see error
- media_url = submissionURL
- shouldUpload = True
- #Workaround to check if v.redd.it link has audio, raises exception if it doesn't.
- try:
- req = urllib.request.Request(audio_url)
- resp = urllib.request.urlopen(req)
- respData = resp.read()
- shouldUpload = True
- except:
- audio = False #Needs something here
- #shouldUpload = True #Uncomment for always upload
- #Check if the file was downloaded or uploaded before
- downloaded = os.path.exists(downloadedPath)
- uploaded = os.path.exists(uploadedPath)
- #Check if file has already been uploaded (if the .txt file with the link was generated before, to avoid uploading the same video multiple times)
- if uploaded:
- try:
- with open(uploadedPath, 'r') as content_file:
- uploadedURL = content_file.read()
- txt = True
- except Exception as e:
- print(e)
- print("Couldn't get URL, continuing..")
- else:
- txt = False
- #DOWNLOAD
- if shouldUpload and not uploaded:
- if not downloaded and not uploaded:
- try:
- print('Downloading "' + submissionTitle + '"')
- ydl_opts = {
- 'outtmpl': downloadedPath,
- #'format': 'bestvideo', #uncomment for video without audio only, see youtube-dl documentation
- 'max_filesize': int('200000000'),
- 'ratelimit': 2000000,
- }
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
- startTimeDownload = time.time()
- ydl.download([submissionURL])
- downloaded = True
- downloadTime = time.time() - startTimeDownload
- except Exception as e:
- print('ERROR: Downloading failed.')
- print(e)
- downloaded = False
- elif downloaded and not uploaded:
- print('Already downloaded "' + submissionTitle + '"')
- elif not shouldUpload:
- uploadedURL = media_url
- #Check again if downloaded file exists now
- downloaded = os.path.exists(downloadedPath)
- ###########################
- ## UPLOADING ##
- ###############
- if downloaded and not uploaded and shouldUpload:
- try:
- print('Uploading file.')
- startTime = time.time()
- stream = api.upload_video(downloadedPath, submissionID)
- shortcode = stream['shortcode']
- uploadedURL = 'https://streamable.com/' + shortcode
- print("Successfully uploaded: " + uploadedURL)
- uploadTime = time.time() - startTime
- uploaded = True
- #Delete downloaded file
- try:
- print('Deleting downloaded file.')
- os.remove(downloadedPath)
- except Exception as e:
- print(e)
- print("ERROR: Can't remove file.")
- except Exception as e:
- print(e)
- print("ERROR: Can't upload file. Sending email and terminating..")
- sendGmail(gmail_user, gmail_password, sent_from, to, email_text)
- uploaded = False
- sys.exit()
- #########################
- ## REPLYING #
- #############
- #Generate normal comment reply string
- if uploaded or not shouldUpload:
- if uploaded and not txt:
- timeText = "\n\nIt took " + "%.f" % downloadTime + " seconds to download " \
- + "and " + "%.f" % uploadTime + " seconds to upload.\n"
- else:
- timeText = ""
- try:
- reply = "Downloadable link: " + uploadedURL + timeText
- except Exception as e:
- print(e)
- #Generate reply string if something went wrong
- elif (not downloaded or not uploaded) and shouldUpload:
- try:
- media_url = mention.submission.media['reddit_video']['fallback_url']
- reply = 'Sorry, there was a problem. [Here](' + media_url + ') is a downloadable v.redd.it link without audio.'
- except:
- reply = 'Sorry, something went wrong :( If the submission is a x-post, try mentioning my name on the original post.'
- #Replying to comment
- file_obj_r = open(commentedPath,'r')
- if mention.id not in file_obj_r.read().splitlines():
- print('Replying... \n')
- try:
- mention.reply(reply + footer)
- file_obj_r.close()
- file_obj_w = open(commentedPath,'a+')
- file_obj_w.write(mention.id + '\n')
- file_obj_w.close()
- #Send PM if replying went wrong (Should only happen if the bot is banned)
- except:
- message = "\n\nIf you wanna help, you could edit your original post to include my link :)"
- sub = str(mention.subreddit)
- pm = reply + message + footer
- subject = 'I\'m probably banned in r/' + sub + ', so you get a PM instead ;)'
- print('Error when replying, probably banned. Replying per PM.')
- reddit.redditor(author).message(subject, pm)
- file_obj_r.close()
- file_obj_w = open(commentedPath,'a+')
- file_obj_w.write(mention.id + '\n')
- file_obj_w.close()
- #########################
- ## FINISH ##
- #############
- #Create .txt file with streamable link, named after the submission ID
- if uploaded and not txt:
- try:
- print('Creating txt file.')
- f = open(uploadedPath,"w+")
- f.write(uploadedURL)
- f.close()
- except Exception as e:
- print(e)
- print("ERROR: Can't create txt file.")
- else:
- reply = 'Sorry, I only work with v.redd.it links.'
- #Replying to comment
- file_obj_r = open(commentedPath,'r')
- if mention.id not in file_obj_r.read().splitlines():
- print('Replying... \n')
- try:
- mention.reply(reply + footer)
- file_obj_r.close()
- file_obj_w = open(commentedPath,'a+')
- file_obj_w.write(mention.id + '\n')
- file_obj_w.close()
- #Send PM if replying went wrong (Should only happen if the bot is banned)
- except:
- print('Error when replying and no valid link.')
- file_obj_r.close()
- #########################
- #print('Waiting 5 seconds...\n')
- time.sleep(5)
- def main():
- reddit = authenticate()
- while True:
- run_bot(reddit)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment