5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

社畜ちゃんの漫画をまとめて入手するRubyスクリプト

5
Last updated at Posted at 2017-06-27

社畜ちゃんとは、とっても可愛い女の子たちが、がんばって仕事に励む姿を描いた漫画作品です。後輩ちゃんの可愛さは、尋常じゃないです(ペカー)!
160711Udemy_comic_02.jpg
漫画は、Webで公開されています↓。是非、読んで見ましょう。
https://syachiku-chan.com/?page_id=228
「とても面白い!漫画のファイルを手元に置いておきたい!でも、量が多い...」
という人のために、ファイルをまとめて保存する(簡単な)スクリプトを作成しました。

syachiku_chan.rb
# !/usr/bin/env ruby

require 'open-uri'
require 'nokogiri'

class Syachiku_chan_downloader

  def initialize
    @doc
  end

  def download_html
    url = 'https://syachiku-chan.com/?page_id=228'

    charset = nil
    html = open(url) do |f|
      charset = f.charset
      f.read
    end
    @doc = Nokogiri::HTML.parse(html, nil, charset)
  end

  def dl_main_story
    gallaly = @doc.css('div#foogallery-gallery-1662')
    max_episode_num = 0 #ダウンロードしている最新エピソードの番号
    latest_episode_num = 0 #今回の入手した最新のエピソード
    gallaly.css('a').each do |each|
      title = each.css('img').attribute('alt').to_s #タイトルの取得

      unless title.empty?
        episode_num = title.match(/\d+/)[0].to_i #話数の数値だけを取得
      else
        # imgタグにalt属性が存在しなかったときの処理
        title = each[:'data-caption-desc']
        episode_num = title.match(/\d+/)[0].to_i unless title.nil? #話数の数値だけを取得
      end

      if title.nil?
        #タイトル情報が存在しないとき
        title = "タイトル情報なし" # タイトル情報の取得は諦める
        episode_num = each[:'data-caption-title'].match(/\d+/)[0].to_i
      end

      next if episode_num <= max_episode_num
      url = each[:href]
      dl_file_name = format("%04d",episode_num)
      `wget -nc -O #{dl_file_name}.jpg #{url}`
      sleep(1)
      latest_episode_num = episode_num if episode_num > latest_episode_num
    end

  end

  def dl_sub_story
    gallaly = @doc.css('div#gallery-2')
    gallaly.css('a').each do |each|
      url = each[:href]
      `wget -nc #{url}`
      sleep(1)
    end
    gallaly = @doc.css('div#gallery-3')
    gallaly.css('a').each do |each|
      url = each[:href]
      `wget -nc #{url}`
      sleep(1)
    end
  end

  def do_download
    download_html
    dl_main_story
    dl_sub_story
  end
end

downloader = Syachiku_chan_downloader.new
downloader.do_download

スクリプトを動かすには、nokogiri(Rubyのgem)とwgetが必要になります。gemとかaptで入れましょう。
単行本も出版されているので、紙媒体で欲しい人は購入してみましょう(Webにはない漫画も収録されています)。
https://www.amazon.co.jp/gp/product/4048659073/ref=as_li_tf_il?ie=UTF8&camp=247&creative=1211&creativeASIN=4048659073&linkCode=as2&tag=vitaone0e-22

5
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?