Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / site_compare / commands / scrape.py
blob8fee5a3b3ba4fec2d4cac974002d58f4acec2b6e
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Command for scraping images from a URL or list of URLs.
7 Prerequisites:
8 1. The command_line package from tools/site_compare
9 2. Either the IE BHO or Firefox extension (or both)
11 Installation:
12 1. Build the IE BHO, or call regsvr32 on a prebuilt binary
13 2. Add a file called "measurepageloadtimeextension@google.com" to
14 the default Firefox profile directory under extensions, containing
15 the path to the Firefox extension root
17 Invoke with the command line arguments as documented within
18 the command line.
19 """
21 import command_line
23 from drivers import windowing
24 from utils import browser_iterate
26 def CreateCommand(cmdline):
27 """Inserts the command and arguments into a command line for parsing."""
28 cmd = cmdline.AddCommand(
29 ["scrape"],
30 "Scrapes an image from a URL or series of URLs.",
31 None,
32 ExecuteScrape)
34 browser_iterate.SetupIterationCommandLine(cmd)
35 cmd.AddArgument(
36 ["-log", "--logfile"], "File to write text output", type="string")
37 cmd.AddArgument(
38 ["-out", "--outdir"], "Directory to store scrapes", type="string", required=True)
41 def ExecuteScrape(command):
42 """Executes the Scrape command."""
44 def ScrapeResult(url, proc, wnd, result):
45 """Capture and save the scrape."""
46 if log_file: log_file.write(result)
48 # Scrape the page
49 image = windowing.ScrapeWindow(wnd)
50 filename = windowing.URLtoFilename(url, command["--outdir"], ".bmp")
51 image.save(filename)
53 if command["--logfile"]: log_file = open(command["--logfile"], "w")
54 else: log_file = None
56 browser_iterate.Iterate(command, ScrapeResult)
58 # Close the log file and return. We're done.
59 if log_file: log_file.close()