-
Notifications
You must be signed in to change notification settings - Fork 3
/
slax-module-mirror.py
183 lines (145 loc) · 5.19 KB
/
slax-module-mirror.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
import errno
import logging
import os
import re
import threading
from urllib2 import urlopen, urlparse, Request
from bs4 import BeautifulSoup as bs
LOGGER_NAME = 'slax-module-mirror'
ARCH_X86 = 'x86'
ARCH_X86_64 = 'x86_64'
SLAX_MODULES_URL = 'https://www.slax.org/en/modules.php'
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
class FlowException(Exception):
pass
class ModuleEntry(threading.Thread):
def __init__(self, parent, tr, primary_url):
super().__init__()
self.logger = logging.getLogger(LOGGER_NAME)
self.tr = tr
self.primary_url = primary_url
self.parent = parent
def get_abspath(self, relpath):
return self.primary_url + relpath
def run(self):
self._nodes = self.tr.find_all('td')
self.logger.debug('tds: %s', tds)
if not self.nodes:
raise FlowException()
if not self.parent.parent.make_duplicate_placeholder(self):
self.download_all()
@property
def name(self):
return self.nodes[0].a.get_text()
@property
def x86(self):
return self.primary_url + tds[2].div.a.get('href')
@property
def x86_64(self):
return self.primary_url + self.nodes[2].div.a.get('href')
@property
def desc(self):
return self.nodes[3].get_text()
@property
def req(self):
return tds[4].get_text()
@property
def size(self):
return self.nodes[5].get_text()
@property
def path(self):
return os.path.join(self.parent.name, self.name)
def download_all(self):
self.download(self.x86, arch=ARCH_X86)
self.download(self.x86_64, arch=ARCH_X86_64)
def download(self, url, arch):
self.logger.info('downloading: %s', url)
if not url:
raise FlowException()
u = urlopen(url)
if u.getcode() != 200:
raise FlowException()
arch_path = os.path.join(self.path, arch)
mkdir_p(arch_path)
with open(os.path.join(arch_path, url.split('/')[-1]), 'w') as f:
f.write(u.read())
def __dict__(self):
return {'name':self.name, 'x86':self.x86, 'x86_64':self.x86_64, 'desc':self.desc, 'req':self.req, 'size':self.size}
def __str__(self):
return str(self.__dict__)
class CategoryEntry:
def __init__(self, parent, div, primary_url):
self.logger = logging.getLogger(LOGGER_NAME)
self.logger.debug('%s; %s', primary_url, div)
self.parent = parent
self.mods = []
cat_a = div.find('a', href=re.compile('\?category=.*'))
self.logger.debug('category.cat_a: %s', cat_a)
self.name = cat_a.get_text()
self.url = cat_a.get('href')
self.download_modules()
def download_modules(self):
mkdir_p(self.name)
primary_urlobj = urlparse.urlsplit(primary_url)
full_category_url = primary_url + self.url
cat_soup = bs(urlopen(full_category_url))
for row in cat_soup.find('table').find_all('tr'):
try:
mod = ModuleEntry(self, row, '%s://%s' % (primary_urlobj.scheme, primary_urlobj.netloc))
mod.start()
self.logger.debug('category.mod: %s', mod)
self.mods.append(mod)
except FlowException:
pass
def __dict__(self):
return {'name':self.name, 'url':self.url, 'mods':[str(x) for x in self.mods]}
def __str__(self):
return str(self.__dict__)
class Page:
def __init__(self):
self.logger = logging.getLogger(LOGGER_NAME)
self.primary_url = SLAX_MODULES_URL
self.categories = []
self.mods = {}
self.download_categories()
def download_categories(self):
soup = bs(urlopen(self.primary_url))
for cat_div in soup.find_all('div',class_=re.compile('category')):
self.logger.debug('page.div: %s', cat_div)
if cat_div:
try:
cat = CategoryEntry(self, cat_div, self.primary_url)
self.mods.update(dict([(x.name, cat.name) for x in cat.mods if x.name]))
logger.debug('page.cat: %s', cat)
self.categories.append(cat)
except FlowException:
pass
def has(self, mod_name):
if mod_name in self.mods:
return True
return False
def make_duplicate_placeholder(self, mod):
if not self.has(mod.name): return False
mkdir_p(mod.path)
def touch_mod(mod, arch, url):
mkdir_p(os.path.join(mod.path, arch))
f = open(os.path.join(mod.path, arch, url.split('/')[-1]), 'w')
f.write('see: %s' % os.path.join(self.mods[mod.name], arch, url.split('/')[-1]))
f.close()
touch_mod(mod, ARCH_X86, mod.x86)
touch_mod(mod, ARCH_X86_64, mod.x86_64)
return True
def __str__(self):
return str([str(x) for x in self.categories])
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(LOGGER_NAME)
p = Page()
print(p)