-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcybrary-video-downloader.py
executable file
·74 lines (59 loc) · 2.21 KB
/
cybrary-video-downloader.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
import os
import getpass
import requests
from bs4 import BeautifulSoup
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--quality', choices=[360, 720], default=360,type=int, help="Select video quality")
parser.add_argument('--course', default="https://www.cybrary.it/course/ethical-hacking/", help="Course link")
parser.add_argument('--ssl', choices=["True", "False"], default="True" , help="Turn on/off SSL")
args = parser.parse_args()
# Make SSL argument boolean
args.ssl = bool(args.ssl)
# Global Session Variables
session = requests.session()
# Initialize Login
def login(username, password):
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
}
values = {
'log': username,
'pwd': password,
'testcookie': 1,
'wp-submit': 'Log+In',
'redirect_to': ''
}
# Submit username and password for login
global session
session.post('https://www.cybrary.it/wp-login.php', data=values, headers=headers)
# Parse course links and download videos
def downloadCourseVideos(quality, course):
global session
courseHTML = (session.get(course, verify=args.ssl)).text
parsedCourseHTML = BeautifulSoup(courseHTML, 'html.parser')
for lessonLink in parsedCourseHTML.find_all('a', attrs={'class':'title'}):
lessonHTML = (session.get(lessonLink.get('href'), verify=args.ssl)).text
parsedLessonHTML = BeautifulSoup(lessonHTML, 'html.parser')
videoLink = parsedLessonHTML.find('iframe', attrs={'class':'sv_lessonvideo'})
if videoLink:
downloadVideo(videoLink.get('src'), quality)
# Download video using youtube-dl
def downloadVideo(videoLink, quality):
# Apparently, cybrary.it uses vimeo to host their videos and this might change at anytime.
# Feel free to submit an issue if errors exist
# If windows
if os.name == 'nt':
command = "youtube-dl.exe -cif http-%sp %s --referer https://www.cybrary.it/" % (quality, videoLink)
# *nix
else:
command = "youtube-dl -cif http-%sp %s --referer https://www.cybrary.it/" % (quality, videoLink)
os.system(command)
# Main function
def main():
username = raw_input("Username: ")
password = getpass.getpass()
login(username, password)
downloadCourseVideos(args.quality, args.course)
if __name__ == '__main__':
main()