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.
4
5"""Command for scraping images from a URL or list of URLs.
6
7Prerequisites:
8  1. The command_line package from tools/site_compare
9  2. Either the IE BHO or Firefox extension (or both)
10
11Installation:
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
16
17Invoke with the command line arguments as documented within
18the command line.
19"""
20
21import command_line
22
23from drivers import windowing
24from utils import browser_iterate
25
26def 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)
33
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)
39
40
41def ExecuteScrape(command):
42  """Executes the Scrape command."""
43
44  def ScrapeResult(url, proc, wnd, result):
45    """Capture and save the scrape."""
46    if log_file: log_file.write(result)
47
48    # Scrape the page
49    image = windowing.ScrapeWindow(wnd)
50    filename = windowing.URLtoFilename(url, command["--outdir"], ".bmp")
51    image.save(filename)
52
53  if command["--logfile"]: log_file = open(command["--logfile"], "w")
54  else: log_file = None
55
56  browser_iterate.Iterate(command, ScrapeResult)
57
58  # Close the log file and return. We're done.
59  if log_file: log_file.close()
60