Search in sources :

Example 1 with EhException

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

the class GalleryListScene method onApplySearch.

@Override
public void onApplySearch(String query) {
    if (null == mUrlBuilder || null == mHelper || null == mSearchLayout) {
        return;
    }
    if (mState == STATE_SEARCH || mState == STATE_SEARCH_SHOW_LIST) {
        if (mSearchLayout.isSpecifyGallery()) {
            int index = query.indexOf(' ');
            if (index <= 0 || index >= query.length() - 1) {
                showTip(R.string.error_invalid_specify_gallery, LENGTH_LONG);
                return;
            }
            long gid;
            String token;
            try {
                gid = Long.parseLong(query.substring(0, index));
            } catch (NumberFormatException e) {
                showTip(R.string.error_invalid_specify_gallery, LENGTH_LONG);
                return;
            }
            token = query.substring(index + 1);
            Bundle args = new Bundle();
            args.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN);
            args.putLong(GalleryDetailScene.KEY_GID, gid);
            args.putString(GalleryDetailScene.KEY_TOKEN, token);
            startScene(new Announcer(GalleryDetailScene.class).setArgs(args));
            return;
        } else {
            try {
                mSearchLayout.formatListUrlBuilder(mUrlBuilder, query);
            } catch (EhException e) {
                showTip(e.getMessage(), LENGTH_LONG);
                return;
            }
        }
    } else {
        mUrlBuilder.reset();
        mUrlBuilder.setKeyword(query);
    }
    onUpdateUrlBuilder();
    mHelper.refresh();
    setState(STATE_NORMAL);
}
Also used : EhException(com.hippo.ehviewer.client.exception.EhException) Announcer(com.hippo.scene.Announcer) Bundle(android.os.Bundle) Point(android.graphics.Point)

Example 2 with EhException

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

the class EhEngine method commentGallery.

public static GalleryComment[] commentGallery(@Nullable EhClient.Task task, OkHttpClient okHttpClient, String url, String comment) throws Exception {
    FormBody.Builder builder = new FormBody.Builder().add("commenttext_new", comment);
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        Document document = Jsoup.parse(body);
        Elements elements = document.select("#chd + p");
        if (elements.size() > 0) {
            throw new EhException(elements.get(0).text());
        }
        return GalleryDetailParser.parseComments(document);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
}
Also used : Call(okhttp3.Call) Headers(okhttp3.Headers) FormBody(okhttp3.FormBody) Request(okhttp3.Request) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException) Response(okhttp3.Response) EhException(com.hippo.ehviewer.client.exception.EhException)

Example 3 with EhException

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

the class EhEngine method modifyFavorites.

public static FavoritesParser.Result modifyFavorites(@Nullable EhClient.Task task, OkHttpClient okHttpClient, String url, long[] gidArray, int dstCat, boolean callApi) throws Exception {
    String catStr;
    if (dstCat == -1) {
        catStr = "delete";
    } else if (dstCat >= 0 && dstCat <= 9) {
        catStr = "fav" + dstCat;
    } else {
        throw new EhException("Invalid dstCat: " + dstCat);
    }
    FormBody.Builder builder = new FormBody.Builder();
    builder.add("ddact", catStr);
    for (long gid : gidArray) {
        builder.add("modifygids[]", Long.toString(gid));
    }
    builder.add("apply", "Apply");
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    FavoritesParser.Result result;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        result = FavoritesParser.parse(body);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
    if (callApi && result.galleryInfoList.size() > 0) {
        fillGalleryListByApi(task, okHttpClient, result.galleryInfoList);
    }
    for (GalleryInfo info : result.galleryInfoList) {
        info.thumb = EhUrl.getFixedPreviewThumbUrl(info.thumb);
    }
    return result;
}
Also used : Call(okhttp3.Call) Headers(okhttp3.Headers) FormBody(okhttp3.FormBody) Request(okhttp3.Request) GalleryInfo(com.hippo.ehviewer.client.data.GalleryInfo) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException) EhException(com.hippo.ehviewer.client.exception.EhException) Response(okhttp3.Response) FavoritesParser(com.hippo.ehviewer.client.parser.FavoritesParser)

Example 4 with EhException

use of com.hippo.ehviewer.client.exception.EhException 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"
        Assert.assertEquals(11, fps.size());
        for (int i = 0; i < 10; i++) {
            Element fp = fps.get(i);
            countArray[i] = ParserUtils.parseInt(fp.child(0).text());
            catArray[i] = ParserUtils.trim(fp.child(2).text());
        }
    } catch (Exception 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.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) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException)

Example 5 with EhException

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

the class EhEngine method downloadArchive.

public static Void downloadArchive(@Nullable EhClient.Task task, OkHttpClient okHttpClient, long gid, String token, String or, String res) throws Exception {
    if (or == null || or.length() == 0) {
        throw new EhException("Invalid form param or: " + or);
    }
    if (res == null || res.length() == 0) {
        throw new EhException("Invalid res: " + res);
    }
    FormBody.Builder builder = new FormBody.Builder();
    builder.add("hathdl_xres", res);
    String url = EhUrl.getDownloadArchive(gid, token, or);
    Log.d(TAG, url);
    Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).post(builder.build()).build();
    Call call = okHttpClient.newCall(request);
    // Put call
    if (null != task) {
        task.setCall(call);
    }
    String body = null;
    Headers headers = null;
    int code = -1;
    try {
        Response response = call.execute();
        code = response.code();
        headers = response.headers();
        body = response.body().string();
        throwException(call, code, headers, body, null);
    } catch (Exception e) {
        throwException(call, code, headers, body, e);
        throw e;
    }
    Matcher m = PATTERN_NEED_HATH_CLIENT.matcher(body);
    if (m.find()) {
        throw new NoHAtHClientException("No H@H client");
    }
    return null;
}
Also used : Call(okhttp3.Call) Matcher(java.util.regex.Matcher) Headers(okhttp3.Headers) FormBody(okhttp3.FormBody) Request(okhttp3.Request) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) EhException(com.hippo.ehviewer.client.exception.EhException) ParseException(com.hippo.ehviewer.client.exception.ParseException) CancelledException(com.hippo.ehviewer.client.exception.CancelledException) NoHAtHClientException(com.hippo.ehviewer.client.exception.NoHAtHClientException) StatusCodeException(com.hippo.network.StatusCodeException) EhException(com.hippo.ehviewer.client.exception.EhException) Response(okhttp3.Response)

Aggregations

EhException (com.hippo.ehviewer.client.exception.EhException)7 ParseException (com.hippo.ehviewer.client.exception.ParseException)5 CancelledException (com.hippo.ehviewer.client.exception.CancelledException)4 NoHAtHClientException (com.hippo.ehviewer.client.exception.NoHAtHClientException)4 StatusCodeException (com.hippo.network.StatusCodeException)4 Call (okhttp3.Call)4 FormBody (okhttp3.FormBody)4 Headers (okhttp3.Headers)4 Request (okhttp3.Request)4 Response (okhttp3.Response)4 Document (org.jsoup.nodes.Document)3 Matcher (java.util.regex.Matcher)2 Elements (org.jsoup.select.Elements)2 Point (android.graphics.Point)1 Bundle (android.os.Bundle)1 GalleryDetail (com.hippo.ehviewer.client.data.GalleryDetail)1 GalleryInfo (com.hippo.ehviewer.client.data.GalleryInfo)1 OffensiveException (com.hippo.ehviewer.client.exception.OffensiveException)1 PiningException (com.hippo.ehviewer.client.exception.PiningException)1 FavoritesParser (com.hippo.ehviewer.client.parser.FavoritesParser)1