001package csv2html;
002
003import java.awt.image.BufferedImage;
004import java.io.File;
005import java.util.List;
006import utility.ImageUtility;
007
008/**
009 * ダウンローダ:CSVファイル・画像ファイル・サムネイル画像ファイルをダウンロードする。
010 */
011public class Downloader extends IO
012{
013        /**
014         * ダウンローダのコンストラクタ。
015         * @param aTable テーブル
016         */
017        public Downloader(Table aTable)
018        {
019                super(aTable);
020
021                return;
022        }
023
024        /**
025         * 総理大臣の情報を記したCSVファイルをダウンロードする。
026         */
027        public void downloadCSV()
028        {
029                String csvUrl = this.attributes().csvUrl();
030                System.out.println("From: " + csvUrl);
031                List<String> aCollection = IO.readTextFromURL(csvUrl);
032
033                String basename = csvUrl.substring(csvUrl.lastIndexOf('/') + 1);
034                File aFile = new File(this.attributes().baseDirectory() + basename);
035                System.out.println("To: " + aFile);
036                IO.writeText(aCollection, aFile);
037
038                return;
039        }
040
041        /**
042         * 総理大臣の画像群をダウンロードする。
043         */
044        public void downloadImages()
045        {
046                int indexOfImage = this.attributes().indexOfImage();
047                this.downloadPictures(indexOfImage);
048
049                return;
050        }
051
052        /**
053         * 総理大臣の画像群またはサムネイル画像群をダウンロードする。
054         * @param indexOfPicture 画像のインデックス
055         */
056        private void downloadPictures(int indexOfPicture)
057        {
058                for (Tuple aTuple : this.tuples())
059                {
060                        String theString = aTuple.values().get(indexOfPicture);
061
062                        // タプルの当該カラムからURL文字列を作って、当該のURLから画像を読み込む。
063                        String aString = this.attributes().baseUrl() + theString;
064                        System.out.println("From: " + aString);
065
066                        BufferedImage anImage = ImageUtility.readImageFromURL(aString);
067
068                        // タプルの当該カラムからダウンロード先のファイルを作って、当該のファイルに画像を書き込む。
069                        List<String> aCollection = IO.splitString(theString, "/");
070                        StringBuffer aBuffer = new StringBuffer();
071                        int lastIndex = aCollection.size() - 1;
072                        for (int index = 0; index < lastIndex; index++)
073                        {
074                                aBuffer.append(aCollection.get(index));
075                                aBuffer.append(File.separator);
076                        }
077                        aString = aBuffer.toString() + aCollection.get(lastIndex);
078                        File aFile = new File(this.attributes().baseDirectory() + aString);
079                        System.out.println("To: " + aFile);
080
081                        File aDirectory = new File(aFile.getParent());
082                        if (!aDirectory.exists()) { aDirectory.mkdirs(); }
083                        ImageUtility.writeImage(anImage, aFile);
084                }
085
086                return;
087        }
088
089        /**
090         * 総理大臣の画像群をダウンロードする。
091         */
092        public void downloadThumbnails()
093        {
094                int indexOfThumbnail = this.attributes().indexOfThumbnail();
095                this.downloadPictures(indexOfThumbnail);
096
097                return;
098        }
099
100        /**
101         * 総理大臣の情報を記したCSVファイルをダウンロードして、画像群やサムネイル画像群もダウロードする。
102         */
103        public void perform()
104        {
105                this.downloadCSV();
106                Reader aReader = new Reader(this.table());
107                aReader.perform();
108                this.downloadImages();
109                this.downloadThumbnails();
110
111                return;
112        }
113}