-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
203 lines (157 loc) · 6.16 KB
/
scraper.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import logging
import re
import sys
from pathlib import Path
import json
def get_course_nodes(shib, url):
"""
BeautifulSoup can't parse JavaScript and RegEx is needed to extract
links to course nodes.
"""
response = shib.session.get(url)
c = response.text
p = re.compile("\"href\"\:\"(.*?)\"\,\"title\"\:\"(.*?)\"")
course_nodes = p.findall(c)
return course_nodes
def get_media(course_nodes, shib, src):
"""
To download the video, we need the sharekey. The sharekey is in
1. the url, if the video is embedded in the page
2. inside an input tag on the VCS page
"""
media = list()
for node in course_nodes:
iframeSrc = None
response = shib.session.get(node[0])
c = response.text
soup = BeautifulSoup(c, 'html.parser')
try:
"""
Some video URLs are not offered in a list, but on a page
that is included as an iframe.
"""
iframeSrc = soup.find('iframe').get('src')
response = shib.session.get(src[0:33]+iframeSrc)
c = response.text
soup = BeautifulSoup(c, 'html.parser')
except Exception:
"""
TODO: Implement error handling
"""
pass
try:
"""
iframe can have another iframe embedded that contains the embedded media.
iframe source contains the sharekey
"""
p = re.compile('embed\?key\=([A-Za-z0-9]+)')
for iframe in soup.find_all('iframe'):
embedded_media = iframe.get('src')
sharekey = p.findall(embedded_media)
response = shib.session.get(embedded_media)
c = response.text
soup = BeautifulSoup(c, 'html.parser')
title = soup.find('video').get('data-piwik-title')
media_dict = {'title': title,
'sharekey': sharekey[0],
'type': 'video'
}
media.append(media_dict)
except Exception:
"""
TODO: Implement error handling
"""
pass
try:
for link in soup.find_all("a", href=re.compile("videocampus")):
"""
If there is no embedded video, there are probably links
to the medium hosted on Videocampus Sachsen
"""
url = link.get("href")
response = shib.session.get(url)
c = response.text
soup = BeautifulSoup(c, 'html.parser')
sharekey = soup.find('input', {'name':'sharekey'}).get('value')
title = soup.find('video').get('data-piwik-title')
media_dict = {
"title": title,
'sharekey': sharekey,
"type": 'video',
"downloaded": False
}
media.append(media_dict)
except Exception:
"""
TODO: Implement error handling
"""
pass
return media
def opal_scraper(shib):
fn = 'content.json'
if not Path(fn).exists():
logging.error(fn + " was not found. Terminating program.")
sys.exit(1)
else:
try:
with open(fn, "r") as read_file:
data = json.load(read_file)
content = data.get("content")
except Exception as e:
logging.error("Got unhandled exception %s" % str(e))
sys.exit(1)
"""
TODO:
- Check if video is already in there
"""
for key in content:
d = content.get(key)
logging.info("Checking for content for " + key)
course_nodes = get_course_nodes(shib, d.get("target"))
media = get_media(course_nodes, shib, d.get("target"))
content[key]["media"] = media
data["content"].update(content)
try:
with open(fn, "w") as write_file:
json.dump(data, write_file, indent = 2)
except Exception as e:
logging.error("Got unhandled exception %s" % str(e))
sys.exit(1)
return 0
def download_file(url, path, shib):
with shib.session.get(url, stream=True) as r:
r.raise_for_status()
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return 0
def get_m3u8(key, sharekey, path, shib):
logging.info("%s: Download m3u8 for %s" % (key, sharekey))
m3u8_url = "https://videocampus.sachsen.de/media/hlsMedium/key/{sharekey}/format/auto/ext/mp4/learning/0/path/m3u8".format(sharekey=sharekey)
m3u8_path = path + "/{sharekey}.m3u8".format(sharekey=sharekey)
download_file(m3u8_url, m3u8_path, shib)
with open(m3u8_path) as file:
lines = [line.rstrip() for line in file]
m3u8mp4_url = (m3u8_url[0:-4]+lines[-1]).format(sharekey=sharekey)
m3u8mp4_path = path + "/{sharekey}_mp4.m3u8".format(sharekey=sharekey)
download_file(m3u8mp4_url, m3u8mp4_path, shib)
return 0
def get_ts(key, sharekey, path, shib):
url = "https://videocampus.sachsen.de/media/hlsMedium/key/{sharekey}/format/auto/ext/mp4/learning/0/path/".format(sharekey=sharekey)
m3u8mp4_path = path + "/{sharekey}_mp4.m3u8".format(sharekey=sharekey)
with open(m3u8mp4_path) as file:
lines = [line.rstrip() for line in file]
ts_keys = [ x for x in lines if "EXT" not in x]
logging.info("%s: Write files.txt for %s" % (key, sharekey))
with open(path + "/files.txt", "w") as text_file:
for ts in ts_keys:
print(f"file '{ts}' ", file=text_file)
for tkey in ts_keys:
logging.info("%s: Download %s" % (key, tkey))
durl = url+tkey
ts_path = path+"/"+tkey
download_file(durl, ts_path, shib)
return 0