Add rate gallery
This commit is contained in:
1 parent
f7b443e0fa
commit
dfbe3767fe
12 files changed
+395
-3
No files matched your search
@@ -41,6 +41,7 @@ public class EhClient {
|
||||
public static final int METHOD_GET_GALLERY_LIST = 1;
|
||||
public static final int METHOD_GET_GALLERY_DETAIL = 2;
|
||||
public static final int METHOD_GET_LARGE_PREVIEW_SET = 3;
|
||||
public static final int METHOD_GET_RATE_GALLERY = 4;
|
||||
|
||||
private final ThreadPoolExecutor mRequestThreadPool;
|
||||
private final OkHttpClient mOkHttpClient;
|
||||
@@ -149,6 +150,17 @@ public class EhClient {
|
||||
}
|
||||
}
|
||||
|
||||
private Object rateGallery(EhConfig ehConfig, Object... params) throws Exception {
|
||||
Call call = EhEngine.prepareRateGallery(mOkHttpClient, ehConfig,
|
||||
(Integer) params[0], (String) params[1], (Float) params[2]);
|
||||
if (!mStop) {
|
||||
mCall = call;
|
||||
return EhEngine.doRateGallery(call);
|
||||
} else {
|
||||
throw new CancelledException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doInBackground(Object... params) {
|
||||
try {
|
||||
@@ -161,6 +173,8 @@ public class EhClient {
|
||||
return getGalleryDetail(mEhConfig, params);
|
||||
case METHOD_GET_LARGE_PREVIEW_SET:
|
||||
return getLargePreviewSet(mEhConfig, params);
|
||||
case METHOD_GET_RATE_GALLERY:
|
||||
return rateGallery(mEhConfig, params);
|
||||
default:
|
||||
return new IllegalStateException("Can't detect method " + mMethod);
|
||||
}
|
||||
|
||||
@@ -26,14 +26,20 @@ import com.hippo.ehviewer.client.exception.CancelledException;
|
||||
import com.hippo.ehviewer.client.exception.EhException;
|
||||
import com.hippo.ehviewer.client.parser.GalleryDetailParser;
|
||||
import com.hippo.ehviewer.client.parser.GalleryListParser;
|
||||
import com.hippo.ehviewer.client.parser.RateGalleryParser;
|
||||
import com.hippo.ehviewer.client.parser.SignInParser;
|
||||
import com.hippo.network.StatusCodeException;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class EhEngine {
|
||||
@@ -44,6 +50,11 @@ public class EhEngine {
|
||||
private static final String SAD_PANDA_TYPE = "image/gif";
|
||||
private static final String SAD_PANDA_LENGTH = "9615";
|
||||
|
||||
public static final long APIUID = 1363542;
|
||||
public static final String APIKEY = "f4b5407ab1727b9d08d7";
|
||||
|
||||
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
private static void throwException(Call call, int code, @Nullable Headers headers,
|
||||
@Nullable String body, Exception e) throws Exception {
|
||||
if (call.isCanceled()) {
|
||||
@@ -167,4 +178,42 @@ public class EhEngine {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// rating 0.0 - 0.5
|
||||
public static Call prepareRateGallery(OkHttpClient okHttpClient,
|
||||
EhConfig ehConfig, int gid, String token, float rating) throws JSONException {
|
||||
final JSONObject json = new JSONObject();
|
||||
json.put("method", "rategallery");
|
||||
json.put("apiuid", APIUID);
|
||||
json.put("apikey", APIKEY);
|
||||
json.put("gid", gid);
|
||||
json.put("token", token);
|
||||
json.put("rating", (int) Math.ceil(rating * 2));
|
||||
|
||||
final RequestBody body = RequestBody.create(JSON, json.toString());
|
||||
|
||||
String url = EhUrl.API_EX;
|
||||
Log.d(TAG, url);
|
||||
|
||||
Request request = new EhRequestBuilder(url, ehConfig)
|
||||
.post(body)
|
||||
.build();
|
||||
return okHttpClient.newCall(request);
|
||||
}
|
||||
|
||||
public static RateGalleryParser.Result doRateGallery(Call call) throws Exception {
|
||||
String body = null;
|
||||
Headers headers = null;
|
||||
int code = -1;
|
||||
try {
|
||||
Response response = call.execute();
|
||||
code = response.code();
|
||||
headers = response.headers();
|
||||
body = response.body().string();
|
||||
return RateGalleryParser.parse(body);
|
||||
} catch (Exception e) {
|
||||
throwException(call, code, headers, body, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,8 @@ public class EhUrl {
|
||||
|
||||
public static final String API_SIGN_IN = "http://forums.e-hentai.org/index.php?act=Login&CODE=01";
|
||||
|
||||
public static final String API_EX = HOST_EX + "api.php";
|
||||
|
||||
public static String getGalleryDetailUrl(int gid, String token) {
|
||||
return getGalleryDetailUrl(gid, token, 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2015 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.ehviewer.client.parser;
|
||||
|
||||
import com.hippo.ehviewer.client.exception.ParseException;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class RateGalleryParser {
|
||||
|
||||
public static class Result {
|
||||
public float rating;
|
||||
public int ratedTimes;
|
||||
}
|
||||
|
||||
public static Result parse(String body) throws Exception {
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(body);
|
||||
Result result = new Result();
|
||||
result.rating = (float)jsonObject.getDouble("rating_avg");
|
||||
result.ratedTimes = jsonObject.getInt("rating_cnt");
|
||||
return result;
|
||||
} catch (JSONException e) {
|
||||
Exception exception = new ParseException("Can't parse rate gallery", body);
|
||||
exception.initCause(e);
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user