11package org .usc .check .in .task ;
22
3+ import com .alibaba .fastjson .JSON ;
4+ import com .alibaba .fastjson .JSONArray ;
5+ import com .alibaba .fastjson .JSONObject ;
6+ import io .undertow .util .FileUtils ;
37import org .apache .commons .lang3 .StringUtils ;
4- import org .apache .http .NameValuePair ;
58import org .apache .http .client .ClientProtocolException ;
69import org .apache .http .client .config .CookieSpecs ;
710import org .apache .http .client .config .RequestConfig ;
1013import org .apache .http .impl .client .BasicCookieStore ;
1114import org .apache .http .impl .client .CloseableHttpClient ;
1215import org .apache .http .impl .client .HttpClients ;
13- import org .apache .http .message . BasicNameValuePair ;
16+ import org .apache .http .impl . cookie . BasicClientCookie ;
1417import org .jsoup .Jsoup ;
15- import org .jsoup .nodes .Document ;
1618import org .jsoup .select .Elements ;
1719import org .slf4j .Logger ;
1820import org .slf4j .LoggerFactory ;
1921import org .springframework .boot .context .properties .ConfigurationProperties ;
22+ import org .springframework .core .io .ClassPathResource ;
23+ import org .springframework .core .io .Resource ;
2024import org .springframework .scheduling .annotation .Scheduled ;
2125import org .springframework .stereotype .Component ;
2226import org .usc .check .in .model .Account ;
2327
2428import java .io .IOException ;
2529import java .net .URISyntaxException ;
26- import java .util .ArrayList ;
27- import java .util .List ;
2830
2931/**
3032 * @author Shunli
@@ -44,7 +46,7 @@ public void run() {
4446 try {
4547 RequestConfig config = RequestConfig .custom ().setCookieSpec (CookieSpecs .STANDARD_STRICT ).build ();
4648 CloseableHttpClient client = HttpClients .custom ().setDefaultRequestConfig (config ).build ();
47- Executor executor = Executor .newInstance (client ). use ( new BasicCookieStore ()) ;
49+ Executor executor = Executor .newInstance (client );
4850 if (login (executor , account )) {
4951 checkIn (executor , account );
5052 }
@@ -55,34 +57,68 @@ public void run() {
5557 }
5658 }
5759
58- private boolean login ( Executor executor , Account account ) throws ClientProtocolException , IOException , URISyntaxException {
59- String usrename = account . getUsername ( );
60+ private BasicCookieStore getCookieStore ( String cookieJson ) {
61+ JSONArray jsonArray = JSON . parseArray ( cookieJson );
6062
61- // 1st get once
62- Document checkLoginOnce = Jsoup .parse (executor .execute (appendTimeOuts (Request .Get (LOGIN_URL ))).returnContent ().asString ());
63- String once = checkLoginOnce .getElementsByAttributeValue ("name" , "once" ).attr ("value" );
64- Elements elementsByClass = checkLoginOnce .getElementsByClass ("sl" );
63+ BasicCookieStore cookieStore = new BasicCookieStore ();
64+ for (int i = 0 ; i < jsonArray .size (); i ++) {
65+ JSONObject jsonObject = jsonArray .getJSONObject (i );
66+ String name = jsonObject .getString ("name" );
67+ String value = jsonObject .getString ("value" );
6568
66- List <NameValuePair > formParams = new ArrayList <NameValuePair >();
67- formParams .add (new BasicNameValuePair (elementsByClass .get (0 ).attr ("name" ), usrename ));
68- formParams .add (new BasicNameValuePair (elementsByClass .get (1 ).attr ("name" ), account .getPassword ()));
69- formParams .add (new BasicNameValuePair ("once" , once ));
70- formParams .add (new BasicNameValuePair ("next" , "/" ));
69+ BasicClientCookie cookie = new BasicClientCookie (name , value );
70+ cookie .setDomain (".v2ex.com" );
71+ cookie .setPath ("/" );
7172
72- // login
73- executor . execute ( appendTimeOuts ( Request . Post ( LOGIN_URL )). bodyForm ( formParams ). userAgent ( USER_AGENT ). addHeader ( "Referer" , "http://www/v2ex.com/signin" )). discardContent ();
73+ cookieStore . addCookie ( cookie );
74+ }
7475
75- // checkIn must load first page once
76- String rtn = executor .execute (appendTimeOuts (Request .Get ("http://www.v2ex.com" ))).returnContent ().asString ();
77- if (StringUtils .contains (rtn , "signout" )) {
78- log .info ("【V2EX】【{}】登录成功" , usrename );
79- return true ;
76+ return cookieStore ;
77+ }
78+
79+ private boolean login (Executor executor , Account account ) throws IOException {
80+ String userName = account .getUsername ();
81+
82+ Resource resource = new ClassPathResource ("v2ex-" + userName + "-cookie.json" );
83+ if (!resource .exists ()) {
84+ log .info ("【V2EX】【{}】没有cookie文件" , userName );
85+ return false ;
8086 }
8187
82- log .info ("【V2EX】【{}】登录失败" , usrename );
83- return false ;
88+ executor .use (getCookieStore (FileUtils .readFile (resource .getInputStream ())));
89+
90+ log .info ("【V2EX】【{}】加载cookie文件成功" , userName );
91+ return true ;
8492 }
8593
94+ // private boolean login(Executor executor, Account account) throws ClientProtocolException, IOException, URISyntaxException {
95+ // String usrename = account.getUsername();
96+ //
97+ // // 1st get once
98+ // Document checkLoginOnce = Jsoup.parse(executor.execute(appendTimeOuts(Request.Get(LOGIN_URL))).returnContent().asString());
99+ // String once = checkLoginOnce.getElementsByAttributeValue("name", "once").attr("value");
100+ // Elements elementsByClass = checkLoginOnce.getElementsByClass("sl");
101+ //
102+ // List<NameValuePair> formParams = new ArrayList<NameValuePair>();
103+ // formParams.add(new BasicNameValuePair(elementsByClass.get(0).attr("name"), usrename));
104+ // formParams.add(new BasicNameValuePair(elementsByClass.get(1).attr("name"), account.getPassword()));
105+ // formParams.add(new BasicNameValuePair("once", once));
106+ // formParams.add(new BasicNameValuePair("next", "/"));
107+ //
108+ // // login
109+ // executor.execute(appendTimeOuts(Request.Post(LOGIN_URL)).bodyForm(formParams).userAgent(USER_AGENT).addHeader("Referer", "http://www/v2ex.com/signin")).discardContent();
110+ //
111+ // // checkIn must load first page once
112+ // String rtn = executor.execute(appendTimeOuts(Request.Get("http://www.v2ex.com"))).returnContent().asString();
113+ // if(StringUtils.contains(rtn, "signout")) {
114+ // log.info("【V2EX】【{}】登录成功", usrename);
115+ // return true;
116+ // }
117+ //
118+ // log.info("【V2EX】【{}】登录失败", usrename);
119+ // return false;
120+ // }
121+
86122 private boolean checkIn (Executor executor , Account account ) throws ClientProtocolException , IOException , URISyntaxException , InterruptedException {
87123 String usrename = account .getUsername ();
88124
0 commit comments