Package org.fuwanovel.java.api

Source Code of org.fuwanovel.java.api.FuwaNovelWorker

/*
* Copyright (c) 2013, Anton Larionov
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.fuwanovel.java.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.fuwanovel.java.api.deserializer.JsonBooleanDeserializer;
import org.fuwanovel.java.api.deserializer.JsonDateDeserializer;
import org.fuwanovel.java.api.deserializer.JsonTagsDeserializer;
import org.fuwanovel.java.api.entity.NovelInfo;
import org.fuwanovel.java.api.entity.NovelInfoExt;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

public class FuwaNovelWorker {
    public static final String DEFAULT_ENDPOINT_LIST = "http://fuwanovel.org/api/";
    public static final String DEFAULT_ENDPOINT_INFO = "http://fuwanovel.org/api/novels/%s";

    public static final String DEFAULT_ENDPOINT_MAIN_ART = "http://fuwanovel.org/application/assets/img/vns/%s/mainArt.jpg";
    public static final String DEFAULT_ENDPOINT_BANNER_ART = "http://fuwanovel.org/application/assets/img/vns/%s/bannerArt.jpg";
    public static final String DEFAULT_ENDPOINT_FRONT_PAGE_ART = "http://fuwanovel.org/application/assets/img/vns/%s/frontPageArt.jpg";
    public static final String DEFAULT_ENDPOINT_THUMBNAIL = "http://fuwanovel.org/application/assets/img/vns/%s/screen0%s_thumb.jpg";
    public static final String DEFAULT_ENDPOINT_SCREENSHOT = "http://fuwanovel.org/application/assets/img/vns/%s/screen0%s.jpg";
    public static final String DEFAULT_ENDPOINT_TORRENT = "http://fuwanovel.org/application/assets/downloads/%s";

    protected final Logger logger = Logger.getLogger(getClass().getName());

    protected String endPointList;
    protected String endPointInfo;

    protected String endPointMainArt;
    protected String endPointBannerArt;
    protected String endPointFrontPageArt;
    protected String endPointThumbnail;
    protected String endPointScreenshot;
    protected String endPointTorrent;


    public FuwaNovelWorker() {
        this(DEFAULT_ENDPOINT_LIST,
                DEFAULT_ENDPOINT_INFO,
                DEFAULT_ENDPOINT_MAIN_ART,
                DEFAULT_ENDPOINT_BANNER_ART,
                DEFAULT_ENDPOINT_FRONT_PAGE_ART,
                DEFAULT_ENDPOINT_THUMBNAIL,
                DEFAULT_ENDPOINT_SCREENSHOT,
                DEFAULT_ENDPOINT_TORRENT);
    }


    public FuwaNovelWorker(String endPointList,
                           String endPointInfo,
                           String endPointMainArt,
                           String endPointBannerArt,
                           String endPointFrontPageArt,
                           String endPointThumbnail,
                           String endPointScreenshot,
                           String endPointTorrent) {

        this.endPointList = endPointList;
        this.endPointInfo = endPointInfo;
        this.endPointMainArt = endPointMainArt;
        this.endPointBannerArt = endPointBannerArt;
        this.endPointFrontPageArt = endPointFrontPageArt;
        this.endPointThumbnail = endPointThumbnail;
        this.endPointScreenshot = endPointScreenshot;
        this.endPointTorrent = endPointTorrent;
    }

    public List<NovelInfo> getNovelList() {
        String inputString = readStringFromUrl(endPointList);
        List<NovelInfo> novelInfos = getNovelInfoFromString(inputString);
        return novelInfos;
    }

    public NovelInfoExt getNovelInfoExt(int novelId) {
        String inputString = readStringFromUrl(String.format(endPointInfo, novelId));
        NovelInfoExt novelInfoExt = getNovelInfoExtFromString(inputString);
        return novelInfoExt;
    }

    public NovelInfoExt refineNovelInfoExt(NovelInfoExt info) {
        if (info.getMainArt() != null && info.getMainArt()) {
            info.setFileMainArt(readBufferedImageFromUrl(String.format(endPointMainArt, info.getId())));
        }

        if (info.getBannerArt() != null && info.getBannerArt()) {
            info.setFileBannerArt(readBufferedImageFromUrl(String.format(endPointBannerArt, info.getId())));
        }

        if (info.getFrontPageArt() != null && info.getFrontPageArt()) {
            info.setFileFrontPageArt(readBufferedImageFromUrl(String.format(endPointFrontPageArt, info.getId())));
        }


        info.setFileThumbnails(new ArrayList<BufferedImage>());
        info.setFileScreens(new ArrayList<BufferedImage>());

        if (info.getScreen01() != null && info.getScreen01()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 1)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 1)));
        }

        if (info.getScreen02() != null && info.getScreen02()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 2)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 2)));
        }

        if (info.getScreen03() != null && info.getScreen03()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 3)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 3)));
        }

        if (info.getScreen04() != null && info.getScreen04()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 4)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 4)));
        }

        if (info.getScreen05() != null && info.getScreen05()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 5)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 5)));
        }

        if (info.getScreen06() != null && info.getScreen06()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 6)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 6)));
        }

        if (info.getScreen07() != null && info.getScreen07()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 7)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 7)));
        }

        if (info.getScreen08() != null && info.getScreen08()) {
            info.getFileThumbnails().add(readBufferedImageFromUrl(String.format(endPointThumbnail, info.getId(), 8)));
            info.getFileScreens().add(readBufferedImageFromUrl(String.format(endPointScreenshot, info.getId(), 8)));
        }

        return info;
    }

    private NovelInfoExt getNovelInfoExtFromString(String inputString) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Boolean.class, new JsonBooleanDeserializer());
        gsonBuilder.registerTypeAdapter(Date.class, new JsonDateDeserializer());
        gsonBuilder.registerTypeAdapter(new TypeToken<List<String>>() {
        }.getType(),
                new JsonTagsDeserializer());
        Gson gson = gsonBuilder.create();
        NovelInfoExt novelInfoExt = gson.fromJson(inputString, new TypeToken<NovelInfoExt>() {
        }.getType());
        return novelInfoExt;
    }


    protected List<NovelInfo> getNovelInfoFromString(String inputString) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Boolean.class, new JsonBooleanDeserializer());
        gsonBuilder.registerTypeAdapter(new TypeToken<List<String>>() {
        }.getType(),
                new JsonTagsDeserializer());
        Gson gson = gsonBuilder.create();
        List<NovelInfo> novelInfos = gson.fromJson(inputString, new TypeToken<List<NovelInfo>>() {
        }.getType());
        return novelInfos;
    }

    protected BufferedImage readBufferedImageFromUrl(String urlAddress) {
        try {
            BufferedImage img = ImageIO.read(new URL(urlAddress));
            return img;
        } catch (IOException e) {
            logger.warning(String.format("Cannot read image file from url = '%s'", urlAddress));
            return null;
        }
    }

    protected String readStringFromUrl(String urlAddress) {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlAddress);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));

            StringBuilder result = new StringBuilder();

            String readed;
            while ((readed = reader.readLine()) != null) {
                result.append(readed);
            }

            return result.toString();

        } catch (MalformedURLException e) {
            logger.warning("Error occurred while reading file from '" + urlAddress + "' : " + e.toString());
            return null;
        } catch (IOException e) {
            logger.warning("Error occurred while reading file from '" + urlAddress + "' : " + e.toString());
            return null;
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.warning("Error occurred while reading file from '" + urlAddress + "' : " + e.toString());
                }
        }
    }
}
TOP

Related Classes of org.fuwanovel.java.api.FuwaNovelWorker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.