Skip to content

Instantly share code, notes, and snippets.

@m-atthieu
Created December 29, 2011 19:54
Show Gist options
  • Select an option

  • Save m-atthieu/1535912 to your computer and use it in GitHub Desktop.

Select an option

Save m-atthieu/1535912 to your computer and use it in GitHub Desktop.
script for manga download from mangafox.com
#!/usr/bin/env ruby
#
# Mangafox Downloader
# By Leonardo Prado
# http://twitter.com/leonardodna
# http://lprado.com.br
#
# Based on AkitaOnRails gist for onemanga.com: https://gist.github.com/285032
#
# USAGE:
# mangafox.rb [manga name] [chapter number]
#
# manga name: Must be as it appears on the mangafox url. Examples:
# - bleach
# - hunter_x_hunter
# - mahou_sensei_negima
# - shinryaku_ika_musume
#
# chapter number: If passed, the script will download from the passed chapter up to
# the last chapter on the site.
#
# You will find the downloaded chapters under $HOME/Documents/Mangas/
# NOTE: If you use windows, you will probably need to fix the place where you save the files. Check
# Example on lines 36-38
#
# CHANGELOG
# 0.3 - Updated the script to prevent an infinite loop when downloading a completed series, that
# doesn't have a "coming soon" link to fetch. Also, changed the chapter number analisys, to
# allow decimal chapters to be saved correctly (like chapters 315, 315.1, 315.2... on Bleach)
# 0.2 - Altered Nokogiri attr method to get_attribute, so user with older versions don't get an
# error. Added a treatment to don't use the volume info on folders and urls, because it's an
# optional information on mangafox. Thanks for Karlisson Bezerra (@nerdson) for finding this
# bugs! :)
# 0.1 - Initial release
#
require 'rubygems'
require 'mechanize'
require 'nokogiri'
require 'open-uri'
# Example for windows users: (don't forget to create the Mangas directory!)
# home = 'C:/Documents and Settings/user_name/Meus documentos'
# manga_download_folder = File.join(home, "/Mangas/")
manga_download_folder = File.join(ENV['HOME'],"/Documents/Mangas/")
manga_root = "http://www.mangafox.com/manga/"
agent = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }
puts "Starting manga downloader"
if ARGV.size == 0
puts "In future versions, running the script without parameters will check your manga folder and get new chapters"
exit 0 # stop script
end
manga_name = ARGV.first || "bakuman"
start_from_chapter = ARGV.size > 1 ? ARGV[1].rjust(3, '0') : nil
puts "Downloading #{manga_name}"
manga_folder = File.join(manga_download_folder, manga_name)
FileUtils.mkdir_p(manga_folder)
puts "Creating #{manga_folder}"
# Access manga listing page
agent.get(manga_root + manga_name + "/?no_warning=1")
# Find the first or the selected chapter
chapter_link = agent.page.links.select do |l|
if start_from_chapter
l.href =~ /manga\/#{manga_name}\/?[^\/]*\/c#{start_from_chapter}/
else
l.href =~ /manga\/#{manga_name}/
end
end.reverse
while true do
# Access the chapter
agent.click chapter_link.first
# Get the next link
chapter_link = agent.page.links.select do |l|
l.href =~ /manga\/#{manga_name}/
end.drop(1)
# Check when to stop following links
case chapter_link.size
# First chapter or last chapter of a completed manga
# If first, change start_from_chapter to 2, so the next time this happens, it will be the last chapter
when 1
if start_from_chapter.nil?
start_from_chapter = 2
else
exit 0
end
# Last chapter of an incompleted manga
when 0
exit 0
end
# get volume and chapter to create the chapter folder
chapter_number = agent.page.uri.to_s.match('c[\.0-9]+').to_s.gsub!("c", "chapter ")
chapter_folder = File.join(manga_download_folder, manga_name, chapter_number)
FileUtils.mkdir_p(chapter_folder)
puts "Creating #{chapter_folder}"
# Mangafox uses a lot of javascript to navigate through pages.
# This code get que selectbox with the page list and access all pages
# of the respective chapter.
chapter_url = agent.page.uri.to_s
pages = agent.page.search("select.middle").first.search('option')
pages.each do |opt|
chapter_url.gsub!( /[0-9]*\.html/, opt['value'] + ".html" )
agent.get chapter_url
img_uri = agent.page.search("//img[@id='image']").first["src"]
image_file = File.join(chapter_folder, img_uri.split("/").last)
open(image_file, 'wb') do |file|
puts " Downloading image #{opt['value']}"
file.write(open(img_uri).read)
end
end
end
@mymilad
Copy link

mymilad commented Dec 12, 2018

Skip to content

