Новый кинотеатр
This commit is contained in:
@@ -5,9 +5,12 @@
|
||||
package kinosearch.kinosearch3.browser;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpMessage;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -37,7 +40,26 @@ public class ApacheBrowser implements Browser {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void setup_headers(HttpGet request) {
|
||||
@Override
|
||||
public String post(String url, String data) {
|
||||
HttpClient client = HttpClients.createDefault();
|
||||
HttpPost request = new HttpPost(url);
|
||||
setup_headers(request);
|
||||
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
String result = "";
|
||||
try {
|
||||
request.setEntity(new StringEntity(data));
|
||||
HttpResponse response = client.execute(request);
|
||||
result = IOUtils.toString(response.getEntity().getContent(), encoding);
|
||||
} catch (IOException ignore) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void setup_headers(HttpMessage request) {
|
||||
request.addHeader("Connection", "close");
|
||||
request.addHeader("Accept-Encoding", "deflate");
|
||||
request.addHeader("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36");
|
||||
|
||||
@@ -7,4 +7,5 @@ package kinosearch.kinosearch3.browser;
|
||||
public interface Browser {
|
||||
void setEncoding(String encoding);
|
||||
String get(String url);
|
||||
String post(String url, String data);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,11 @@ public class ScannerImpl implements ScannerCinema {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "OnlineLife";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int lastPage = getLastPage();
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2017-04-02
|
||||
*/
|
||||
package kinosearch.kinosearch3.cinema.seasonvar;
|
||||
|
||||
import kinosearch.kinosearch3.browser.Browser;
|
||||
import kinosearch.kinosearch3.spider.FileDownloader;
|
||||
import kinosearch.kinosearch3.spider.ScannerCinema;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ScannerImpl implements ScannerCinema {
|
||||
private Logger logger = LoggerFactory.getLogger(ScannerImpl.class);
|
||||
private final FileDownloader fileDownloader;
|
||||
private final Browser browser;
|
||||
private final File saveTo;
|
||||
private int i = 1;
|
||||
|
||||
public ScannerImpl(FileDownloader fileDownloader, Browser browser, File saveTo) {
|
||||
this.fileDownloader = fileDownloader;
|
||||
this.browser = browser;
|
||||
this.saveTo = saveTo;
|
||||
if (!this.saveTo.mkdirs() && !this.saveTo.exists()) {
|
||||
throw new IllegalStateException(String.format("dir not found: '%s'", this.saveTo.getAbsolutePath()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Seasonvar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String html = browser.get("http://seasonvar.ru/");
|
||||
Document document = Jsoup.parse(html);
|
||||
Elements elements = document.getElementsByClass("betterT");
|
||||
|
||||
elements.stream()
|
||||
.filter(element -> element.tagName().equals("div"))
|
||||
.map(element -> element.children().get(0))
|
||||
.forEach(element -> {
|
||||
logger.info("element #{}", i++);
|
||||
String str = element.attr("href");
|
||||
str = str.substring(1, str.lastIndexOf("."));
|
||||
|
||||
String str2 = element.attr("data");
|
||||
str2 = browser.get("http://seasonvar.ru"+str2);
|
||||
Document doc = Jsoup.parse(str2);
|
||||
str2 = doc.getElementsByTag("img").get(0).attr("src");
|
||||
|
||||
this.fileDownloader.addFile(str2, new File(this.saveTo, str+".jpg"));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,6 @@
|
||||
package kinosearch.kinosearch3.spider;
|
||||
|
||||
public interface ScannerCinema {
|
||||
String getName();
|
||||
void run();
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ public class Spider {
|
||||
|
||||
void start() {
|
||||
fileDownloader.start();
|
||||
scanners.forEach(ScannerCinema::run);
|
||||
for (ScannerCinema scanner : scanners) {
|
||||
(new Thread(scanner::run, "Scanner " + scanner.getName())).start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,17 @@
|
||||
<constructor-arg name="saveToDir" value="file:R:/onlinelife"/>
|
||||
</bean>
|
||||
|
||||
<bean id="seasonvarScanner" class="kinosearch.kinosearch3.cinema.seasonvar.ScannerImpl">
|
||||
<constructor-arg name="fileDownloader" ref="fileDownloader"/>
|
||||
<constructor-arg name="browser" ref="browser"/>
|
||||
<constructor-arg name="saveTo" value="file:R:/seasonvar"/>
|
||||
</bean>
|
||||
|
||||
<bean id="spider" class="kinosearch.kinosearch3.spider.Spider">
|
||||
<constructor-arg name="scanners">
|
||||
<list>
|
||||
<ref bean="onlinelifeScanner"/>
|
||||
<ref bean="seasonvarScanner"/>
|
||||
</list>
|
||||
</constructor-arg>
|
||||
<constructor-arg name="fileDownloader" ref="fileDownloader"/>
|
||||
|
||||
Reference in New Issue
Block a user