Exemplo n.º 1
0
 def get_media_requests(self, item, info):
     click.echo("Saving image {}".format(item.get("url")))
     url, page, title, alt_text = itemgetter("url", "page", "title",
                                             "alt_text")(item)
     image_path_directory = Comic.save_image_location(
         url, page, info.spider.directory, title)
     if os.path.isfile(image_path_directory) or self.image_in_zipfile(
             item, info.spider.directory):
         click.echo("The image was already downloaded. Skipping...")
         raise DropItem("The image was already downloaded. Skipping...")
     if alt_text is not None:
         with open(
                 Comic.save_alt_text_location(page, info.spider.directory),
                 "w") as alt_text_file:
             alt_text_file.write(alt_text)
     yield scrapy.Request(
         item.get("url"),
         meta={
             "image_file_name":
             Comic.save_image_filename(item.get("url"), item.get("page"),
                                       title, info.spider.directory)
         },
     )
Exemplo n.º 2
0
def test_save_image_filename_with_title_present():
    assert (Comic.save_image_filename(
        "http://imgs.xkcd.com/comics/barrel_cropped_(1).jpg", 1, True,
        "foo") == "foo-1.jpg")