Search…
All gists
GitHub
New gist
@mymilad
0
0@m-atthieum-atthieu/mangafox.rb
Created 7 years ago •

<script src="https://gist.github.com/m-atthieu/1535912.js"></script>

Code Revisions 4
script for manga download from mangafox.com
mangafox.rb
#!/usr/bin/env ruby

Mangafox Downloader

By Leonardo Prado

http://twitter.com/leonardodna

http://lprado.com.br

Based on AkitaOnRails gist for onemanga.com: https://gist.github.com/285032

USAGE:

mangafox.rb [manga name] [chapter number]

manga name: Must be as it appears on the mangafox url. Examples:

- bleach

- hunter_x_hunter

- mahou_sensei_negima

- shinryaku_ika_musume

chapter number: If passed, the script will download from the passed chapter up to

the last chapter on the site.

You will find the downloaded chapters under $HOME/Documents/Mangas/

NOTE: If you use windows, you will probably need to fix the place where you save the files. Check

Example on lines 36-38

CHANGELOG

0.3 - Updated the script to prevent an infinite loop when downloading a completed series, that

doesn't have a "coming soon" link to fetch. Also, changed the chapter number analisys, to

allow decimal chapters to be saved correctly (like chapters 315, 315.1, 315.2... on Bleach)

0.2 - Altered Nokogiri attr method to get_attribute, so user with older versions don't get an

error. Added a treatment to don't use the volume info on folders and urls, because it's an

optional information on mangafox. Thanks for Karlisson Bezerra (@Nerdson) for finding this

bugs! :)

0.1 - Initial release

require 'rubygems'
require 'mechanize'
require 'nokogiri'
require 'open-uri'

Example for windows users: (don't forget to create the Mangas directory!)

home = 'C:/Documents and Settings/user_name/Meus documentos'

manga_download_folder = File.join(home, "/Mangas/")

manga_download_folder = File.join(ENV['HOME'],"/Documents/Mangas/")
manga_root = "http://www.mangafox.com/manga/"

agent = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }

puts "Starting manga downloader"

if ARGV.size == 0
puts "In future versions, running the script without parameters will check your manga folder and get new chapters"
exit 0 # stop script
end

manga_name = ARGV.first || "bakuman"
start_from_chapter = ARGV.size > 1 ? ARGV[1].rjust(3, '0') : nil
puts "Downloading #{manga_name}"

manga_folder = File.join(manga_download_folder, manga_name)
FileUtils.mkdir_p(manga_folder)
puts "Creating #{manga_folder}"

Access manga listing page

agent.get(manga_root + manga_name + "/?no_warning=1")

Find the first or the selected chapter

chapter_link = agent.page.links.select do |l|
if start_from_chapter
l.href =~ /manga/#{manga_name}/?[^\/]*/c#{start_from_chapter}/
else
l.href =~ /manga/#{manga_name}/
end
end.reverse

while true do

Access the chapter

agent.click chapter_link.first

Get the next link

chapter_link = agent.page.links.select do |l|
l.href =~ /manga/#{manga_name}/
end.drop(1)

Check when to stop following links

case chapter_link.size
# First chapter or last chapter of a completed manga
# If first, change start_from_chapter to 2, so the next time this happens, it will be the last chapter
when 1
if start_from_chapter.nil?
start_from_chapter = 2
else
exit 0
end
# Last chapter of an incompleted manga
when 0
exit 0
end

get volume and chapter to create the chapter folder

chapter_number = agent.page.uri.to_s.match('c[.0-9]+').to_s.gsub!("c", "chapter ")
chapter_folder = File.join(manga_download_folder, manga_name, chapter_number)
FileUtils.mkdir_p(chapter_folder)
puts "Creating #{chapter_folder}"

Mangafox uses a lot of javascript to navigate through pages.

This code get que selectbox with the page list and access all pages

of the respective chapter.

chapter_url = agent.page.uri.to_s
pages = agent.page.search("select.middle").first.search('option')
pages.each do |opt|

chapter_url.gsub!( /[0-9]*\.html/, opt['value'] + ".html" )
agent.get chapter_url

img_uri = agent.page.search("//img[@id='image']").first["src"]
image_file = File.join(chapter_folder, img_uri.split("/").last)
open(image_file, 'wb') do |file|
  puts "  Downloading image #{opt['value']}"
  file.write(open(img_uri).read)
end

end

end
@mymilad

Leave a comment
Attach files by dragging & dropping, selecting them, or pasting from the clipboard.

Styling with Markdown is supported
© 2018 GitHub, Inc.
Terms
Privacy
Security
Status
Help
Contact GitHub
Pricing
API
Training
Blog
About
Press h to open a hovercard with more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment