-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetthelook.py
136 lines (109 loc) · 4.59 KB
/
getthelook.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
import imp
import os
import re
import sys
import urlparse
from datetime import datetime
from optparse import OptionParser
from selenium import webdriver
class Photog(object):
def __init__(self):
self.drivers = {
'firefox': webdriver.Firefox,
'chrome': webdriver.Chrome
}
self.active_drivers = []
def look(self, drivers, urls, options):
for driver in drivers:
if driver in self.drivers:
try:
browser = self.drivers.get(driver)()
self.active_drivers.append(browser)
except Exception, msg:
sys.stderr.write('%s: %s' % (Exception, msg))
continue
else:
sys.stderr.write('No driver for %s.' % driver)
continue
for url in urls:
url = url.strip()
site_name = urlparse.urlparse(url).netloc
if not options.green:
if not os.path.exists(os.path.join(options.screenshot_dir, site_name)):
os.mkdir(os.path.join(options.screenshot_dir, site_name))
browser.get(url)
# Check for further instructions.
try:
try:
mod = imp.find_module(site_name.replace('.', '_'), [options.extension_dir])
except ImportError:
mod = imp.find_module(site_name.lstrip('www.').replace('.', '_'), [options.extension_dir])
blueprint = imp.load_module('blueprint', *mod)
blueprint.check_site(url, browser)
except ImportError, msg:
#print sys.stderr.write('%s: %s\n' % (ImportError, msg))
pass
except AttributeError, msg:
sys.stderr.write('%s\n' % msg)
continue
if not options.green:
filename = '-'.join([datetime.strftime(datetime.now(), '%Y%m%d_%H%M%S'), self.clean_filename(url), driver])
filename = os.path.join(options.screenshot_dir, site_name, filename + '.png')
print 'saving %s' % filename
browser.get_screenshot_as_file(filename)
if options.interactive:
self.prompt()
browser.close()
self.active_drivers.remove(browser)
def clean_filename(self, filename):
filename = re.sub('^http(|s)://', '', filename)
return re.sub('[/-\?]', '.', filename)
def prompt(self):
while True:
print 'Continue? [Y/n] ',
cont = raw_input()
if cont.lower() == 'n':
continue
else:
return
def __del__(self):
for driver in self.active_drivers:
driver.close()
def main(browsers, urls, options):
look = Photog()
if isinstance(urls, str):
urls = [urls]
if getattr(options, 'file'):
try:
with open(options.file) as f:
# So we can get from both the cmd line arguments and the file.
urls += f.readlines()
except IOError:
sys.stderr.write('Bad source file')
return
if not urls:
sys.stderr.write('No URLs.')
return
look.look(browsers, urls, options)
if __name__ == '__main__':
browsers = []
def callback(option, opt_str, value, parser):
browsers.append(opt_str.strip('--'))
return
parser = OptionParser()
parser.add_option('--chrome', action='callback', callback=callback)
parser.add_option('--firefox', action='callback', callback=callback)
parser.add_option('-g', '--green', action='store_true', default=False, help='Do not take screenshots')
parser.add_option('-i', '--interactive', action='store_true', default=False, help='Control when you go to the next site')
parser.add_option('-d', '--extension_dir', dest='extension_dir', default='extensions', help='Look for directions in this DIRECTORY', metavar='DIRECTORY')
parser.add_option('-s', '--screenshot_dir', dest='screenshot_dir', default='screenshots', help='Save screenshots to DIRECTORY', metavar='DIRECTORY')
parser.add_option('--file', help='Read URLs from FILE', metavar='FILE')
# args = urls, or a file
(options, args) = parser.parse_args()
if not args and not options.file:
sys.stderr.write('Need URLs.\n')
sys.exit(2)
if not browsers:
sys.stderr.write('No browsers to test.\n')
sys.exit(2)
main(browsers, args, options)