Search in sources :

Example 1 with ParseException

use of com.hippo.ehviewer.client.exception.ParseException in project EhViewer by seven332.

the class WhatsHotParser method parse.

@SuppressWarnings("ConstantConditions")
public static List<GalleryInfo> parse(String body) throws ParseException {
    try {
        List<GalleryInfo> galleryInfoList = new ArrayList<>(15);
        Document d = Jsoup.parse(body);
        Element pp = d.getElementById("pp");
        Elements id1List = pp.getElementsByClass("id1");
        for (int i = 0, n = id1List.size(); i < n; i++) {
            GalleryInfo galleryInfo = new GalleryInfo();
            Element id1 = id1List.get(i);
            Element id3 = JsoupUtils.getElementByClass(id1, "id3");
            Element temp = JsoupUtils.getElementByTag(id3, "a");
            String url = temp.attr("href");
            GalleryDetailUrlParser.Result result = GalleryDetailUrlParser.parse(url);
            galleryInfo.gid = result.gid;
            galleryInfo.token = result.token;
            temp = JsoupUtils.getElementByTag(temp, "img");
            galleryInfo.thumb = EhUtils.handleThumbUrlResolution(temp.attr("src"));
            galleryInfo.title = temp.attr("title");
            galleryInfo.generateSLang();
            galleryInfoList.add(galleryInfo);
        }
        return galleryInfoList;
    } catch (Exception e) {
        throw new ParseException("Parse whats hot error", body);
    }
}
Also used : Element(org.jsoup.nodes.Element) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) ArrayList(java.util.ArrayList) ParseException(com.hippo.ehviewer.client.exception.ParseException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 2 with ParseException

use of com.hippo.ehviewer.client.exception.ParseException in project EhViewer by seven332.

the class GalleryDetailParser method parsePreviewPages.

/**
 * Parse preview pages with regular expressions
 */
public static int parsePreviewPages(String body) throws ParseException {
    Matcher m = PATTERN_PREVIEW_PAGES.matcher(body);
    int previewPages = -1;
    if (m.find()) {
        previewPages = ParserUtils.parseInt(m.group(1), -1);
    }
    if (previewPages <= 0) {
        throw new ParseException("Parse preview page count error", body);
    }
    return previewPages;
}
Also used : Matcher(java.util.regex.Matcher) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 3 with ParseException

use of com.hippo.ehviewer.client.exception.ParseException in project EhViewer by seven332.

the class GalleryDetailParser method parsePages.

/**
 * Parse pages with regular expressions
 */
public static int parsePages(String body) throws ParseException {
    int pages = -1;
    Matcher m = PATTERN_PAGES.matcher(body);
    if (m.find()) {
        pages = ParserUtils.parseInt(m.group(1), -1);
    }
    if (pages < 0) {
        throw new ParseException("Parse pages error", body);
    }
    return pages;
}
Also used : Matcher(java.util.regex.Matcher) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 4 with ParseException

use of com.hippo.ehviewer.client.exception.ParseException in project EhViewer by seven332.

the class GalleryDetailParser method parseDetail.

@SuppressWarnings("ConstantConditions")
private static void parseDetail(GalleryDetail gd, Document d, String body) throws ParseException {
    Matcher matcher = PATTERN_DETAIL.matcher(body);
    if (matcher.find()) {
        gd.gid = NumberUtils.parseLongSafely(matcher.group(1), -1L);
        gd.token = matcher.group(2);
        gd.apiUid = NumberUtils.parseLongSafely(matcher.group(3), -1L);
        gd.apiKey = matcher.group(4);
    } else {
        throw new ParseException("Can't parse gallery detail", body);
    }
    if (gd.gid == -1L) {
        throw new ParseException("Can't parse gallery detail", body);
    }
    matcher = PATTERN_TORRENT.matcher(body);
    if (matcher.find()) {
        gd.torrentUrl = StringUtils.unescapeXml(StringUtils.trim(matcher.group(1)));
        gd.torrentCount = NumberUtils.parseIntSafely(matcher.group(2), 0);
    } else {
        gd.torrentCount = 0;
        gd.torrentUrl = "";
    }
    matcher = PATTERN_ARCHIVE.matcher(body);
    if (matcher.find()) {
        gd.archiveUrl = StringUtils.unescapeXml(StringUtils.trim(matcher.group(1)));
    } else {
        gd.archiveUrl = "";
    }
    try {
        Element gm = JsoupUtils.getElementByClass(d, "gm");
        // Thumb url
        Element gd1 = gm.getElementById("gd1");
        try {
            gd.thumb = parseCoverStyle(StringUtils.trim(gd1.child(0).attr("style")));
        } catch (Throwable e) {
            ExceptionUtils.throwIfFatal(e);
            gd.thumb = "";
        }
        // Title
        Element gn = gm.getElementById("gn");
        if (null != gn) {
            gd.title = StringUtils.trim(gn.text());
        } else {
            gd.title = "";
        }
        // Jpn title
        Element gj = gm.getElementById("gj");
        if (null != gj) {
            gd.titleJpn = StringUtils.trim(gj.text());
        } else {
            gd.titleJpn = "";
        }
        // Category
        Element gdc = gm.getElementById("gdc");
        try {
            Element ce = JsoupUtils.getElementByClass(gdc, "cn");
            if (ce == null) {
                ce = JsoupUtils.getElementByClass(gdc, "cs");
            }
            gd.category = EhUtils.getCategory(ce.text());
        } catch (Throwable e) {
            ExceptionUtils.throwIfFatal(e);
            gd.category = EhUtils.UNKNOWN;
        }
        // Uploader
        Element gdn = gm.getElementById("gdn");
        if (null != gdn) {
            gd.uploader = StringUtils.trim(gdn.text());
        } else {
            gd.uploader = "";
        }
        Element gdd = gm.getElementById("gdd");
        gd.posted = "";
        gd.parent = "";
        gd.visible = "";
        gd.visible = "";
        gd.size = "";
        gd.pages = 0;
        gd.favoriteCount = 0;
        try {
            Elements es = gdd.child(0).child(0).children();
            for (int i = 0, n = es.size(); i < n; i++) {
                parseDetailInfo(gd, es.get(i), body);
            }
        } catch (Throwable e) {
            ExceptionUtils.throwIfFatal(e);
        // Ignore
        }
        // Rating count
        Element rating_count = gm.getElementById("rating_count");
        if (null != rating_count) {
            gd.ratingCount = NumberUtils.parseIntSafely(StringUtils.trim(rating_count.text()), 0);
        } else {
            gd.ratingCount = 0;
        }
        // Rating
        Element rating_label = gm.getElementById("rating_label");
        if (null != rating_label) {
            String ratingStr = StringUtils.trim(rating_label.text());
            if ("Not Yet Rated".equals(ratingStr)) {
                gd.rating = -1.0f;
            } else {
                int index = ratingStr.indexOf(' ');
                if (index == -1 || index >= ratingStr.length()) {
                    gd.rating = 0f;
                } else {
                    gd.rating = NumberUtils.parseFloatSafely(ratingStr.substring(index + 1), 0f);
                }
            }
        } else {
            gd.rating = -1.0f;
        }
        // isFavorited
        Element gdf = gm.getElementById("gdf");
        gd.isFavorited = null != gdf && !StringUtils.trim(gdf.text()).equals("Add to Favorites");
        if (gdf != null) {
            final String favoriteName = StringUtils.trim(gdf.text());
            if (favoriteName.equals("Add to Favorites")) {
                gd.favoriteName = null;
            } else {
                gd.favoriteName = StringUtils.trim(gdf.text());
            }
        }
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        throw new ParseException("Can't parse gallery detail", body);
    }
}
Also used : Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) ParseException(com.hippo.ehviewer.client.exception.ParseException) Elements(org.jsoup.select.Elements)

