1111import java .util .List ;
1212import java .util .Map ;
1313import java .util .Map .Entry ;
14- import java .util .concurrent .BlockingQueue ;
1514
1615import org .apache .http .Header ;
1716import org .apache .http .HeaderIterator ;
2928import org .apache .http .message .BasicNameValuePair ;
3029import org .apache .http .util .EntityUtils ;
3130
32- import container .URLContainer ;
3331import us .codecraft .webmagic .selector .XpathSelector ;
3432
35- public class HttpClientDownloader extends URLContainer implements Downloader {
33+ public class HttpClientDownloader implements Downloader {
34+ private static final String ALGORITHMS = "https://www.leetcode.com/problemset/algorithms/" ;
3635 private static final String INDEX_URL = "https://leetcode.com" ; //首页的url地址
3736 private static final String LOGIN_URL = "https://leetcode.com/accounts/login/" ; //登录页面url地址
3837 private static final String PASSWORD = "thd04180015" ; //登录密码
@@ -51,16 +50,12 @@ public class HttpClientDownloader extends URLContainer implements Downloader {
5150 private static final String problemNamePath = "//div[@class='col-md-12']/h4/a/text()" ; //题目名称Xpath路径
5251 private static final String codePath = "//div[@class='ace_content']/text()" ; //得到代码Xpath路径
5352 private static CloseableHttpClient httpClient ;
54- /**
55- *
56- * 创建一个新的实例 HttpClientDownloader.
57- *
58- */
59- public HttpClientDownloader () {
60- init ();
61- }
53+ private static final HttpClientDownloader downloader = new HttpClientDownloader ();
6254
63- public void init () {
55+ public static HttpClientDownloader getInstance () {
56+ return downloader ;
57+ }
58+ public static void init (String username , String password ) {
6459 httpClient = HttpClients .createDefault ();
6560 HttpGet httpGet = new HttpGet (INDEX_URL );
6661 CloseableHttpResponse response1 ;
@@ -87,8 +82,8 @@ public void init() {
8782 httpPost .addHeader ("Referer" , LOGIN_URL );
8883 httpPost .addHeader ("Origin" , INDEX_URL );
8984 Map <String , String > map = new HashMap <String , String >();
90- map .put ("login" , USER_NAME );
91- map .put ("password" , PASSWORD );
85+ map .put ("login" , username );
86+ map .put ("password" , password );
9287 map .put ("csrfmiddlewaretoken" , cookieString );
9388 map .put ("remember" , "on" );
9489 try {
@@ -196,20 +191,21 @@ public static String getCookie(HttpResponse httpResponse) {
196191 return null ;
197192 }
198193
199- public void problemListDownloader (String url ) {
200- doDispatcher (url , problemQueue );
194+ public List < String > problemListDownloader () {
195+ return doDispatcher (ALGORITHMS );
201196 }
202197
203- public void problemDescriptionDownloader (String url ) {
204- doDispatcher (url , problemSubmission );
198+ public List < String > problemDescriptionDownloader (String url ) {
199+ return doDispatcher (url );
205200 }
206201
207- public void submissionListDownloader (String url , String name ) {
208- doDispatcher (url , problemCodePage , name );
202+ public List < String > submissionListDownloader (String url , String name ) {
203+ return doDispatcher (url , name , httpClient );
209204 }
210205
211- public void codePageDownloader (String url ) {
206+ public List < String > codePageDownloader (String url ) {
212207 //爬取代码
208+ return null ;
213209 }
214210
215211 public String getHtml (HttpResponse response ) {
@@ -247,26 +243,23 @@ public String getHtml(HttpResponse response) {
247243 * @exception
248244 * @since 1.0.0
249245 */
250- public void doDispatcher (String url , BlockingQueue < String > blockingQueue ) {
246+ public List < String > doDispatcher (String url ) {
251247 HttpGet httpGet1 = new HttpGet (url );
252248 try {
253249 HttpResponse response1 = httpClient .execute (httpGet1 );
254250 printResponse (response1 );
255251
256252 XpathSelector xpathSelector = new XpathSelector (problemLinkPath );
257253 List <String > listTmp = xpathSelector .selectList (getHtml (response1 ));
258- for (String string : listTmp ) {
259- blockingQueue .put (string );
260- }
261254 HttpEntity entity1 = response1 .getEntity ();
262255 EntityUtils .consume (entity1 );
256+ return listTmp ;
263257 } catch (ClientProtocolException e ) {
264258 System .out .println (e .getMessage ());
265259 } catch (IOException e ) {
266260 System .out .println (e .getMessage ());
267- } catch (InterruptedException e ) {
268- System .out .println (e .getMessage ());
269261 }
262+ return null ;
270263 }
271264 /**
272265 *
@@ -279,25 +272,22 @@ public void doDispatcher(String url, BlockingQueue<String> blockingQueue) {
279272 * @exception
280273 * @since 1.0.0
281274 */
282- public void doDispatcher (String url , BlockingQueue < String > blockingQueue , String name ) {
275+ public List < String > doDispatcher (String url , String name , CloseableHttpClient httpClient ) {
283276 HttpGet httpGet1 = new HttpGet (url );
284277 try {
285278 HttpResponse response1 = httpClient .execute (httpGet1 );
286279 printResponse (response1 );
287280
288281 XpathSelector xpathSelector = new XpathSelector (problemLinkPath );
289282 List <String > listTmp = xpathSelector .selectList (getHtml (response1 ));
290- for (String string : listTmp ) {
291- blockingQueue .put (string );
292- }
293283 HttpEntity entity1 = response1 .getEntity ();
294284 EntityUtils .consume (entity1 );
285+ return listTmp ;
295286 } catch (ClientProtocolException e ) {
296287 System .out .println (e .getMessage ());
297288 } catch (IOException e ) {
298289 System .out .println (e .getMessage ());
299- } catch (InterruptedException e ) {
300- System .out .println (e .getMessage ());
301290 }
291+ return null ;
302292 }
303293}
0 commit comments