forked from jaraco/cssutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsscapture.py
More file actions
97 lines (83 loc) · 2.4 KB
/
csscapture.py
File metadata and controls
97 lines (83 loc) · 2.4 KB
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
"""Retrieve all CSS stylesheets including embedded for a given URL.
Retrieve as StyleSheetList or save to disk - raw, parsed or minified version.
TODO:
- maybe use DOM 3 load/save?
- logger class which handles all cases when no log is given...
- saveto: why does urllib2 hang?
"""
__all__ = ['CSSCapture']
import logging
import optparse
import sys
from cssutils.script import CSSCapture
def main(args=None):
usage = "usage: %prog [options] URL"
parser = optparse.OptionParser(usage=usage)
parser.add_option(
'-d',
'--debug',
action='store_true',
dest='debug',
help='show debug messages during capturing',
)
parser.add_option(
'-m',
'--minified',
action='store_true',
dest='minified',
help='saves minified version of captured files',
)
parser.add_option(
'-n',
'--notsave',
action='store_true',
dest='notsave',
help='if given files are NOT saved, only log is written',
)
# parser.add_option('-r', '--saveraw', action='store_true', dest='saveraw',
# help='if given saves raw css otherwise cssutils\' parsed files')
parser.add_option(
'-s',
'--saveto',
action='store',
dest='saveto',
help='saving retrieved files to "saveto", defaults to "_CSSCapture_SAVED"',
)
parser.add_option(
'-u',
'--useragent',
action='store',
dest='ua',
help='useragent to use for request of URL, default is urllib2s default',
)
options, url = parser.parse_args()
# TODO:
options.saveraw = False
if not url:
parser.error('no URL given')
else:
url = url[0]
if options.debug:
level = logging.DEBUG
else:
level = logging.INFO
# START
c = CSSCapture(ua=options.ua, defaultloglevel=level)
stylesheetlist = c.capture(url)
if options.notsave is None or not options.notsave:
if options.saveto:
saveto = options.saveto
else:
saveto = '_CSSCapture_SAVED'
c.saveto(saveto, saveraw=options.saveraw, minified=options.minified)
else:
for i, s in enumerate(stylesheetlist):
print(
'''%s.
encoding: %r
title: %r
href: %r'''
% (i + 1, s.encoding, s.title, s.href)
)
if __name__ == "__main__":
sys.exit(main())