Example 5 with ParseException

use of com.hippo.ehviewer.client.exception.ParseException in project EhViewer by seven332.

the class FavoritesParser method parse.

public static Result parse(String body) throws Exception {
    if (body.contains("This page requires you to log on.</p>")) {
        throw new EhException(GetText.getString(R.string.need_sign_in));
    }
    String[] catArray = new String[10];
    int[] countArray = new int[10];
    try {
        Document d = Jsoup.parse(body);
        Element ido = JsoupUtils.getElementByClass(d, "ido");
        // noinspection ConstantConditions
        Elements fps = ido.getElementsByClass("fp");
        // Last one is "fp fps"
        AssertUtils.assertEquals(11, fps.size());
        for (int i = 0; i < 10; i++) {
            Element fp = fps.get(i);
            countArray[i] = ParserUtils.parseInt(fp.child(0).text(), 0);
            catArray[i] = ParserUtils.trim(fp.child(2).text());
        }
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        throw new ParseException("Parse favorites error", body);
    }
    GalleryListParser.Result result = GalleryListParser.parse(body);
    Result re = new Result();
    re.catArray = catArray;
    re.countArray = countArray;
    re.pages = result.pages;
    re.nextPage = result.nextPage;
    re.galleryInfoList = result.galleryInfoList;
    return re;
}
Also used : Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Aggregations

ParseException (com.hippo.ehviewer.client.exception.ParseException)14 Matcher (java.util.regex.Matcher)8 Element (org.jsoup.nodes.Element)7 Document (org.jsoup.nodes.Document)5 Elements (org.jsoup.select.Elements)5 GalleryInfo (com.hippo.ehviewer.client.data.GalleryInfo)2 LargePreviewSet (com.hippo.ehviewer.client.data.LargePreviewSet)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 NormalPreviewSet (com.hippo.ehviewer.client.data.NormalPreviewSet)1 EhException (com.hippo.ehviewer.client.exception.EhException)1