-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoposter.py
104 lines (95 loc) · 4.49 KB
/
videoposter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# videoposter.py is reddit bot that scrapes a website to find
# the last video posted on it and creates a reddit submission
# with a link to the video, then it posts the youtube channel
# mirror as a comment in the submission.
# Made in Python 3.4.3, by /u/MorrisCasper and /u/twistitup.
import requests, time, praw, oaux, traceback
# install with "pip install beautifulsoup4"
from bs4 import BeautifulSoup
# install with "pip install --upgrade google-api-python-client"
from apiclient.discovery import build
from difflib import SequenceMatcher
### USER CONFIGURATION ###
SLEEP_TIME = 300 # Time to sleep between requesting the website in seconds
SUBREDDIT = "TwoBestFriendsPlay" # Without /r/
YOUTUBE_API_KEY = ""
HIGH_SIMILARITY = 0.99
LOW_SIMILARITY = 0.95
### END USER CONFIGURATION ###
# Returns the link and title of the lastest video on the SBFP website.
def get_latest_video():
r = requests.get("http://superbestfriendsplay.com/category/videos/", "lxml")
soup = BeautifulSoup(r.content, "html.parser")
latest_list_item = soup.find("li", {"class": "archiveitem"})
link = latest_list_item.find("div", {"class": "archivetitle"}).find("a").get("href")
title = latest_list_item.find("div", {"class": "archivetitle"}).find("a").text
return link, title
# Compares two strings and returns a float from 0.0 to 1.0 depending on
# the similarity of the strings.
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
# Returns whether or not the youtube video is found and the comment is posted.
def postYoutubeComment(title_ws, submission_posted, similarity):
service = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
playlistItems = service.playlistItems()
results = 50 # Maximum allowed number of requested videos
request = playlistItems.list(
part="snippet", playlistId="UU84X0epDRFdTrybxEX8ZWkA",
maxResults=results)
uploads = request.execute()
for i in range(0, results):
iterationTitle = uploads["items"][i]["snippet"]["title"]
# Compare how similar the two titles are. If it's almost
# certain that they are the same video, post the comment
if similar(iterationTitle, title_ws) > similarity:
print("Video title on youtube channel: " + iterationTitle)
print("Posting youtube url comment")
videoId = (uploads["items"][i]["snippet"]["resourceId"]["videoId"])
videoURL = "https://www.youtube.com/watch?v=" + videoId
submission_posted.add_comment(
"Here is the video on Youtube! " + videoURL)
print(
"Submission and comment posted. Sleeping",
SLEEP_TIME, "seconds.")
return True
return False
if __name__ == "__main__":
r = oaux.login()
subreddit = r.get_subreddit(SUBREDDIT)
latest_link_posted = ""
while True:
try:
link_ws, title_ws = get_latest_video() # Latest link and title from website
print("Latest video on website: " + title_ws)
if link_ws != latest_link_posted: # If link_ws not already posted
latest_link = link_ws
print("Posting latest video")
submission_posted = subreddit.submit(title_ws, url = link_ws)
# submission for testing purposes
# r.get_submission(submission_id = '3h5hzr')
if postYoutubeComment(
title_ws, submission_posted, HIGH_SIMILARITY) == False:
print(
"Submission posted but not comment.",
"Could not find appropriate youtube video.",
"Trying again with a lower similarity ratio.")
if postYoutubeComment(
title_ws, submission_posted,
LOW_SIMILARITY) == False:
print(
"Still couldn't find youtube video,",
"please find it and comment manually.",
"Sleeping", SLEEP_TIME, "seconds")
else:
print(
"Not posting because I already posted. Sleeping",
SLEEP_TIME, seconds)
time.sleep(SLEEP_TIME)
except KeyboardInterrupt:
print("Shutting down.")
break
except Exception as e:
print("Something bad happened!", e)
traceback.print_exc()
print("Waiting", SLEEP_TIME, "seconds.")
time.sleep(SLEEP_TIME)