Skip to content

Commit

Permalink
Merge pull request #1 from LoxiaLiSA/master
Browse files Browse the repository at this point in the history
add DocumentGetter
  • Loading branch information
fuzhengyin authored Sep 14, 2021
2 parents 05ecd17 + d00b50e commit ae8e2d0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.leocardz.link.preview.library;

public abstract class BaseDocumentGetter implements DocumentGetter {

protected String url;

public BaseDocumentGetter(String url) {
this.url = url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.leocardz.link.preview.library;

import org.jsoup.nodes.Document;

import java.io.IOException;

public interface DocumentGetter {

Document getDocument() throws IOException;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.leocardz.link.preview.library;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class JsoupDocumentGetter extends BaseDocumentGetter {

public JsoupDocumentGetter(String url) {
super(url);
}

@Override
public Document getDocument() throws IOException {
return Jsoup.connect(url).userAgent("Mozilla").get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class TextCrawler {
public TextCrawler() {
}

public DocumentGetter buildDocumentGetter (String url) {
return new JsoupDocumentGetter(url);
}

public void makePreview(LinkPreviewCallback callback, String url) {
ImagePickingStrategy imagePickingStrategy = new DefaultImagePickingStrategy();

Expand Down Expand Up @@ -123,7 +127,8 @@ protected Void doInBackground(String... params) {
wasPreviewGenerationSuccessful = true;
} else {
try {
Document doc = getDocument();
DocumentGetter getter = buildDocumentGetter(sourceContent.getFinalUrl());
Document doc = getter.getDocument();

sourceContent.setHtmlCode(extendedTrim(doc.toString()));

Expand Down

0 comments on commit ae8e2d0

Please sign in to comment.