forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_youtube_view.py
49 lines (40 loc) · 1.22 KB
/
get_youtube_view.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
"""
Created on Thu Apr 27 16:28:36 2017
@author: barnabysandeford
"""
# Currently works for Safari, but just change to whichever
# browser you're using.
import time
# Added pafy to get video length for the user
import pafy
# Changed the method of opening the browser.
# Selenium allows for the page to be refreshed.
from selenium import webdriver
# adding ability to change number of repeats
count = int(input("Number of times to be repeated: "))
# Same as before
url = input("Enter the URL : ")
refreshrate = None
# tries to get video length using pafy
try:
video = pafy.new(url)
if hasattr(video, "length"):
refreshrate = video.length
# if pafy fails to work, prints out error and asks for video length from the user
except Exception as e:
print(e)
print("Length of video:")
minutes = int(input("Minutes "))
seconds = int(input("Seconds "))
# Calculating the refreshrate from the user input
refreshrate = minutes * 60 + seconds
# Selecting Safari as the browser
driver = webdriver.Safari()
if url.startswith("https://"):
driver.get(url)
else:
driver.get("https://" + url)
for i in range(count):
# Sets the page to refresh at the refreshrate.
time.sleep(refreshrate)
driver.refresh()