Search in sources :

Example 6 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 7 with ParseException

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

the class ForumsParser method parse.

public static String parse(String body) throws ParseException {
    try {
        Document d = Jsoup.parse(body, EhUrl.URL_FORUMS);
        Element userlinks = d.getElementById("userlinks");
        Element child = userlinks.child(0).child(0).child(0);
        return child.attr("href");
    } catch (Exception e) {
        throw new ParseException("Parse forums error", body);
    }
}
Also used : Element(org.jsoup.nodes.Element) ParseException(com.hippo.ehviewer.client.exception.ParseException) Document(org.jsoup.nodes.Document) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 8 with ParseException

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

the class GalleryDetailParser method parseLargePreviewSet.

/**
 * Parse large previews with regular expressions
 */
private static LargePreviewSet parseLargePreviewSet(String body) throws ParseException {
    Matcher m = PATTERN_LARGE_PREVIEW.matcher(body);
    LargePreviewSet largePreviewSet = new LargePreviewSet();
    while (m.find()) {
        int index = ParserUtils.parseInt(m.group(2)) - 1;
        String imageUrl = ParserUtils.trim(m.group(3));
        String pageUrl = ParserUtils.trim(m.group(1));
        if (Settings.getFixThumbUrl()) {
            imageUrl = EhUrl.getFixedPreviewThumbUrl(imageUrl);
        }
        largePreviewSet.addItem(index, imageUrl, pageUrl);
    }
    if (largePreviewSet.size() == 0) {
        throw new ParseException("Can't parse large preview", body);
    }
    return largePreviewSet;
}
Also used : Matcher(java.util.regex.Matcher) LargePreviewSet(com.hippo.ehviewer.client.data.LargePreviewSet) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 9 with ParseException

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

the class GalleryDetailParser method parseNormalPreviewSet.

/**
 * Parse normal previews with regular expressions
 */
private static NormalPreviewSet parseNormalPreviewSet(String body) throws ParseException {
    Matcher m = PATTERN_NORMAL_PREVIEW.matcher(body);
    NormalPreviewSet normalPreviewSet = new NormalPreviewSet();
    while (m.find()) {
        normalPreviewSet.addItem(ParserUtils.parseInt(m.group(6)) - 1, ParserUtils.trim(m.group(3)), ParserUtils.parseInt((m.group(4))), 0, ParserUtils.parseInt(m.group(1)), ParserUtils.parseInt(m.group(2)), ParserUtils.trim(m.group(5)));
    }
    if (normalPreviewSet.size() == 0) {
        throw new ParseException("Can't parse normal preview", body);
    }
    return normalPreviewSet;
}
Also used : NormalPreviewSet(com.hippo.ehviewer.client.data.NormalPreviewSet) Matcher(java.util.regex.Matcher) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 10 with ParseException

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

the class GalleryListParser method parse.

public static Result parse(@NonNull String body) throws Exception {
    Result result = new Result();
    Document d = Jsoup.parse(body);
    try {
        result.pages = parsePages(d, body);
    } catch (ParseException e) {
        if (body.contains("No hits found</p>")) {
            result.pages = 0;
            // noinspection unchecked
            result.galleryInfoList = Collections.EMPTY_LIST;
            return result;
        } else {
            throw e;
        }
    }
    try {
        Elements es = d.getElementsByClass("itg").first().child(0).children();
        List<GalleryInfo> list = new ArrayList<>(es.size() - 1);
        for (int i = 1; i < es.size(); i++) {
            // First one is table header, skip it
            GalleryInfo gi = parseGalleryInfo(es.get(i));
            if (null != gi) {
                list.add(gi);
            }
        }
        result.galleryInfoList = list;
    } catch (Exception e) {
        throw new ParseException("Can't parse gallery list", body);
    }
    return result;
}
Also used : 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)

Aggregations

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