First
This commit is contained in:
@@ -13,4 +13,13 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
compile 'org.jsoup:jsoup:1.13.1'
|
||||
compile 'com.google.code.gson:gson:2.7'
|
||||
compile 'org.apache.httpcomponents:httpclient:4.5.12'
|
||||
compile group: 'org.seleniumhq.selenium', name: 'selenium-java',
|
||||
version: '3.141.59'
|
||||
compile 'org.mongodb:mongo-java-driver:2.12.3'
|
||||
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.11.0-beta4'
|
||||
compile group: 'org.apache.poi', name: 'poi', version: '3.9'
|
||||
compile group: 'org.jdom', name: 'jdom', version: '2.0.0'
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
#Fri Nov 27 10:21:50 CET 2020
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
|
||||
public class CorporatePatentClassificationCrawler {
|
||||
|
||||
private WebDriver driver;
|
||||
|
||||
public CorporatePatentClassificationCrawler() {
|
||||
System.setProperty("webdriver.chrome.driver", Settings.PATHTOCROMEDRIVER.getContent());
|
||||
driver = new ChromeDriver();
|
||||
driver.navigate().to("https://worldwide.espacenet.com/classification");
|
||||
search();
|
||||
}
|
||||
|
||||
private synchronized void search() {
|
||||
String toSearch = Settings.USPTOCATEGORY.getContent();
|
||||
WebElement searchBox = driver.findElement(By.name("key"));
|
||||
searchBox.sendKeys(toSearch);
|
||||
driver.findElement(By.cssSelector("button[class*=cpcbrowser-btn-search]")).click();
|
||||
|
||||
try {
|
||||
wait(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String text = driver.findElement(By.cssSelector("span[class=title]")).getText();
|
||||
if (text.equals(toSearch)) {
|
||||
String classification = driver.findElement(By.cssSelector("a[class*=symbol")).getText();
|
||||
CorporatePatentClassificationImporter.importToDB(classification, text);
|
||||
}
|
||||
|
||||
this.tearDown();
|
||||
|
||||
}
|
||||
|
||||
public void tearDown() {
|
||||
if (driver != null) {
|
||||
driver.close();
|
||||
}
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import com.mongodb.*;
|
||||
|
||||
public class CorporatePatentClassificationImporter {
|
||||
|
||||
public static void importToDB(String classification, String text) {
|
||||
String path = Settings.PATHTODOWNLOADPATENTS.getContent();
|
||||
MongoClient mongo = new MongoClient(new MongoClientURI(Settings.MONGOURL.getContent()));
|
||||
DB mongoDB = mongo.getDB(Settings.DBCPC.getContent());
|
||||
DBCollection collection;
|
||||
if (mongoDB.getCollectionNames().contains(Settings.COLLECTIONCPC.getContent())) {
|
||||
collection = mongoDB.getCollection(Settings.COLLECTIONCPC.getContent());
|
||||
} else {
|
||||
collection = mongoDB.getCollection(Settings.COLLECTIONCPC.getContent());
|
||||
collection.createIndex(new BasicDBObject("Classification", 1), "Classification", true);
|
||||
}
|
||||
|
||||
BasicDBObject obj = new BasicDBObject();
|
||||
obj.put("Classification", classification);
|
||||
obj.put("Text", text);
|
||||
|
||||
if (collection.findOne(new BasicDBObject("Classification", classification)) == null) {
|
||||
collection.insert(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//Crawler
|
||||
startUSPTOCrawler();
|
||||
startWOSCrawler();
|
||||
getCPC();
|
||||
|
||||
//Importer
|
||||
startUSPTOImport();
|
||||
startWOSImport();
|
||||
}
|
||||
|
||||
private static void getCPC() {
|
||||
if (Settings.GETCPCMAPPING.getContent().equals("YES")) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
new CorporatePatentClassificationCrawler();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
try {
|
||||
thread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void startUSPTOCrawler() {
|
||||
if (Settings.USPTSTARTDOWNLAOD.getContent().equals("YES")) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
new PatentCrawler(1976, 2000);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
try {
|
||||
thread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void startUSPTOImport() {
|
||||
if (Settings.USPTSTARTIMPORT.getContent().equals("YES")) {
|
||||
new PatentImporter(2019, 2019);
|
||||
}
|
||||
}
|
||||
|
||||
private static void startWOSCrawler() {
|
||||
if (Settings.WOSSTARTDOWNLOAD.getContent().equals("YES")) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
WoSCrawler crawler = new WoSCrawler("\"" + Settings.WOSCATEGORY.getContent() + "\"", SearchMode.TOPIC, 0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
try {
|
||||
thread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void startWOSImport() {
|
||||
if (Settings.WOSSTARTIMPORT.getContent().equals("YES")) {
|
||||
try {
|
||||
WOSImporter importer = new WOSImporter();
|
||||
importer.doImport();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
import org.openqa.selenium.remote.CapabilityType;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Calendar;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PatentCrawler {
|
||||
|
||||
int currentYear;
|
||||
private WebDriver driver;
|
||||
|
||||
public PatentCrawler(int startYear, int endYear) {
|
||||
if (startYear < 1976) {
|
||||
currentYear = 1976;
|
||||
}
|
||||
currentYear = startYear;
|
||||
setUpDownloadFolder();
|
||||
driver.navigate().to("https://bulkdata.uspto.gov/data/patent/grant/redbook/fulltext/" + String.valueOf(startYear) + "/");
|
||||
int todaysYear = Calendar.getInstance().get(Calendar.YEAR);
|
||||
if (endYear < todaysYear) {
|
||||
todaysYear = endYear;
|
||||
}
|
||||
while (currentYear <= todaysYear) {
|
||||
dowloadYear();
|
||||
moveToYearFoleder(currentYear);
|
||||
goToNextYear();
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpDownloadFolder() {
|
||||
System.setProperty("webdriver.chrome.driver", Settings.PATHTOCROMEDRIVER.getContent());
|
||||
String downloadFilepath = Settings.PATHTODOWNLOADPATENTS.getContent();
|
||||
Map<String, Object> preferences = new Hashtable<String, Object>();
|
||||
preferences.put("profile.default_content_settings.popups", 0);
|
||||
preferences.put("download.prompt_for_download", "false");
|
||||
preferences.put("download.default_directory", downloadFilepath);
|
||||
|
||||
// disable flash and the PDF viewer
|
||||
preferences.put("plugins.plugins_disabled", new String[]{
|
||||
"Adobe Flash Player", "Chrome PDF Viewer"});
|
||||
|
||||
ChromeOptions options = new ChromeOptions();
|
||||
options.setExperimentalOption("prefs", preferences);
|
||||
|
||||
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
|
||||
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
|
||||
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
|
||||
driver = new ChromeDriver(capabilities);
|
||||
}
|
||||
|
||||
private synchronized void dowloadYear() {
|
||||
List<WebElement> elementList = driver.findElements(By.cssSelector("a[href*=zip]"));
|
||||
for (WebElement e : elementList) {
|
||||
e.click();
|
||||
try {
|
||||
wait(30000);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void goToNextYear() {
|
||||
currentYear++;
|
||||
System.out.println(currentYear);
|
||||
driver.navigate().to("https://bulkdata.uspto.gov/data/patent/grant/redbook/fulltext/" + String.valueOf(currentYear) + "/");
|
||||
}
|
||||
|
||||
private void moveToYearFoleder(int year) {
|
||||
File file = new File(Settings.PATHTODOWNLOADPATENTS.getContent());
|
||||
File[] allFiles = file.listFiles();
|
||||
new File(Settings.PATHTODOWNLOADPATENTS.getContent() + "\\" + year).mkdir();
|
||||
for (int i = 0; i < allFiles.length; i++) {
|
||||
if (allFiles[i].getName().contains("zip")) {
|
||||
allFiles[i].renameTo(new File(Settings.PATHTODOWNLOADPATENTS.getContent() + "\\" + year + "\\" + allFiles[i].getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
import com.mongodb.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class PatentImporter {
|
||||
|
||||
String path;
|
||||
DBCollection collection;
|
||||
private MongoClient mongo;
|
||||
private DB mongoDB;
|
||||
|
||||
public PatentImporter(int startYear, int endYear) {
|
||||
path = Settings.PATHTODOWNLOADPATENTS.getContent();
|
||||
mongo = new MongoClient(new MongoClientURI(Settings.MONGOURL.getContent()));
|
||||
mongoDB = mongo.getDB(Settings.DBPATENT.getContent());
|
||||
if (mongoDB.getCollectionNames().contains(Settings.COLLECTIONPATENT.getContent())) {
|
||||
collection = mongoDB.getCollection(Settings.COLLECTIONPATENT.getContent());
|
||||
} else {
|
||||
collection = mongoDB.getCollection(Settings.COLLECTIONPATENT.getContent());
|
||||
//collection.createIndex(new BasicDBObject("DOI",1),"DOI",true);
|
||||
}
|
||||
importYear(startYear, endYear);
|
||||
}
|
||||
|
||||
public void importYear(int startYear, int endYear) {
|
||||
int todaysYear = Calendar.getInstance().get(Calendar.YEAR);
|
||||
if (endYear < todaysYear) {
|
||||
endYear = todaysYear;
|
||||
}
|
||||
if (startYear < 1976) {
|
||||
startYear = 1976;
|
||||
}
|
||||
|
||||
while (startYear <= endYear) {
|
||||
importYear(startYear);
|
||||
startYear++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void importYear(int year) {
|
||||
unzip(year);
|
||||
saveTXTToDB(year);
|
||||
}
|
||||
|
||||
private void saveTXTToDB(int year) {
|
||||
File directory = new File(Settings.PATHTODOWNLOADPATENTS.getContent() + "\\" + year);
|
||||
File[] files = directory.listFiles();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
String name = files[i].getName();
|
||||
if (name.contains("zip")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (name.contains("txt")) {
|
||||
saveOlderTTX(files[i]);
|
||||
}
|
||||
|
||||
if (name.contains("xml")) {
|
||||
saveNewerXML(files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void saveNewerXML(File file) {
|
||||
|
||||
boolean prevLineAdress = false;
|
||||
boolean documentId = false;
|
||||
boolean citedByExaminer = false;
|
||||
BasicDBObject patent = null;
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
|
||||
//Neues Patent
|
||||
if (line.contains("<us-patent-grant")) {
|
||||
if (patent != null) {
|
||||
collection.insert(patent);
|
||||
}
|
||||
patent = new BasicDBObject();
|
||||
}
|
||||
|
||||
//Titel
|
||||
if (line.contains("<claim-text>")) {
|
||||
int start = line.indexOf('>') + 1;
|
||||
int end = line.indexOf("</");
|
||||
String title = null;
|
||||
if (end == -1) {
|
||||
|
||||
title = line.substring(start);
|
||||
} else {
|
||||
title = line.substring(start, end);
|
||||
|
||||
}
|
||||
String titleAlreadyExisting = "";
|
||||
|
||||
if (patent.getString("Text") == null) {
|
||||
|
||||
} else {
|
||||
titleAlreadyExisting = patent.getString("Text");
|
||||
}
|
||||
patent.append("Text", titleAlreadyExisting + " " + title);
|
||||
}
|
||||
|
||||
|
||||
if (line.contains("<category>cited by examiner</category>")) {
|
||||
citedByExaminer = true;
|
||||
}
|
||||
|
||||
//Classifiaction
|
||||
if (line.contains("<classification-cpc-text>")) {
|
||||
if (citedByExaminer == true) {
|
||||
citedByExaminer = false;
|
||||
continue;
|
||||
}
|
||||
String classification = line.substring(line.indexOf('>') + 1, line.indexOf("</"));
|
||||
String classificationAlreadyExisting = "";
|
||||
if (patent.getString("Classification") == null) {
|
||||
|
||||
} else {
|
||||
classificationAlreadyExisting = patent.getString("Classification");
|
||||
}
|
||||
patent.append("Classification", classificationAlreadyExisting + " ; " + classification);
|
||||
}
|
||||
|
||||
if (line.contains("<document-id>")) {
|
||||
documentId = true;
|
||||
}
|
||||
|
||||
if (line.contains("</documentId>")) {
|
||||
documentId = false;
|
||||
}
|
||||
|
||||
//Application Filing Year
|
||||
if (line.contains("<date>") && documentId == true) {
|
||||
String year = line.substring(6, 10);
|
||||
if (patent.getString("Year") != null) {
|
||||
continue;
|
||||
}
|
||||
patent.put("Year", year);
|
||||
}
|
||||
|
||||
//Country
|
||||
if (line.contains("<country>") && documentId == true) {
|
||||
String country = line.substring(9, line.lastIndexOf("</"));
|
||||
if (patent.getString("Country") != null) {
|
||||
continue;
|
||||
}
|
||||
patent.put("Country", country);
|
||||
}
|
||||
}
|
||||
collection.insert(patent);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (Settings.DELETEFILEAFTEIMPORT.getContent().equals("YES")) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void saveOlderTTX(File file) {
|
||||
BasicDBObject patent = null;
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
|
||||
//Neues Patent
|
||||
if (line.contains("PATN")) {
|
||||
if (patent != null) {
|
||||
collection.insert(patent);
|
||||
}
|
||||
patent = new BasicDBObject();
|
||||
}
|
||||
|
||||
//Titel
|
||||
if (line.contains("TTL")) {
|
||||
String title = line.substring(5);
|
||||
patent.put("Text", title);
|
||||
}
|
||||
|
||||
//City
|
||||
if (line.contains("CTY")) {
|
||||
String city = line.substring(5);
|
||||
patent.put("City", city);
|
||||
}
|
||||
|
||||
//Country
|
||||
if (line.contains("CNT")) {
|
||||
String city = line.substring(5);
|
||||
patent.put("Country", city);
|
||||
}
|
||||
|
||||
//City
|
||||
if (line.contains("ICL")) {
|
||||
String classification = line.substring(5);
|
||||
patent.put("Classification", classification);
|
||||
}
|
||||
|
||||
//Application Filing Year
|
||||
if (line.contains("APD")) {
|
||||
String year = line.substring(5, 9);
|
||||
patent.put("Year", year);
|
||||
}
|
||||
}
|
||||
collection.insert(patent);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (Settings.DELETEFILEAFTEIMPORT.getContent().equals("YES")) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void unzip(int year) {
|
||||
UnzipUtility.unzipYear(year);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
public enum SearchMode {
|
||||
|
||||
|
||||
KEYWORD(1),
|
||||
RESEARCHAREA(2),
|
||||
TOPIC(3),
|
||||
|
||||
;
|
||||
|
||||
|
||||
private int content;
|
||||
|
||||
SearchMode(int content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public int getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
public enum Settings {
|
||||
|
||||
|
||||
MONGOURL("mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false"),
|
||||
|
||||
//Download here: https://chromedriver.chromium.org/downloads
|
||||
PATHTOCROMEDRIVER("C:\\Users\\teddy\\Desktop\\Crawler_WebOfScience\\src\\test\\resources\\chromedriver.exe"),
|
||||
|
||||
|
||||
//Search Category
|
||||
WOSCATEGORY("Computer systems based on specific mathematical models"),
|
||||
USPTOCATEGORY("Administration; Management"),
|
||||
GETCPCMAPPING("YES"),
|
||||
DBCPC("CPC"),
|
||||
COLLECTIONCPC("CPC"),
|
||||
|
||||
|
||||
//WOS
|
||||
//Starts the crawler
|
||||
WOSSTARTDOWNLOAD("NO"),
|
||||
WOSSTARTIMPORT("NO"),
|
||||
USERNAME("teddybond1@gmail.com"),
|
||||
PASSWORD("K7.QW&34"),
|
||||
DBDOCUMENT("Document"),
|
||||
COLLECTIONDOCUMENT("Document"),
|
||||
PATHTOWOSFILES("C:\\Users\\teddy\\Desktop\\files"),
|
||||
|
||||
//USPD
|
||||
PATHTODOWNLOADPATENTS("D:\\Patente"),
|
||||
//NO or YES
|
||||
//StartsDownload
|
||||
USPTSTARTDOWNLAOD("NO"),
|
||||
//Starts import
|
||||
USPTSTARTIMPORT("NO"),
|
||||
//Automatically imports dowloaded Files while downlaoding
|
||||
AUTOMATEDIMPORT("NO"),
|
||||
//Deletes ZipFile after import
|
||||
DELETEZIPAFTERIMPORT("NO"),
|
||||
//Deletes TxtFile after import
|
||||
DELETEFILEAFTEIMPORT("NO"),
|
||||
|
||||
|
||||
DBPATENT("Patent"),
|
||||
COLLECTIONPATENT("Patent"),
|
||||
|
||||
|
||||
;
|
||||
|
||||
|
||||
private String content;
|
||||
|
||||
Settings(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
|
||||
public class UnzipUtility {
|
||||
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
|
||||
public static void unzipYear(int year) {
|
||||
File folder = new File(Settings.PATHTODOWNLOADPATENTS.getContent() + "\\" + year);
|
||||
File[] files = folder.listFiles();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
try {
|
||||
if (files[i].getName().contains(".zip") && checkIfAlreadyUnzipped(folder, files[i]) == false) {
|
||||
unzip(files[i].getPath(), folder.getPath());
|
||||
if (Settings.DELETEZIPAFTERIMPORT.getContent().equals("YES")) {
|
||||
files[i].delete();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean checkIfAlreadyUnzipped(File direcotry, File zipFile) {
|
||||
File file = new File(zipFile.getAbsolutePath().replace("zip", "xml"));
|
||||
boolean newer = Arrays.asList(direcotry.listFiles()).contains(file);
|
||||
file = new File(zipFile.getAbsolutePath().replace("zip", "txt"));
|
||||
boolean older = Arrays.asList(direcotry.listFiles()).contains(file);
|
||||
return newer | older;
|
||||
}
|
||||
|
||||
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
|
||||
File destDir = new File(destDirectory);
|
||||
if (!destDir.exists()) {
|
||||
destDir.mkdir();
|
||||
}
|
||||
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
|
||||
ZipEntry entry = zipIn.getNextEntry();
|
||||
// iterates over entries in the zip file
|
||||
while (entry != null) {
|
||||
String filePath = destDirectory + File.separator + entry.getName();
|
||||
if (!entry.isDirectory()) {
|
||||
// if the entry is a file, extracts it
|
||||
extractFile(zipIn, filePath);
|
||||
} else {
|
||||
// if the entry is a directory, make the directory
|
||||
File dir = new File(filePath);
|
||||
dir.mkdirs();
|
||||
}
|
||||
zipIn.closeEntry();
|
||||
entry = zipIn.getNextEntry();
|
||||
}
|
||||
zipIn.close();
|
||||
}
|
||||
|
||||
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
while ((read = zipIn.read(bytesIn)) != -1) {
|
||||
bos.write(bytesIn, 0, read);
|
||||
}
|
||||
bos.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import com.mongodb.*;
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class WOSImporter {
|
||||
|
||||
String path;
|
||||
DBCollection collection;
|
||||
private MongoClient mongo;
|
||||
private DB mongoDB;
|
||||
|
||||
public WOSImporter() {
|
||||
path = Settings.PATHTOWOSFILES.getContent();
|
||||
mongo = new MongoClient(new MongoClientURI(Settings.MONGOURL.getContent()));
|
||||
mongoDB = mongo.getDB(Settings.DBDOCUMENT.getContent());
|
||||
if (mongoDB.getCollectionNames().contains(Settings.COLLECTIONDOCUMENT.getContent())) {
|
||||
collection = mongoDB.getCollection(Settings.COLLECTIONDOCUMENT.getContent());
|
||||
} else {
|
||||
collection = mongoDB.getCollection(Settings.COLLECTIONDOCUMENT.getContent());
|
||||
collection.createIndex(new BasicDBObject("DOI", 1), "DOI", true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void doImport() throws IOException {
|
||||
File directory = new File(Settings.PATHTOWOSFILES.getContent());
|
||||
String[] array = directory.list();
|
||||
|
||||
int duplicates = 0;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
File file = new File(Settings.PATHTOWOSFILES.getContent() + "\\" + array[i]);
|
||||
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
|
||||
HSSFWorkbook wb = new HSSFWorkbook(fs);
|
||||
HSSFSheet sheet = wb.getSheetAt(0);
|
||||
HSSFRow row;
|
||||
int rows = sheet.getPhysicalNumberOfRows();
|
||||
|
||||
|
||||
for (int r = 1; r < rows; r++) {
|
||||
row = sheet.getRow(r);
|
||||
if (row != null) {
|
||||
|
||||
BasicDBObject document = new BasicDBObject();
|
||||
|
||||
String author = row.getCell(5).getStringCellValue();
|
||||
document.put("Author", author);
|
||||
|
||||
String title = row.getCell(8).getStringCellValue();
|
||||
document.put("Title", title);
|
||||
|
||||
String sourceTitle = row.getCell(9).getStringCellValue();
|
||||
document.put("SourceTitle", sourceTitle);
|
||||
|
||||
String documentType = row.getCell(13).getStringCellValue();
|
||||
document.put("DocumentType", documentType);
|
||||
|
||||
if (row.getCell(19).getCellType() == 0) {
|
||||
String authorKeywords = String.valueOf(row.getCell(19).getNumericCellValue());
|
||||
document.put("AuthorKeyword", authorKeywords);
|
||||
} else {
|
||||
String authorKeywords = row.getCell(19).getStringCellValue();
|
||||
document.put("AuthorKeyword", authorKeywords);
|
||||
}
|
||||
|
||||
String keywords = row.getCell(20).getStringCellValue();
|
||||
document.put("Keywords", keywords);
|
||||
|
||||
String abstrac = row.getCell(21).getStringCellValue();
|
||||
document.put("Abstract", abstrac);
|
||||
|
||||
if (row.getCell(27).getCellType() == 0) {
|
||||
String fundingOrg = String.valueOf(row.getCell(27).getNumericCellValue());
|
||||
document.put("FundingOrg", fundingOrg);
|
||||
} else {
|
||||
String fundingOrg = row.getCell(27).getStringCellValue();
|
||||
document.put("FundingOrg", fundingOrg);
|
||||
}
|
||||
|
||||
if (row.getCell(28).getCellType() == 0) {
|
||||
String fundingText = String.valueOf(row.getCell(28).getNumericCellValue());
|
||||
document.put("FundingText", fundingText);
|
||||
} else {
|
||||
String fundingText = row.getCell(28).getStringCellValue();
|
||||
document.put("FundingText", fundingText);
|
||||
}
|
||||
|
||||
|
||||
double citedReferenceCount = row.getCell(30).getNumericCellValue();
|
||||
document.put("CitedReferenceCount", citedReferenceCount);
|
||||
|
||||
double citedTimes = row.getCell(31).getNumericCellValue();
|
||||
document.put("CitedTimes", citedTimes);
|
||||
|
||||
double OneHundredEightyUsageCount = row.getCell(32).getNumericCellValue();
|
||||
document.put("OneHundredEightyUsageCount", OneHundredEightyUsageCount);
|
||||
|
||||
double since2013UsageCount = row.getCell(33).getNumericCellValue();
|
||||
document.put("Since2013UsageCount", since2013UsageCount);
|
||||
|
||||
String publisher = row.getCell(35).getStringCellValue();
|
||||
document.put("Publisher", publisher);
|
||||
|
||||
String publisherCity = row.getCell(36).getStringCellValue();
|
||||
document.put("PublisherCity", publisherCity);
|
||||
|
||||
String publicationDate = row.getCell(43).getStringCellValue();
|
||||
document.put("PublicationDate", publicationDate);
|
||||
|
||||
double publicationYear = 0;
|
||||
int type = row.getCell(44).getCellType();
|
||||
if (type == 0) //Wenn vorhanden
|
||||
{
|
||||
publicationYear = row.getCell(44).getNumericCellValue();
|
||||
document.put("PublicationYear", publicationYear);
|
||||
}
|
||||
//String volume = row.getCell(44).getStringCellValue();
|
||||
double numberOfPages = row.getCell(57).getNumericCellValue();
|
||||
document.put("NumberOfPages", numberOfPages);
|
||||
|
||||
String wosCategories = row.getCell(58).getStringCellValue();
|
||||
document.put("WosCategories", wosCategories);
|
||||
|
||||
String researchAreas = row.getCell(59).getStringCellValue();
|
||||
document.put("ResearchAreas", researchAreas);
|
||||
|
||||
String doi = row.getCell(54).getStringCellValue();
|
||||
document.put("DOI", doi);
|
||||
|
||||
if (collection.findOne(new BasicDBObject("DOI", doi)) == null) {
|
||||
collection.insert(document);
|
||||
} else {
|
||||
duplicates++;
|
||||
System.out.println("Duplikat: " + title + ";;;" + author + ";;;" + doi + ";;;" + String.valueOf(duplicates));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class WoSCrawler {
|
||||
|
||||
private WebDriver driver;
|
||||
private String searchPage;
|
||||
private String currentSearch;
|
||||
private int start;
|
||||
|
||||
|
||||
public WoSCrawler(String search, SearchMode mode, int start) {
|
||||
try {
|
||||
currentSearch = search;
|
||||
this.start = start;
|
||||
System.setProperty("webdriver.chrome.driver", Settings.PATHTOCROMEDRIVER.getContent());
|
||||
driver = new ChromeDriver();
|
||||
login();
|
||||
//prepareSearchPage();
|
||||
|
||||
switch (mode) {
|
||||
case KEYWORD:
|
||||
searchByTopic();
|
||||
break;
|
||||
case RESEARCHAREA:
|
||||
searchComplex("SU=" + currentSearch);
|
||||
break;
|
||||
case TOPIC:
|
||||
searchComplex("TS = (" + currentSearch + ")");
|
||||
}
|
||||
startMain();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private synchronized void searchByTopic() throws InterruptedException {
|
||||
WebElement searchBox = driver.findElement(By.id("value(input1)"));
|
||||
searchBox.sendKeys(currentSearch);
|
||||
searchBox.submit();
|
||||
|
||||
wait(5000);
|
||||
}
|
||||
|
||||
private synchronized void searchComplex(String search) throws InterruptedException {
|
||||
driver.findElement(By.cssSelector("a[title*=Advanced]")).click();
|
||||
wait(1000);
|
||||
WebElement box = driver.findElement(By.cssSelector("textarea[class*=Adv_formBoxesSearch]"));
|
||||
box.sendKeys(search);
|
||||
wait(500);
|
||||
driver.findElement(By.id("search-button")).click();
|
||||
wait(1000);
|
||||
driver.findElement(By.id("hitCount")).click();
|
||||
wait(5000);
|
||||
|
||||
}
|
||||
|
||||
private synchronized void startMain() throws InterruptedException {
|
||||
int lastPage = Integer.parseInt(driver.findElement(By.id("pageCount.top")).getText().replace(",", ""));
|
||||
goToPage(lastPage);
|
||||
|
||||
List<WebElement> list = driver.findElements(By.cssSelector("div[class=search-results-number-align"));
|
||||
int maxCount = Integer.parseInt(list.get(list.size() - 1).getText().replace(",", "").replace(".", ""));
|
||||
|
||||
driver.findElement(By.cssSelector("span[class*=onload-secondary-button")).click();
|
||||
|
||||
wait(1000);
|
||||
|
||||
driver.findElement(By.id("numberOfRecordsRange")).click();
|
||||
|
||||
int min = start;
|
||||
int max = start + 49999;
|
||||
if (maxCount < max) {
|
||||
max = maxCount;
|
||||
setToMarkedList(min, max);
|
||||
driver.findElement(By.cssSelector("button[class=onload-primary-button]")).click();
|
||||
|
||||
wait(30000);
|
||||
|
||||
goAndPrepareMarked(max);
|
||||
} else {
|
||||
setToMarkedList(min, max);
|
||||
driver.findElement(By.cssSelector("button[class=onload-primary-button]")).click();
|
||||
|
||||
wait(30000);
|
||||
|
||||
goAndPrepareMarked(max);
|
||||
goToSeachPage();
|
||||
min = max;
|
||||
max = maxCount;
|
||||
setToMarkedList(min, max);
|
||||
driver.findElement(By.cssSelector("button[class=onload-primary-button]")).click();
|
||||
|
||||
wait(30000);
|
||||
|
||||
goAndPrepareMarked(max);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void setToMarkedList(int min, int max) throws InterruptedException {
|
||||
WebElement from = driver.findElement(By.id("markFrom"));
|
||||
WebElement to = driver.findElement(By.id("markTo"));
|
||||
from.clear();
|
||||
to.clear();
|
||||
from.sendKeys(String.valueOf(min));
|
||||
wait(500);
|
||||
to.sendKeys(String.valueOf(max));
|
||||
}
|
||||
|
||||
private void goAndPrepareMarked(int maxNumber) throws InterruptedException {
|
||||
|
||||
driver.findElement(By.cssSelector("a[title*=Marked")).click();
|
||||
wait(1000);
|
||||
driver.findElement(By.id("value(select_All)")).click();
|
||||
wait(590);
|
||||
driver.findElement(By.id("ISSN_fields")).click();
|
||||
wait(540);
|
||||
driver.findElement(By.id("ACCESSION_NUM_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("ADDRS_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("IDS_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("PMID_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("OPEN_ACCESS_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("SABBR_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("AUTHORSIDENTIFIERS_fields")).click();
|
||||
wait(513);
|
||||
driver.findElement(By.id("CONFERENCE_SPONSORS_fields")).click();
|
||||
wait(513);
|
||||
|
||||
int min = 1;
|
||||
int max = 0;
|
||||
if (maxNumber < 499) {
|
||||
export(1, maxNumber);
|
||||
clearMarkedList();
|
||||
return;
|
||||
}
|
||||
while (max + 499 < maxNumber) {
|
||||
wait(10000);
|
||||
export(min, max + 499);
|
||||
min = min + 499;
|
||||
max = max + 499;
|
||||
}
|
||||
export(min, maxNumber);
|
||||
clearMarkedList();
|
||||
|
||||
}
|
||||
|
||||
private void clearMarkedList() throws InterruptedException {
|
||||
driver.findElement(By.cssSelector("button[class*=snowplow-wos-markedlist-clear-click")).click();
|
||||
wait(500);
|
||||
driver.switchTo().alert().accept();
|
||||
}
|
||||
|
||||
private void goToSeachPage() {
|
||||
driver.findElement(By.cssSelector("a[title*=Results]")).click();
|
||||
}
|
||||
|
||||
private void export(int start, int end) throws InterruptedException {
|
||||
WebElement from = driver.findElement(By.id("markFrom"));
|
||||
from.clear();
|
||||
WebElement to = driver.findElement(By.id("markTo"));
|
||||
to.clear();
|
||||
|
||||
from.sendKeys(String.valueOf(start));
|
||||
wait(512);
|
||||
to.sendKeys(String.valueOf(end));
|
||||
|
||||
driver.findElement(By.id("record_select_type_range")).click();
|
||||
wait(1056);
|
||||
|
||||
if (driver.findElements(By.id("exportTypeName")).size() != 0) {
|
||||
driver.findElement(By.id("exportTypeName")).click();
|
||||
wait(1056);
|
||||
driver.findElement(By.cssSelector("a[name*=Excel")).click();
|
||||
} else {
|
||||
driver.findElement(By.cssSelector("button[title*=Excel]")).click();
|
||||
|
||||
}
|
||||
wait(981);
|
||||
driver.findElement(By.cssSelector("button[name=saveToExcel")).click();
|
||||
|
||||
wait((int) (60000 + Math.random() * 10000)); //Erhöhen
|
||||
}
|
||||
|
||||
|
||||
public void tearDown() {
|
||||
if (driver != null) {
|
||||
driver.close();
|
||||
}
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
|
||||
public synchronized void login() throws InterruptedException {
|
||||
driver.navigate().to("https://apps.webofknowledge.com/UA_GeneralSearch_input.do?product=UA&SID=C6dqdgBgdQXkwQwYd3J&search_mode=GeneralSearch");
|
||||
|
||||
if (!driver.getTitle().equals("Web of Science - Please Sign In to Access Web of Science")) {
|
||||
searchPage = driver.getCurrentUrl();
|
||||
return;
|
||||
}
|
||||
|
||||
WebElement box = driver.findElement(By.name("username"));
|
||||
box.sendKeys(Settings.USERNAME.getContent());
|
||||
box = driver.findElement(By.name("password"));
|
||||
box.sendKeys(Settings.PASSWORD.getContent());
|
||||
List<WebElement> list = driver.findElements(By.cssSelector("a[class=no-focus-style]"));
|
||||
wait(1000);
|
||||
list.get(0).click();
|
||||
if (driver.getTitle() == "Web of Science - SIGN IN") ;
|
||||
{
|
||||
box = driver.findElement(By.linkText("continue and establish a new session"));
|
||||
box.click();
|
||||
}
|
||||
wait(1000);
|
||||
searchPage = driver.getCurrentUrl();
|
||||
}
|
||||
|
||||
|
||||
private void setAllDatabases() throws InterruptedException {
|
||||
List<WebElement> buttons = driver.findElements(By.cssSelector("span[class=select2-selection__arrow]"));
|
||||
buttons.get(0).click();
|
||||
WebElement button = driver.findElement(By.cssSelector("li[class=select2-results__option]"));
|
||||
button.click();
|
||||
wait(1000);
|
||||
}
|
||||
|
||||
|
||||
private void goToPage(int page) {
|
||||
WebElement currentPage = driver.findElement(By.name("page"));
|
||||
currentPage.clear();
|
||||
currentPage.sendKeys(Integer.toString(page));
|
||||
currentPage.submit();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user