-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmanning.py
executable file
·142 lines (129 loc) · 5.18 KB
/
manning.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
#!/bin/python3
"""
Date: Tue Sep 15 12:30:33 UTC 2020
Author: Lucian Maly
License: MIT
"""
import requests
from bs4 import BeautifulSoup
import datetime
import os
import errno
import sys
import getopt
loginURL = 'https://login.manning.com/login?service=https://www.manning.com/login/cas'
dashboardURL = 'https://www.manning.com/dashboard/index?filter=book&max=999&order=lastUpdated&sort=desc'
def main(argv):
global username
global password
username = ''
password = ''
try:
opts, args = getopt.getopt(argv, "hu:p:", ["username=", "password="])
except getopt.GetoptError:
print('Usage: `manning.py -u <email> -p <password>`')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Usage: `manning.py -u <email> -p <password>`')
sys.exit()
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
else:
print('Help: `manning.py -h`')
sys.exit()
def create_folder():
global folder
datetime_object = datetime.date.today()
folder = 'Manning_' + str(datetime_object)
try:
os.mkdir(folder)
print('Created folder', folder)
except OSError as e:
if e.errno == errno.EEXIST:
print(f'Directory {folder} already exists.')
else:
raise
def get_list():
with requests.Session() as s:
# PURPOSE: Get the value of LT
soup0 = BeautifulSoup(s.get(loginURL).text, 'html.parser')
headers = {
'Origin': 'https://login.manning.com',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
}
data = {
'username': username,
'password': password,
'lt': soup0.find('input', {'name': 'lt'}).get('value'),
'execution': 'e1s1',
'_eventId': 'submit',
'submit': ''
}
# PURPOSE: Log in
s.post(loginURL, cookies=s.cookies, headers=headers, data=data)
dashboard = s.get(dashboardURL)
# PURPOSE: Parse the dashboard with up to 999 products
soup = BeautifulSoup(dashboard.text, 'html.parser')
div_container = soup.find('table', {'id': 'productTable'})
for product in div_container.find_all('tr', {'class': 'license-row'}):
try:
# EXAMPLE: Terraform in Action
title = str(product.find(
'div', {'class': 'product-title'}).text.strip())
# EXAMPLE: winkler
author = str(product.find(
'form', {'class': 'download-form'})['name']).split('-')[1]
restrictedDownloadIds = author + '-restrictedDownloadIds'
download_payload = [
('dropbox', 'false'),
('productExternalId', author)
]
# PURPOSE: Find all the restrictedDownloadIds and create a complete payload
for downloadSelection in product.find_all('div', {'class': 'download-selection'}):
hidden = downloadSelection.find_all(
'input', {'type': 'hidden'})
for val in hidden:
checkbox1 = (val['id'], val['value'])
checkbox2 = (restrictedDownloadIds, val['id'])
download_payload.append(checkbox1)
download_payload.append(checkbox2)
# EXAMPLE: [('dropbox', 'false'), ('productExternalId', 'winkler'), ('1971', '7850702'), ('winkler-restrictedDownloadIds', '1971'), ('1972', '7850703'), ('winkler-restrictedDownloadIds', '1972'), ('1973', '7850704'), ('winkler-restrictedDownloadIds', '1973')]
try:
subfolder = str(title.replace(' ', '_'))
path = os.path.join(folder, subfolder)
os.makedirs(path)
print('Created folder', path)
except OSError as e:
if e.errno == errno.EEXIST:
print(f'Directory {path} already exists.')
else:
raise
downloadURL = 'https://www.manning.com/dashboard/download?id=downloadForm-' + author
print('Downloading', title, '...')
dl = s.post(downloadURL, cookies=s.cookies,
headers=headers, data=download_payload)
# PURPOSE: Some free titles are only in PDF format, this can be determined from the amount of hidden inputs
if len(download_payload) <= 4:
extension = '.pdf'
else:
extension = '.zip'
filename = path + '/' + subfolder + extension
file = open(filename, "wb")
file.write(dl.content)
file.close()
except TypeError:
pass
def end_script():
print('Exiting the script...')
sys.exit()
if len(sys.argv) > 1:
main(sys.argv[1:])
create_folder()
get_list()
end_script()
else:
print('Help: `manning.py -h`')
sys.exit()