-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.py
191 lines (127 loc) · 5.06 KB
/
build.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
from bs4 import BeautifulSoup
import yaml
import requests
import sqlite3
import timeit
class DocSet(object):
def __init__(self, name):
self.name = name
self.init_db()
self.entries = []
@property
def path(self):
return '{name}.docset'.format(name = self.name)
def init_db(self):
path = '{path}/Contents/Resources/docSet.dsidx'.format(path = self.path)
database = sqlite3.connect(path)
cursor = database.cursor()
try: cursor.execute('DROP TABLE searchIndex;')
except: pass
cursor.execute('CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);')
cursor.execute('CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);')
database.commit()
database.close()
def insert_entries(self):
path = '{path}/Contents/Resources/docSet.dsidx'.format(path = self.path)
database = sqlite3.connect(path)
cursor = database.cursor()
inserts = [(entry.name, entry.type_, entry.path) for entry in self.entries]
cursor.executemany('insert into searchIndex(name, type, path) values (?,?,?)', inserts)
database.commit()
database.close()
def create(self):
for entry in list(self.entries):
try:
entry.download()
print 'Downloaded {entry}'.format(entry = entry.name)
except Exception, e:
print 'Failed to download {entry}'.format(entry = entry.name)
print str(e)
self.entries.remove(entry)
for entry in self.entries: entry.rewrite(self.entries)
self.insert_entries()
class Entry(object):
def __init__(self, name, path, type_, url, docset):
self.name = name
self.type_ = type_
self.url = url
self.docset = docset
self.path = path
@property
def full_path(self):
return '{docset}/Contents/Resources/Documents/{path}'.format(
docset = self.docset.path,
path = self.path)
def download(self):
r = requests.get(self.url)
if r.status_code == 200:
with open(self.full_path, 'w') as f:
f.write(r.content)
else:
raise Exception('Received "{code}" when downloading "{name}"'.format(
code = r.status_code,
name = self.name))
def rewrite(self, entries):
source = open(self.full_path, 'r+')
soup = BeautifulSoup(source.read())
unnecessary = [
'#megabladeContainer',
'#ux-header',
'#isd_print',
'#isd_printABook',
'#expandCollapseAll',
'#leftNav',
'.feedbackContainer',
'#isd_printABook',
'.communityContentContainer',
'#ux-footer'
]
for u in unnecessary:
if u[0] == '#':
try:
soup.find(id=u[1:]).decompose()
except AttributeError:
pass
elif u[0] == '.':
for element in soup.find_all('div', class_=u[1:]):
element.decompose()
for link in soup.find_all('a'):
for entry in entries:
try:
if link.attrs['href'] == entry.url:
link.attrs['href'] = entry.path
except KeyError:
pass
source.seek(0)
source.write(str(soup))
source.truncate()
source.close()
print 'Finished rewriting {entry}'.format(entry = self.name)
if __name__ == '__main__':
start = timeit.default_timer()
indexes = yaml.load(open('indexes.yaml', 'r').read())['cmdlet']
docset = DocSet('PowerShell')
for index in indexes:
r = requests.get(index['url'])
if r.status_code == 200:
soup = BeautifulSoup(r.content)
group = index['name']
for div in soup.find_all('div'):
try:
if div['data-toclevel'] == '2':
link = div.a.attrs['href'].strip()
title = div.a.attrs['title']
docset.entries.append(
Entry(title, group+'-'+title+'.html', 'Command', link, docset))
except KeyError:
pass
print 'Finished indexing {index}'.format(index = index['name'])
else:
print 'Failed to index {index} ({code})'.format(
index = index['name'],
code = r.status_code)
docset.create()
stop = timeit.default_timer()
print 'Downloaded {count} entries in {elapsed} seconds.'.format(
count = len(docset.entries),
elapsed = (stop - start))