Skip to content

Instantly share code, notes, and snippets.

@johannesmols
Created February 14, 2017 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannesmols/056a4ed4ed9beb3eafa8a559a1cf5166 to your computer and use it in GitHub Desktop.
Save johannesmols/056a4ed4ed9beb3eafa8a559a1cf5166 to your computer and use it in GitHub Desktop.
import pafy #Docs: http://pythonhosted.org/pafy/
import praw
import urllib.parse
import credentials
import time
#Read and Write Files Doc: https://www.tutorialspoint.com/python3/python_files_io.htm
def load_cache_on_startup(): #loads cached comments on startup in the list
try:
file = open('cache.txt', "r") #r = read only
cached_comments = file.read()
id_list = cached_comments.split(',')
return id_list
except Exception as e:
print('Error loading cache on startup')
words_to_match = ['youtube.com/watch?v=', 'youtu.be/'] #search criteria for youtube links
blacklist = ['pathofexile']
cache = load_cache_on_startup()
def main():
reddit = praw.Reddit(user_agent=credentials.user_agent, client_id=credentials.client_id, client_secret=credentials.client_secret,
username=credentials.username, password=credentials.password)
subreddit = reddit.subreddit('all')
comments = subreddit.comments(limit=100)
for comment in comments:
comment_text = comment.body
isMatch = any(string in comment_text for string in words_to_match) #finds out if a comment has a youtube link in it
if comment.id not in cache and isMatch and comment.subreddit.display_name.lower() not in blacklist:
#Get the whole video url
try:
url = get_youtube_url(str(comment.body))
output = construct_table(url) #construct the comment in reddit formatting for tables
comment.reply(output)
print("Reply successfull")
cache.append(comment.id)
with open('cache.txt', 'a') as out: #a = appending modus
out.write(comment.id + ',') #append comment id to cache
update_cache()
except Exception as e:
print('Error')
continue
elif comment.id in cache:
print("ID already in cache")
print('Done with 100 comments, wait until restart')
def update_cache():
file = open('cache.txt', "r") #r = read only
cached_comments = file.read()
cache.append(cached_comments.split(',')) #split by , and put in the list
def get_youtube_url(comment_text):
url = str(comment_text).split("/") #split into it's parts
stepone = str(url[2]) + "/" + str(url[3]) #left part cutted
steptwo = str(stepone).split("&ab_channel=") #split right part
return steptwo[0]
def construct_table(url):
video = pafy.new(url)
metadata = {'title':str(video.title), 'author':str(video.author), 'viewcount':str(video.viewcount), 'duration':str(video.duration),
'likes':str(video.likes), 'dislikes':str(video.dislikes), 'rating':str(video.rating), 'date':str(video.published),
'description':str(video.description), 'category':str(video.category), 'keywords':str(video.keywords), 'thumbnail':str(video.thumb), 'id':str(video.videoid)}
output = ( "Hello! It looks like you posted a YouTube video. I am here to provide you with unncessecary meta information about the video. Enjoy!\n\n" +
"|Category|Information|\n" + ":--|:--\n" + "Title" + "|" + metadata['title'] + "\n" +
"Author" + "|" + metadata['author'] + "\n" +
"Views" + "|" + str(format(int(metadata['viewcount']), ',d')) + "\n" + #format to have commas for every thousand
"Duration" + "|" + metadata['duration'] + "\n" +
"Likes" + "|" + metadata['likes'] + "\n" + #doesn't show up
"Dislikes" + "|" + metadata['dislikes'] + "\n" + #doesn't show up
"Rating" + "|" + metadata['rating'] + "\n" + #cut decimals
"Upload Date" + "|" + metadata['date'] + "\n" + #format into a better readable format
#"Description" + "|" + metadata['description'] + "\n" + #way tooo long sometimes
"Category" + "|" + metadata['category'] + "\n" +
"Keywords" + "|" + metadata['keywords'] + "\n" + #remove brackets and '
"Thumbnail" + "|" + "[Thumbnail](" + metadata['thumbnail'] + ")" + "\n" +
"Video ID" + "|" + metadata['id'] + "\n" +
"I'm a bot working hard to help Redditors find easy information about YouTube videos posted here." + "" +
"If you are unhappy with my services or found a bug, please [write a message](https://www.reddit.com/message/compose?to=maggiforever) to my creator /u/maggiforever."
)
return output
#run the program
while True:
try:
main()
time.sleep(10)
except Exception as e:
print('Error')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment