Last active
December 28, 2015 04:59
-
-
Save mbikovitsky/7446403 to your computer and use it in GitHub Desktop.
Python 3 script for downloading the featured image from the homepage of Kanobu.
After running the script the current directory will contain the individual panels and the merged image. Dependencies:
- Beautiful Soup 4 (https://pypi.python.org/pypi/beautifulsoup4/)
- Pillow (https://pypi.python.org/pypi/Pillow)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2013 Michael Bikovitsky | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
import urllib.request as request # For downloading html and images | |
from bs4 import BeautifulSoup # For parsing HTML | |
from posixpath import basename # For saving the downloaded images | |
# For setting the modification time | |
import os | |
from datetime import datetime, timezone | |
import calendar | |
# For image manipulation | |
from PIL import Image | |
import io | |
import sys # For exiting in case of errors | |
# Helper for dealing with months | |
months = dict((v, k) for k, v in enumerate(calendar.month_abbr)) | |
# Downloading the web-page | |
response = request.urlopen("http://kanobu.ru") | |
raw_page = response.read() | |
soup = BeautifulSoup(raw_page) | |
container = soup.find(class_="page-feature-top") # Finding the feature div | |
try: | |
# Getting all the layers | |
layers = container.find_all(class_="article-block--img") | |
except AttributeError: | |
print("page-feature-top not found on page. Website is probably " | |
"undergoing maintenance.\n") | |
sys.exit(1) | |
# Getting image data | |
images = [layer["src"] for layer in layers] | |
# Downloading images | |
for i in range(len(images)): | |
image = images[i] | |
image_response = request.urlopen(image) | |
time_header = image_response.getheader('Last-Modified').split() | |
date = datetime(int(time_header[3]), months[time_header[2]], | |
int(time_header[1]), | |
*[int(part) for part in time_header[4].split(':')], | |
tzinfo=timezone.utc) | |
image_data = image_response.read() | |
with open(basename(image), mode='wb') as f: | |
f.write(image_data) | |
os.utime(basename(image), (date.timestamp(), date.timestamp())) | |
# Storing downloaded data instead of url | |
images[i] = image_data | |
result = Image.new("RGBA", (0, 0)) | |
for img in images: | |
to_copy = Image.open(io.BytesIO(img)) | |
to_copy = to_copy.convert("RGBA") | |
# Resizing if necessary | |
if result.size[0] < to_copy.size[0]: | |
temp = Image.new("RGBA", (to_copy.size[0], result.size[1])) | |
temp.paste(result, (0, 0)) | |
result = temp | |
if result.size[1] < to_copy.size[1]: | |
temp = Image.new("RGBA", (result.size[0], to_copy.size[1])) | |
temp.paste(result, (0, 0)) | |
result = temp | |
result.paste(to_copy, (0, 0), to_copy) | |
result.save("Full.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment