Last active
August 29, 2015 14:04
-
-
Save roddds/c838af0e3ebdedab9c47 to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
import os | |
import urllib | |
from BeautifulSoup import BeautifulSoup as bs | |
def get_soup(link): | |
html = urllib.urlopen(link).read() | |
return bs(html) | |
def get_image_link(soup): | |
return soup.find("section", id="viewer").find('img').get('src') | |
def get_number_of_pages(soup): | |
return len(soup.find('select', attrs={'class': 'wid60'}).findAll('option')) | |
def make_image_links(image_link, number_of_pages): | |
current_page_number = re.findall(r"\d\d\d\.jpg", image_link) | |
if len(current_page_number) != 1: | |
raise ValueError("Invalid image link") | |
before, after = image_link.split(current_page_number[0]) | |
links = [] | |
for i in range(1, number_of_pages+1): | |
links.append(before + str(i).zfill(3) + '.jpg' + after) | |
return links | |
def make_download_path(directory_name, image_link): | |
file_name = re.findall(r"\d\d\d\.jpg", image_link)[0] | |
print "Downloading page %s" % file_name | |
return os.path.join(".", directory_name, file_name) | |
def download_image(image_link, path): | |
urllib.urlretrieve(image_link, path) | |
if __name__ == '__main__': | |
link = raw_input("Entre com o link: ") | |
soup = get_soup(link) | |
manga_title = soup.title.text.split(' - ')[0] | |
print '\nDownloading manga "%s"' % manga_title | |
number_of_pages = get_number_of_pages(soup) | |
print '%s has a total of %d pages' % (manga_title, number_of_pages) | |
image_link = get_image_link(soup) | |
list_of_links = make_image_links(image_link, number_of_pages) | |
os.mkdir(manga_title) | |
for link in list_of_links: | |
path = make_download_path(manga_title, link) | |
download_image(link, path) | |
print "Finished downloading %s" % manga_title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment