-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathisourl.py
executable file
·64 lines (52 loc) · 1.71 KB
/
isourl.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script for obtaining BlackArch iso url and sha1sum
"""
import os
import sys
import datetime
from urllib.request import urlopen
from urllib.error import URLError
try:
from lxml import html as html
except ModuleNotFoundError as import_error:
print("Can't find python3 module lxml: {}".format(import_error))
sys.exit(1)
DOWNLOAD_PAGE = 'https://blackarch.org/downloads.html'
class ExitException(Exception):
pass
def obtain_iso_url():
"""
Parse information from official site
"""
try:
response = urlopen(DOWNLOAD_PAGE, timeout=5)
content = response.read()
except URLError as e:
raise ExitException(str(e) + ' ' + DOWNLOAD_PAGE)
tree = html.fromstring(content.decode())
try:
subtree = tree.xpath('//table[@class="download"]/tr[contains(td[1], "BlackArch Linux 64 bit Netinstall ISO")]').pop()
sha1sum = subtree.xpath('td[5]//text()').pop()
iso_url = subtree.xpath('td[1]/a/@href').pop()
except IndexError:
raise ExitException("Can't parse content on '{}' for obtaining iso url".format(DOWNLOAD_PAGE))
return iso_url, sha1sum
def gen_vars():
iso_url, sha1sum = obtain_iso_url()
return {
'iso_url': iso_url,
'iso_checksum': sha1sum,
'created_at': datetime.date.today().strftime('%Y%m%d'),
'headless': 'true'}
if __name__ == '__main__':
try:
iso_url, sha1sum = obtain_iso_url()
sys.stdout.write(iso_url + ' ' + sha1sum)
except ExitException as e:
sys.stderr.write(str(e) + os.linesep)
sys.exit(1)
except KeyboardInterrupt:
sys.stderr.write('generating variables aborted')
sys.exit(1)