Support voting gallery tags

This commit is contained in:
NekoInverter committed 2020-11-01 12:52:50 +08:00
1 parent 06d31b81c9
commit 20b15b8794
7 files changed
+150 -5

No files matched your search

@@ -54,6 +54,7 @@ public class EhClient {
public static final int METHOD_IMAGE_SEARCH = 16;
public static final int METHOD_ARCHIVE_LIST = 17;
public static final int METHOD_DOWNLOAD_ARCHIVE = 18;
public static final int METHOD_VOTE_TAG = 19;
private final ThreadPoolExecutor mRequestThreadPool;
private final OkHttpClient mOkHttpClient;
@@ -175,6 +176,8 @@ public class EhClient {
return EhEngine.getArchiveList(this, mOkHttpClient, (String) params[0], (Long) params[1], (String) params[2]);
case METHOD_DOWNLOAD_ARCHIVE:
return EhEngine.downloadArchive(this, mOkHttpClient, (Long) params[0], (String) params[1], (String) params[2], (String) params[3]);
case METHOD_VOTE_TAG:
return EhEngine.voteTag(this, mOkHttpClient, (Long) params[0], (String) params[1], (Long) params[2], (String) params[3], (String) params[4], (Integer) params[5]);
default:
return new IllegalStateException("Can't detect method " + mMethod);
}
@@ -48,6 +48,7 @@ import com.hippo.ehviewer.client.parser.RateGalleryParser;
import com.hippo.ehviewer.client.parser.SignInParser;
import com.hippo.ehviewer.client.parser.TorrentParser;
import com.hippo.ehviewer.client.parser.VoteCommentParser;
import com.hippo.ehviewer.client.parser.VoteTagParser;
import com.hippo.network.StatusCodeException;
import com.hippo.util.ExceptionUtils;
import com.hippo.yorozuya.AssertUtils;
@@ -852,6 +853,47 @@ public class EhEngine {
}
}
public static VoteTagParser.Result voteTag(@Nullable EhClient.Task task, OkHttpClient okHttpClient,
long apiUid, String apiKey, long gid, String token, String tags, int vote) throws Throwable {
final JSONObject json = new JSONObject();
json.put("method", "taggallery");
json.put("apiuid", apiUid);
json.put("apikey", apiKey);
json.put("gid", gid);
json.put("token", token);
json.put("tags", tags);
json.put("vote", vote);
final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json.toString());
String url = EhUrl.getApiUrl();
String referer = EhUrl.getReferer();
String origin = EhUrl.getOrigin();
Log.d(TAG, url);
Request request = new EhRequestBuilder(url, referer, origin)
.post(requestBody)
.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();
return VoteTagParser.parse(body);
} catch (Throwable e) {
ExceptionUtils.throwIfFatal(e);
throwException(call, code, headers, body, e);
throw e;
}
}
/**
* @param image Must be jpeg
*/
@@ -0,0 +1,19 @@
package com.hippo.ehviewer.client.parser;
import org.json.JSONException;
import org.json.JSONObject;
public class VoteTagParser {
// {"error":"The tag \"neko\" is not allowed. Use character:neko or artist:neko"}
public static VoteTagParser.Result parse(String body) throws JSONException {
VoteTagParser.Result result = new VoteTagParser.Result();
JSONObject jo = new JSONObject(body);
result.error = jo.getString("error");
return result;
}
public static class Result {
public String error;
}
}
@@ -86,6 +86,7 @@ import com.hippo.ehviewer.client.data.ListUrlBuilder;
import com.hippo.ehviewer.client.data.PreviewSet;
import com.hippo.ehviewer.client.exception.NoHAtHClientException;
import com.hippo.ehviewer.client.parser.RateGalleryParser;
import com.hippo.ehviewer.client.parser.VoteTagParser;
import com.hippo.ehviewer.dao.DownloadInfo;
import com.hippo.ehviewer.dao.Filter;
import com.hippo.ehviewer.ui.CommonOperations;
@@ -114,6 +115,7 @@ import com.hippo.yorozuya.IOUtils;
import com.hippo.yorozuya.IntIdGenerator;
import com.hippo.yorozuya.SimpleHandler;
import com.hippo.yorozuya.ViewUtils;
import com.hippo.yorozuya.collect.IntList;
import java.io.File;
import java.io.FileNotFoundException;
@@ -1417,20 +1419,58 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
}
final String tag2 = temp;
List<String> menu = new ArrayList<>();
final IntList menuId = new IntList();
Resources resources = context.getResources();
menu.add(resources.getString(R.string.show_definition));
menuId.add(R.id.show_definition);
menu.add(resources.getString(R.string.add_filter));
menuId.add(R.id.add_filter);
menu.add(resources.getString(R.string.tag_vote_up));
menuId.add(R.id.vote_up);
menu.add(resources.getString(R.string.tag_vote_down));
menuId.add(R.id.vote_down);
new MaterialAlertDialogBuilder(context)
.setTitle(tag)
.setItems(R.array.tag_menu_entries, (dialog, which) -> {
switch (which) {
case 0:
.setItems(menu.toArray(new String[0]), (dialog, which) -> {
if (which < 0 || which >= menuId.size()) {
return;
}
int id = menuId.get(which);
switch (id) {
case R.id.vote_up:
voteTag(tag2, 1);
break;
case R.id.vote_down:
voteTag(tag2, -1);
break;
case R.id.show_definition:
UrlOpener.openUrl(context, EhUrl.getTagDefinitionUrl(tag2), false);
break;
case 1:
case R.id.add_filter:
showFilterTagDialog(tag);
break;
}
}).show();
}
private void voteTag(String tag, int vote) {
Context context = getContext2();
MainActivity activity = getActivity2();
if (null == context || null == activity) {
return;
}
EhRequest request = new EhRequest()
.setMethod(EhClient.METHOD_VOTE_TAG)
.setArgs(mGalleryDetail.apiUid, mGalleryDetail.apiKey, mGid, mToken, tag, vote)
.setCallback(new VoteTagListener(context,
activity.getStageId(), getTag(), mGid));
EhApplication.getEhClient(context).execute(request);
}
@Override
public boolean onLongClick(View v) {
MainActivity activity = getActivity2();
@@ -1716,6 +1756,39 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
}
}
private static class VoteTagListener extends EhCallback<GalleryDetailScene, VoteTagParser.Result> {
private final long mGid;
public VoteTagListener(Context context, int stageId, String sceneTag, long gid) {
super(context, stageId, sceneTag);
mGid = gid;
}
@Override
public void onSuccess(VoteTagParser.Result result) {
if (!TextUtils.isEmpty(result.error)) {
showTip(result.error, LENGTH_SHORT);
} else {
showTip(R.string.tag_vote_successfully, LENGTH_SHORT);
}
}
@Override
public void onFailure(Exception e) {
showTip(R.string.vote_failed, LENGTH_LONG);
}
@Override
public void onCancel() {
}
@Override
public boolean isInstance(SceneFragment scene) {
return scene instanceof GalleryDetailScene;
}
}
private static class RateGalleryListener extends EhCallback<GalleryDetailScene, RateGalleryParser.Result> {
private final long mGid;