LoginSignup
9
11

More than 5 years have passed since last update.

[JAVA] 小説ダウンローダー

Last updated at Posted at 2015-05-11

そんな高尚なものでは無いのだが・・・

小説ダウンローダー

何これ?

小説家になろうで公開されている小説の本文のみを一つのテキストファイルにまとめてダウンロードするもの。(18禁の方も対応)

使い方

java NovelDownloader [Nコード] ([保存先])
保存先はオプションです。
保存先を指定しなければ、classファイルと同じ階層に保存されます。

補足

コマンドでjava -versionを打って何も出てこなければJava SEを導入してください。
作者の方は基本的にバックアップをとっているかと思いますが、何らかの理由で消えた時なんかに重宝するのではないかと思いつくりました。

ソース

NovelDownloader.java
/*
/  NovelDownloader v1.0
/  by @okkn
/  (c) 2015 okkn
/  
/  Java SE JDK 1.8.0_31
/  
*/

import java.io.IOException;
import java.io.File;
import java.io.FileWriter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class NovelDownloader {

    private final static String br = System.getProperty("line.separator");
    private static String ncode = "";

    public static void main(String args[]) throws IOException {

        String path = "";

        if (args.length == 1) {
            ncode = args[0];
            path  = System.getProperty("user.dir");
        } else if (args.length == 2) {
            ncode = args[0];
            path  = args[1];
        } else {
            System.out.println("java NovelDownloader [Ncode] ([保存パス])");
            System.exit(0);
        }

        if (!(ncode.length() == 7)) {
            System.out.println("Nコードの値が不正です");
            System.exit(-1);
        }

        File dir = new File(path);

        if (!(dir.exists())) {
            System.out.println("指定されたディレクトリが見つかりません");
            System.out.println("カレントディレクトリに保存します");
            path = System.getProperty("user.dir");
        }

        String url = "http://ncode.syosetu.com/" + ncode;

        try {

            Document document = Jsoup.connect(url).get();


            String title = document.getElementsByTag("title").text();
            title = title.replace(" ", " ").trim();


            File file = new File(path + File.separator + "[" + ncode + "]_" + title + ".txt");
            fileOperator(file);

            textCreator(file);
            System.exit(0);

        } catch (IOException e) {

            System.out.println("指定したNコードの小説は存在しません");
            System.exit(-1);

        }
    }


    static void textCreator(File file) throws IOException {

        int count = 1;

        while (true) {

            String url = "http://ncode.syosetu.com/" + ncode + "/" + String.valueOf(count);

            try {

                Document document = Jsoup.connect(url).get();

                String paragraphs = br+br+br+br+br+br+br+br+br+br;

                String subtitle = document.getElementsByClass("novel_subtitle").text();
                String content  = document.getElementById("novel_honbun").html().replaceAll("\n", br);

                String honbun = content.replaceAll("", "")
                                       .replaceAll("(\r)?\n$<", "<")
                                       .replaceAll("(\r)?(\n)? ?<[^>]+>(\r)?(\n)?", "")
                                       .replaceAll("  ", "");

                FileWriter fw = new FileWriter(file, true);

                fw.write(subtitle + " -----" + count + "-----" + paragraphs + honbun + paragraphs);
                fw.close();

                System.out.println(count + "話完了");
                count += 1;

                continue;

            } catch (IOException e) {

                System.out.println("全話完了");
                break;

            }
        }
    }


    private static void fileOperator(File file) throws IOException {

        if (file.exists()) {

            if (file.delete()) {
                System.out.println("全話更新します");
            } else {
                System.out.println("過去のファイルの削除に失敗しました");
                System.out.println("書き込み権限もしくは削除権限がない可能性があります");
                System.exit(-1);
            }

        } else {

            try {
                file.createNewFile();
                System.out.println("ファイルが見つからないので新規作成します");
            } catch (IOException e) {
                System.out.println("書き込み権限がありません");
                System.exit(-1);

            }
        }       
    }
}

Jsoup使ってるので導入してください。
MITライセンスか何かです。
何しても自由です。

Git?
ごめんなさいわかりません。
 
 
 

9
11
0

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
9
11