-
Notifications
You must be signed in to change notification settings - Fork 2
/
Downloader.py
108 lines (88 loc) · 2.95 KB
/
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
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
import requests
import re
from bs4 import BeautifulSoup
import json
def get_sesh_id(session):
soup = BeautifulSoup(session.get('https://moodle31.upei.ca/login/index.php').text,"html.parser")
return soup.find('input', attrs={'name':'logintoken'})['value']
def login():
session = requests.Session()
payload = {
'logintoken': get_sesh_id(session),
'username': config["username"],
'password': config["password"]
}
session.post('https://moodle31.upei.ca/login/index.php', data=payload)
return session
def get_moodle_doc(url):
doc= session.get(url)
disp = doc.headers['Content-disposition']
doc_name = re.findall("filename.+", disp)[0].split("\"")[1]
return {
'doc': doc.content,
'name': doc_name
}
def write_doc(doc, name):
file = open(name, 'wb')
file.write(doc)
file.close
def print_downloaded():
print('Downloaded:')
print('===========')
for name in config["downloaded"]:
print(name)
print('================================================================================')
def download_stnd_moodle():
website = session.get("https://moodle31.upei.ca/course/view.php?id="
+ str(config["course_id"]))
html = website.text
soup = BeautifulSoup(html, "html.parser")
links = soup.findAll(class_='activityinstance')
print_downloaded()
print('Downloading:')
print('===========')
for link in links:
name = link.find(class_ = "instancename").text
if config["match"] not in name.lower() or name in config["downloaded"]:
continue
print(name)
url = link.a["href"]
doc = get_moodle_doc(url)
write_doc(doc['doc'], doc['name'])
config["downloaded"].append(name)
print('\t', doc['name'])
def download_cezar():
semester = config["semester"].split('-')
year = semester[0]
season = semester[1]
course = config["course"]
website = requests.get(
'http://www.smcs.upei.ca/~ccampeanu/Teach/'
+ season
+ '/'
+ year
+ '/'
+ course
+ '/LN/'
)
html = website.text
links = re.findall('"(http://.*4.pdf?)"', html)
print('Downloading:')
print('===========')
for link in links:
name = link[(link.rfind('/')+1 ):]
print(name)
doc = session.get(link).content
write_doc(doc, name)
if __name__== "__main__":
config = json.loads(open('config.json', 'r').read())
session = login()
if config["course_type"] == "stnd_moodle": download_stnd_moodle()
elif config["course_type"] == "cezar": download_cezar()
config_file = open('config.json', 'w')
pretty_json = json.dumps(config,
indent=4,
separators=(',', ': ')
)
config_file.write(pretty_json)
config_file.close()