Add get profile

This commit is contained in:
seven332 committed 2016-03-10 00:44:54 +08:00
1 parent 6549f070eb
commit 8650565829
10 files changed
+264 -71

No files matched your search

@@ -112,6 +112,17 @@ public class Settings {
putString(KEY_DISPLAY_NAME, value);
}
private static final String KEY_AVATAR = "avatar";
private static final String DEFAULT_AVATAR = null;
public static String getAvatar() {
return getString(KEY_AVATAR, DEFAULT_AVATAR);
}
public static void putAvatar(String value) {
putString(KEY_AVATAR, value);
}
private static final String KEY_SHOW_WARNING = "show_warning";
private static final boolean DEFAULT_SHOW_WARNING = true;
@@ -55,6 +55,7 @@ public class EhClient {
public static final int METHOD_MODIFY_FAVORITES = 11;
public static final int METHOD_GET_TORRENT_LIST = 12;
public static final int METHOD_GET_WHATS_HOT = 13;
public static final int METHOD_GET_PROFILE = 14;
private final ThreadPoolExecutor mRequestThreadPool;
private final OkHttpClient mOkHttpClient;
@@ -174,6 +175,8 @@ public class EhClient {
return EhEngine.getTorrentList(this, mOkHttpClient, (String) params[0]);
case METHOD_GET_WHATS_HOT:
return EhEngine.getWhatsHot(this, mOkHttpClient);
case METHOD_GET_PROFILE:
return EhEngine.getProfile(this, mOkHttpClient);
default:
return new IllegalStateException("Can't detect method " + mMethod);
}
@@ -30,10 +30,12 @@ import com.hippo.ehviewer.client.exception.CancelledException;
import com.hippo.ehviewer.client.exception.EhException;
import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.ehviewer.client.parser.FavoritesParser;
import com.hippo.ehviewer.client.parser.ForumsParser;
import com.hippo.ehviewer.client.parser.GalleryApiParser;
import com.hippo.ehviewer.client.parser.GalleryDetailParser;
import com.hippo.ehviewer.client.parser.GalleryListParser;
import com.hippo.ehviewer.client.parser.GalleryTokenApiParser;
import com.hippo.ehviewer.client.parser.ProfileParser;
import com.hippo.ehviewer.client.parser.RateGalleryParser;
import com.hippo.ehviewer.client.parser.SignInParser;
import com.hippo.ehviewer.client.parser.TorrentParser;
@@ -546,4 +548,53 @@ public class EhEngine {
throw e;
}
}
private static ProfileParser.Result getProfileInternal(EhClient.Task task,
OkHttpClient okHttpClient, String url) throws Exception {
Log.d(TAG, url);
Request request = new EhRequestBuilder(url, task.getEhConfig()).build();
Call call = okHttpClient.newCall(request);
// Put call
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 ProfileParser.parse(body);
} catch (Exception e) {
throwException(call, code, headers, body, e);
throw e;
}
}
public static ProfileParser.Result getProfile(EhClient.Task task,
OkHttpClient okHttpClient) throws Exception {
String url = EhUrl.URL_FORUMS;
Log.d(TAG, url);
Request request = new EhRequestBuilder(url, task.getEhConfig()).build();
Call call = okHttpClient.newCall(request);
// Put call
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 getProfileInternal(task, okHttpClient, ForumsParser.parse(body));
} catch (Exception e) {
throwException(call, code, headers, body, e);
throw e;
}
}
}
@@ -29,12 +29,13 @@ public class EhUrl {
public static final String HOST_G = "http://" + DOMAIN_G + "/";
public static final String API_SIGN_IN = "http://forums.e-hentai.org/index.php?act=Login&CODE=01";
public static final String API_REGISTER = "http://forums.e-hentai.org/index.php?act=Reg&CODE=00";
public static final String API_EX = HOST_EX + "api.php";
public static final String URL_SIGN_IN = "http://forums.e-hentai.org/index.php?act=Login";
public static final String URL_REGISTER = "http://forums.e-hentai.org/index.php?act=Reg&CODE=00";
public static final String URL_FAVORITES = HOST_EX + "favorites.php";
public static final String URL_FORUMS = "https://forums.e-hentai.org/";
public static String getGalleryDetailUrl(long gid, String token) {
return getGalleryDetailUrl(gid, token, 0, false);
@@ -16,6 +16,7 @@
package com.hippo.ehviewer.client.parser;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.util.JsoupUtils;
@@ -51,7 +52,7 @@ public class FavoritesParser {
int limit;
try {
Document d = Jsoup.parse(body);
Document d = Jsoup.parse(body, EhUrl.HOST_EX);
Element ido = JsoupUtils.getElementByClass(d, "ido");
Elements fps = ido.getElementsByClass("fp");
// Last one is "fp fps"
@@ -0,0 +1,38 @@
/*
* Copyright 2016 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.EhUrl;
import com.hippo.ehviewer.client.exception.ParseException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class ForumsParser {
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);
}
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2016 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.EhUrl;
import com.hippo.ehviewer.client.exception.ParseException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class ProfileParser {
public static Result parse(String body) throws ParseException {
try {
Result result = new Result();
Document d = Jsoup.parse(body, EhUrl.URL_FORUMS);
Element profilename = d.getElementById("profilename");
result.displayName = profilename.child(0).text();
result.avatar = profilename.nextElementSibling().nextElementSibling().child(0).attr("src");
return result;
} catch (Exception e) {
throw new ParseException("Parse forums error", body);
}
}
public static class Result {
public String displayName;
public String avatar;
}
}
@@ -16,6 +16,7 @@
package com.hippo.ehviewer.client.parser;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.util.JsoupUtils;
@@ -34,7 +35,7 @@ public class WhatsHotParser {
public static List<GalleryInfo> parse(String body) throws ParseException {
try {
List<GalleryInfo> galleryInfoList = new ArrayList<>(15);
Document d = Jsoup.parse(body);
Document d = Jsoup.parse(body, EhUrl.HOST_G);
Element pp = d.getElementById("pp");
Elements id1List = pp.getElementsByClass("id1");
for (int i = 0, n = id1List.size(); i < n; i++) {
@@ -17,13 +17,11 @@
package com.hippo.ehviewer.ui.scene;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@@ -33,7 +31,6 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.hippo.app.EditTextDialogBuilder;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
@@ -41,6 +38,7 @@ import com.hippo.ehviewer.UrlOpener;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.parser.ProfileParser;
import com.hippo.ehviewer.ui.MainActivity;
import com.hippo.rippleold.RippleSalon;
import com.hippo.scene.Announcer;
@@ -151,8 +149,7 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
EhApplication application = (EhApplication) getContext().getApplicationContext();
if (application.containGlobalStuff(mRequestId)) {
// request exist
mProgress.setAlpha(1.0f);
mProgress.setVisibility(View.VISIBLE);
showProgress(false);
}
return view;
@@ -163,44 +160,51 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
super.onViewCreated(view, savedInstanceState);
// Show IME
ActivityHelper.showSoftInput(getActivity(), mUsername);
if (mProgress != null && View.INVISIBLE != mProgress.getVisibility()) {
ActivityHelper.showSoftInput(getActivity(), mUsername);
}
}
private void showNoDisplayDialog() {
final EditTextDialogBuilder builder = new EditTextDialogBuilder(getContext(),
"", getString(R.string.display_name));
builder.setTitle(R.string.display_name_dialog_title);
builder.setPositiveButton(android.R.string.ok, null);
builder.setCancelable(false);
final AlertDialog dialog = builder.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = builder.getText().trim();
if (TextUtils.isEmpty(text)) {
builder.setError(getString(R.string.display_name_is_empty));
} else {
builder.setError(null);
dialog.dismiss();
// TODO get display name from web
Settings.putDisplayName(text);
redirectTo();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mProgress = null;
mUsernameLayout = null;
mPasswordLayout = null;
mUsername = null;
mPassword = null;
mRegister = null;
mSignIn = null;
mSignInViaWebView = null;
mSignInViaCookies = null;
mSkipSigningIn = null;
}
private void showProgress(boolean animation) {
if (null != mProgress && View.VISIBLE != mProgress.getVisibility()) {
if (animation) {
mProgress.setAlpha(0.0f);
mProgress.setVisibility(View.VISIBLE);
mProgress.animate().alpha(1.0f).setDuration(500).start();
} else {
mProgress.setAlpha(1.0f);
mProgress.setVisibility(View.VISIBLE);
}
});
}
}
private void hideProgress() {
if (null != mProgress) {
mProgress.setVisibility(View.GONE);
}
}
@Override
protected void onSceneResult(int requestCode, int resultCode, Bundle data) {
if (REQUEST_CODE_WEBVIEW == requestCode) {
if (RESULT_OK == resultCode) {
String displayName;
if (null == data || null == (displayName = data.getString(WebViewLoginScene.KEY_DISPLAY_NAME))) {
showNoDisplayDialog();
} else {
Settings.putDisplayName(displayName);
redirectTo();
}
getProfile();
}
} else {
super.onSceneResult(requestCode, resultCode, data);
@@ -210,7 +214,7 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
@Override
public void onClick(View v) {
if (mRegister == v) {
UrlOpener.openUrl(getActivity(), EhUrl.API_REGISTER, false, true);
UrlOpener.openUrl(getActivity(), EhUrl.URL_REGISTER, false, true);
} else if (mSignIn == v) {
signIn();
} else if (mSignInViaWebView == v) {
@@ -236,7 +240,7 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
private void signIn() {
if (null == mUsername || null == mPassword || null == mUsernameLayout ||
null == mPasswordLayout || null == mProgress) {
null == mPasswordLayout) {
return;
}
@@ -258,10 +262,7 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
}
ActivityHelper.hideSoftInput(getActivity());
mProgress.setAlpha(0.0f);
mProgress.setVisibility(View.VISIBLE);
mProgress.animate().alpha(1.0f).setDuration(500).start();
showProgress(true);
// Clean up for sign in
EhApplication.getEhCookieStore(getContext()).cleanUpForSignIn();
@@ -276,6 +277,19 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
EhApplication.getEhClient(getContext()).execute(request);
}
private void getProfile() {
ActivityHelper.hideSoftInput(getActivity());
showProgress(true);
EhCallback callback = new GetProfileListener(getContext(),
((StageActivity) getActivity()).getStageId(), getTag());
mRequestId = ((EhApplication) getContext().getApplicationContext()).putGlobalStuff(callback);
EhRequest request = new EhRequest()
.setMethod(EhClient.METHOD_GET_PROFILE)
.setCallback(callback);
EhApplication.getEhClient(getContext()).execute(request);
}
private void redirectTo() {
((MainActivity) getActivity()).startSceneForCheckStep(
MainActivity.CHECK_STEP_SIGN_IN, getArguments());
@@ -293,22 +307,28 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
public void onSignInSuccess() {
if (EhApplication.getEhCookieStore(getContext()).hasSignedIn()) {
// Has signed in
redirectTo();
getProfile();
} else {
if (null != mProgress) {
mProgress.setVisibility(View.GONE);
}
hideProgress();
whetherToSkip();
}
}
public void onSignInFailure(Exception e) {
if (null != mProgress) {
mProgress.setVisibility(View.GONE);
}
Toast.makeText(getContext(), ExceptionUtils.getReadableString(getContext(), e),
Toast.LENGTH_SHORT).show();
hideProgress();
whetherToSkip();
}
public void onGetProfileSuccess() {
redirectTo();
}
public void onGetProfileFailure(Exception e) {
Toast.makeText(getContext(), ExceptionUtils.getReadableString(getContext(), e),
Toast.LENGTH_SHORT).show();
hideProgress();
whetherToSkip();
}
@@ -350,4 +370,44 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
return scene instanceof SignInScene;
}
}
private class GetProfileListener extends EhCallback<SignInScene, ProfileParser.Result> {
public GetProfileListener(Context context, int stageId, String sceneTag) {
super(context, stageId, sceneTag);
}
@Override
public void onSuccess(ProfileParser.Result result) {
getApplication().removeGlobalStuff(this);
Settings.putDisplayName(result.displayName);
Settings.putAvatar(result.avatar);
SignInScene scene = getScene();
if (scene != null) {
scene.onGetProfileSuccess();
}
}
@Override
public void onFailure(Exception e) {
getApplication().removeGlobalStuff(this);
e.printStackTrace();
SignInScene scene = getScene();
if (scene != null) {
scene.onGetProfileFailure(e);
}
}
@Override
public void onCancel() {
getApplication().removeGlobalStuff(this);
}
@Override
public boolean isInstance(SceneFragment scene) {
return scene instanceof SignInScene;
}
}
}
@@ -24,14 +24,12 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.client.EhCookieStore;
import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.parser.SignInParser;
import java.util.ArrayList;
import java.util.Collections;
@@ -57,6 +55,7 @@ public class WebViewLoginScene extends BaseScene {
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -64,7 +63,6 @@ public class WebViewLoginScene extends BaseScene {
CookieManager.getInstance().removeAllCookie();
mWebView = new WebView(getContext());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
mWebView.setWebViewClient(new LoginWebViewClient());
mWebView.loadUrl(EhUrl.URL_SIGN_IN);
return mWebView;
@@ -135,24 +133,8 @@ public class WebViewLoginScene extends BaseScene {
}
if (getId && getHash) {
// Get content
view.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
}
}
private class MyJavaScriptInterface {
@JavascriptInterface
public void processHTML(String html) {
try {
String displayName = SignInParser.parse(html);
Bundle bundle = new Bundle();
bundle.putString(KEY_DISPLAY_NAME, displayName);
setResult(RESULT_OK, bundle);
setResult(RESULT_OK, null);
finish();
} catch (Exception e) {
// TODO NO ask username
}
}
}