mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Add] Pixiv 添加一组与 Pixiv 登录会话相关的类;
[Add] PixivLogin, PixivLoginException 初版的登录器接口; [Add] PixivSession, BasicPixivSession Pixiv 帐号登录会话抽象类和一个默认基本实现; [Add] ObjectInputStreamLogin 通过反序列化 CacheStore 进行登录的登录器;
This commit is contained in:
parent
52d015a4ba
commit
2bea395cf7
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2020 LamGC
|
||||
*
|
||||
* ContentGrabbingJi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* ContentGrabbingJi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.lamgc.pixiv;
|
||||
|
||||
import com.google.common.net.HttpHeaders;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 基本的 Pixiv 会话对象.
|
||||
* @author LamGC
|
||||
*/
|
||||
public class BasicPixivSession extends PixivSession {
|
||||
|
||||
private final static String PIXIV_LOGOUT_URL = "https://www.pixiv.net/logout.php";
|
||||
private final HttpClient httpClient;
|
||||
private boolean login = true;
|
||||
|
||||
/**
|
||||
* 构造一个会话对象.
|
||||
* @param cookieStore 会话 Cookies.
|
||||
* @param httpClient 用于请求登出的 HttpClient 对象.
|
||||
*/
|
||||
public BasicPixivSession(CookieStore cookieStore, HttpClient httpClient) {
|
||||
super(cookieStore);
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean logOut() throws IOException {
|
||||
HttpGet request = new HttpGet(PIXIV_LOGOUT_URL);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
getCookies().getCookies().forEach(cookie ->
|
||||
builder.append(cookie.getName()).append("=").append(cookie.getValue()).append("; "));
|
||||
request.addHeader(HttpHeaders.COOKIE, builder.toString());
|
||||
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
final int httpRedirectCode = 302;
|
||||
if (response.getStatusLine().getStatusCode() == httpRedirectCode) {
|
||||
login = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package net.lamgc.pixiv;
|
||||
|
||||
/**
|
||||
* @author LamGC
|
||||
*/
|
||||
public interface PixivLogin {
|
||||
|
||||
/**
|
||||
* 执行登录操作.
|
||||
* @return 返回登录后的会话对象
|
||||
* @see PixivSession
|
||||
* @throws PixivLoginException 当无法完成登录时抛出.
|
||||
*/
|
||||
PixivSession login() throws PixivLoginException;
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package net.lamgc.pixiv;
|
||||
|
||||
/**
|
||||
* Pixiv 登录异常.
|
||||
* @author LamGC
|
||||
*/
|
||||
public class PixivLoginException extends Exception {
|
||||
|
||||
public PixivLoginException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public PixivLoginException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package net.lamgc.pixiv;
|
||||
|
||||
import org.apache.http.client.CookieStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Pixiv 会话.
|
||||
* @author LamGC
|
||||
*/
|
||||
public abstract class PixivSession {
|
||||
|
||||
private final CookieStore cookieStore;
|
||||
|
||||
protected PixivSession(CookieStore cookieStore) {
|
||||
this.cookieStore = Objects.requireNonNull(cookieStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出当前会话.
|
||||
*
|
||||
* <p>登出后该会话 Cookies 将会失效.
|
||||
* @return 如果登出成功, 返回 true.
|
||||
* @throws IOException 当尝试登出发生异常时抛出.
|
||||
*/
|
||||
public abstract boolean logOut() throws IOException;
|
||||
|
||||
/**
|
||||
* 检查是否已登录, 或者说会话是否有效.
|
||||
* @return 如果会话已登出(失效), 返回 false.
|
||||
*/
|
||||
public abstract boolean isLogin();
|
||||
|
||||
/**
|
||||
* 获取 CookieStore 对象.
|
||||
* @return 返回 CookieStore.
|
||||
*/
|
||||
public CookieStore getCookies() {
|
||||
return cookieStore;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2020 LamGC
|
||||
*
|
||||
* ContentGrabbingJi is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* ContentGrabbingJi is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.lamgc.pixiv.login;
|
||||
|
||||
import net.lamgc.pixiv.*;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.HttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* 读入对象进行登录.
|
||||
* @author LamGC
|
||||
*/
|
||||
public class ObjectInputStreamLogin implements PixivLogin {
|
||||
|
||||
private final HttpClient httpClient;
|
||||
private final ObjectInputStream inputStream;
|
||||
private final AtomicReference<PixivSession> sessionCache = new AtomicReference<>();
|
||||
|
||||
public ObjectInputStreamLogin(HttpClient httpClient, InputStream cookieInput) throws IOException {
|
||||
this.httpClient = httpClient;
|
||||
this.inputStream = new ObjectInputStream(cookieInput);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized PixivSession login() throws PixivLoginException {
|
||||
if (sessionCache.get() == null) {
|
||||
CookieStore cookieStore;
|
||||
try {
|
||||
cookieStore = (CookieStore) inputStream.readObject();
|
||||
} catch (Exception e) {
|
||||
throw new PixivLoginException("An exception occurred while trying to read in a Java serialized object", e);
|
||||
}
|
||||
PixivSession session = new BasicPixivSession(cookieStore, this.httpClient);
|
||||
sessionCache.set(session);
|
||||
return session;
|
||||
} else {
|
||||
return sessionCache.get();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user