Skip to content

Commit a2d9066

Browse files
committed
Added maven as vc support, made dl dynamic and changed parsing routines. More parsing changes to come.
1 parent b27e46d commit a2d9066

File tree

6 files changed

+133
-56
lines changed

6 files changed

+133
-56
lines changed

.project

+6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
<arguments>
1111
</arguments>
1212
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
1318
</buildSpec>
1419
<natures>
20+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
1521
<nature>org.eclipse.jdt.core.javanature</nature>
1622
</natures>
1723
</projectDescription>

pom.xml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>kindle_autoloader</groupId>
4+
<artifactId>kindle_autoloader</artifactId>
5+
<version>1.0.0-beta</version>
6+
<build>
7+
<sourceDirectory>src</sourceDirectory>
8+
<resources>
9+
<resource>
10+
<directory>src</directory>
11+
<excludes>
12+
<exclude>**/*.java</exclude>
13+
</excludes>
14+
</resource>
15+
</resources>
16+
<plugins>
17+
<plugin>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.1</version>
20+
<configuration>
21+
<source/>
22+
<target/>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.jsoup</groupId>
30+
<artifactId>jsoup</artifactId>
31+
<version>1.8.3</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.ning</groupId>
35+
<artifactId>async-http-client</artifactId>
36+
<version>1.9.36</version>
37+
</dependency>
38+
</dependencies>
39+
</project>

src/com/jackyoustra/kautoload/Book.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Book {
1111
private String title;
1212
private String author;
1313
private String filename;
14+
private String md5;
1415
private URL downloadLocation;
1516
private int size; // in kb
1617

@@ -20,10 +21,11 @@ public Book(String title, String author, String filename) {
2021
this.filename = filename;
2122
}
2223

23-
public Book(String title, String author, String filename, URL downloadLocation, int size) {
24+
public Book(String title, String author, String md5, String extension, URL downloadLocation, int size) {
2425
this.title = title;
2526
this.author = author;
26-
this.filename = filename;
27+
this.md5 = md5;
28+
this.filename = title + "." + extension;
2729
this.downloadLocation = downloadLocation;
2830
this.size = size;
2931
}
@@ -37,7 +39,7 @@ public String getAuthor() {
3739
}
3840

3941
public String getMD5(){
40-
return filename.substring(0, filename.indexOf('.'));
42+
return md5;
4143
}
4244

4345
public String getExtension(){

src/com/jackyoustra/kautoload/Libgen.java

+53-42
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static Book[] search(String term) throws IOException{
3434
"foreignfiction/index.php"+ // subdirectory (fiction only now)
3535
"?s="+ // prefix for query
3636
URLEncoder.encode(term, "UTF-8")+ // encode search term properly
37-
"&f_lang=0"+ // language type
37+
"&f_lang=English"+ // language type
3838
"&f_columns=0"+
3939
"&f_ext=0");
4040
URLConnection libgenConnection = destWebpage.openConnection();
@@ -49,58 +49,69 @@ public static Book[] search(String term) throws IOException{
4949
}
5050

5151
final Book[] urls = booksFromHTML(finalHTML);
52-
return urls; // unable to find any
52+
return urls;
5353
}
5454

5555
private static Book[] booksFromHTML(String finalHTML) throws MalformedURLException {
5656
ArrayList<Book> books = new ArrayList<Book>();
5757
Document doc = Jsoup.parse(finalHTML);
58-
Elements tableData = doc.getElementsByTag("td"); // find all table data elements
58+
Elements tables = doc.getElementsByTag("table");
59+
Element targetTable = null;
60+
// target: the table after the headings table
61+
for(int i = 0; i < tables.size(); i++){
62+
Element table = tables.get(i);
63+
if(table.getElementsByTag("td").get(0).html().contains("Author(s)")){
64+
targetTable = tables.get(i+1);
65+
break;
66+
}
67+
}
68+
69+
Elements tableData = targetTable.getElementsByTag("td"); // find all table data elements
5970
for(Element tableDatum : tableData){
6071
Element dlTag = tableDatum.getElementById("1");
6172
if(dlTag != null){
6273
final String currentLine = dlTag.toString();
6374
final String lowerCaseLine = currentLine.toLowerCase();
64-
if(lowerCaseLine.contains("english")){
65-
Pattern dlPattern = Pattern.compile("/foreignfiction/get\\.php\\?md5=([a-z]|[0-9])*"); // it's an http request with an md5 arg
66-
Matcher dlMatches = dlPattern.matcher(currentLine);
67-
dlMatches.find();
68-
final String dlLink = dlMatches.group();
69-
final String md5 = dlLink.substring(dlLink.indexOf("=") + 1);
70-
final URL dlURL = new URL(mirror + dlLink);
71-
72-
final String titlePrefix = "itle:</td><td>";
73-
final Pattern titlePattern = Pattern.compile(titlePrefix + "[^<]*"); // <td>Title1:</td><td>The Hunt for Red October</td>
74-
final Matcher titleMatcher = titlePattern.matcher(currentLine);
75-
titleMatcher.find();
76-
String title = titleMatcher.group();
77-
title = title.substring(titlePrefix.length());
78-
79-
final String authorPrefix = "uthor1:</td><td>";
80-
final Pattern authorPattern = Pattern.compile(authorPrefix + "[^<]*"); // <td>Author1:</td><td>Clancy, Tom</td>
81-
final Matcher authorMatcher = authorPattern.matcher(currentLine);
82-
authorMatcher.find();
83-
String author = authorMatcher.group();
84-
author = author.substring(authorPrefix.length());
85-
86-
final Pattern extensionPattern = Pattern.compile(">[a-z]*\\([0-9]*.*\\)"); //>epub(854kb)</a>
87-
final Matcher extensionMatcher = extensionPattern.matcher(currentLine);
88-
extensionMatcher.find();
89-
final String extensionSize = extensionMatcher.group();
90-
String extension = extensionSize.substring(1, extensionSize.indexOf('('));
91-
92-
String sizeNotation = extensionSize.substring(extensionSize.indexOf('(')+1, extensionSize.indexOf(')')).toLowerCase();
93-
int size = 0;
94-
if(sizeNotation.indexOf('k') != -1){
95-
size = Integer.parseInt(sizeNotation.substring(0, sizeNotation.indexOf('k')));
96-
}
97-
else if(sizeNotation.indexOf('m') != -1){
98-
size = Integer.parseInt(sizeNotation.substring(0, sizeNotation.indexOf('m'))) * 1024; // iz megabyte
99-
}
100-
101-
Book currentBook = new Book(title, author, md5 + "." + extension, dlURL, size);
102-
books.add(currentBook);
75+
if(lowerCaseLine.contains("english")){
76+
Pattern dlPattern = Pattern.compile("/foreignfiction/get\\.php\\?md5=([a-z]|[0-9])*"); // it's an http request with an md5 arg
77+
Matcher dlMatches = dlPattern.matcher(currentLine);
78+
dlMatches.find();
79+
final String dlLink = dlMatches.group();
80+
final String md5 = dlLink.substring(dlLink.indexOf("=") + 1);
81+
final URL dlURL = new URL(mirror + dlLink);
82+
83+
final String titlePrefix = "itle:</td><td>";
84+
final Pattern titlePattern = Pattern.compile(titlePrefix + "[^<]*"); // <td>Title1:</td><td>The Hunt for Red October</td>
85+
final Matcher titleMatcher = titlePattern.matcher(currentLine);
86+
titleMatcher.find();
87+
String title = titleMatcher.group();
88+
title = title.substring(titlePrefix.length());
89+
90+
final String authorPrefix = "uthor1:</td><td>";
91+
final Pattern authorPattern = Pattern.compile(authorPrefix + "[^<]*"); // <td>Author1:</td><td>Clancy, Tom</td>
92+
final Matcher authorMatcher = authorPattern.matcher(currentLine);
93+
authorMatcher.find();
94+
String author = authorMatcher.group();
95+
author = author.substring(authorPrefix.length());
96+
97+
final Pattern extensionPattern = Pattern.compile(">[a-z]*\\([0-9]*.*\\)"); //>epub(854kb)</a>
98+
final Matcher extensionMatcher = extensionPattern.matcher(currentLine);
99+
extensionMatcher.find();
100+
final String extensionSize = extensionMatcher.group();
101+
String extension = extensionSize.substring(1, extensionSize.indexOf('('));
102+
103+
String sizeNotation = extensionSize.substring(extensionSize.indexOf('(')+1, extensionSize.indexOf(')')).toLowerCase();
104+
int size = 0;
105+
if(sizeNotation.indexOf('k') != -1){
106+
size = Integer.parseInt(sizeNotation.substring(0, sizeNotation.indexOf('k')));
107+
}
108+
else if(sizeNotation.indexOf('m') != -1){
109+
size = Integer.parseInt(sizeNotation.substring(0, sizeNotation.indexOf('m'))) * 1024; // iz megabyte
103110
}
111+
112+
Book currentBook = new Book(title, author, md5, extension, dlURL, size);
113+
books.add(currentBook);
114+
}
104115
}
105116
}
106117
Book[] urls = new Book[books.size()];

src/com/jackyoustra/kautoload/LibgenTableModel.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class LibgenTableModel extends AbstractTableModel {
66

77
private Book[] books;
8-
private String[] columnNames = {"Title", "Author", "Extension", "Size (kb)"};
8+
private String[] columnNames = {"Title", "Author", "Size (kb)", "Extension"};
99

1010
public LibgenTableModel(){
1111
this(new Book[0]);
@@ -48,9 +48,9 @@ public Object getValueAt(int rowIndex, int columnIndex) {
4848
case 1:
4949
return currentBook.getAuthor();
5050
case 2:
51-
return currentBook.getExtension();
52-
case 3:
5351
return currentBook.getSize();
52+
case 3:
53+
return currentBook.getExtension();
5454
default:
5555
break;
5656
}

src/com/jackyoustra/kautoload/MainApp.java

+27-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import javax.swing.JTable;
3737
import javax.swing.JTextField;
3838
import javax.swing.SwingConstants;
39+
import javax.swing.SwingUtilities;
40+
import javax.swing.SwingWorker;
3941
import javax.swing.UIManager;
4042
import javax.swing.UnsupportedLookAndFeelException;
4143
import javax.swing.filechooser.FileSystemView;
@@ -146,7 +148,6 @@ private void initialize() {
146148
public void actionPerformed(ActionEvent e) {
147149
try {
148150
Book[] books = Libgen.search(txtSearch.getText());
149-
System.out.println(books.toString());
150151
tableModel.update(books);
151152

152153
} catch (IOException e1) {
@@ -209,13 +210,31 @@ public void actionPerformed(ActionEvent event) {
209210
final int selectedRow = table.getSelectedRow();
210211
final Book selectedBook = tableModel.getBook(selectedRow);
211212
final String md5 = selectedBook.getMD5();
212-
try {
213-
Libgen.download(md5, getKindleDocumentsDirectory() + selectedBook.getFilename());
214-
JOptionPane.showMessageDialog(frmKindleAutoloader, "Book successfuly downloaded to Kindle", "Success", JOptionPane.INFORMATION_MESSAGE);
215-
} catch (IOException e) {
216-
JOptionPane.showMessageDialog(frmKindleAutoloader, "Error", "Error downloading book", JOptionPane.ERROR_MESSAGE);
217-
e.printStackTrace();
218-
}
213+
final String dlPath = getKindleDocumentsDirectory() + selectedBook.getFilename();
214+
// return true on success, false on failure
215+
SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
216+
@Override
217+
protected Boolean doInBackground(){
218+
try{
219+
Libgen.download(md5, dlPath);
220+
SwingUtilities.invokeLater(new Runnable() {
221+
@Override
222+
public void run() {
223+
JOptionPane.showMessageDialog(frmKindleAutoloader, "Book successfuly downloaded to Kindle", "Success", JOptionPane.INFORMATION_MESSAGE);
224+
}
225+
});
226+
return true;
227+
}catch(IOException e){
228+
SwingUtilities.invokeLater(new Runnable() {
229+
@Override
230+
public void run() {
231+
JOptionPane.showMessageDialog(frmKindleAutoloader, "Error", "Error downloading book", JOptionPane.ERROR_MESSAGE);
232+
}
233+
});
234+
return false;
235+
}
236+
}
237+
};
219238
}
220239
});
221240

0 commit comments

Comments
 (0)