Created
January 11, 2017 22:15
-
-
Save Domacoski/368c46b925bf7124a9de88ba6f0388e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import android.util.Patterns; | |
import android.webkit.URLUtil; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.nodes.Element; | |
import org.jsoup.select.Elements; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
/** | |
* Created by thiago.domacoski on 06/01/17. | |
*/ | |
public class LoaderPreview extends AsyncTask<String, Void, LoaderPreview.ObjectPreview> { | |
private final Context mContext; | |
private final MainActivity.PreviewViewHolder mHolder; | |
public LoaderPreview(final Context mContext, final MainActivity.PreviewViewHolder mHolder){ | |
this.mContext = mContext; | |
this.mHolder = mHolder; | |
} | |
@Override | |
protected ObjectPreview doInBackground(String... strings) { | |
if(null == strings || 0 == strings.length){ | |
return null; | |
} | |
String title = null; | |
String url = strings[0]; | |
if(null == url){ | |
return null; | |
} | |
if(!isValidURL(url)){ | |
final String[] values = url.split("http"); | |
final String clean = values[1]; | |
title = values[0]; | |
final String cleanUrl = "http" + clean; | |
if(!isValidURL(cleanUrl)){ | |
return null; | |
}else{ | |
url = cleanUrl; | |
} | |
} | |
try { | |
final Document doc = Jsoup.connect(url).get(); | |
Log.i("DOCUMENT", doc.toString()); | |
Elements itens = doc.select("meta[property^=og:]"); | |
if (itens.size() == 0) { | |
return null; | |
} | |
final ObjectPreview objectPreview = new ObjectPreview(); | |
objectPreview.url = url; | |
for (int i = 0; i < itens.size(); i++) { | |
Element tag = itens.get(i); | |
String text = tag.attr("property"); | |
if ("og:image".equals(text)) { | |
objectPreview.image = tag.attr("content"); | |
} else if ("og:description".equals(text)) { | |
objectPreview.description = tag.attr("content"); | |
} else if ("og:title".equals(text)) { | |
objectPreview.title = tag.attr("content"); | |
} | |
} | |
if(title != null){ | |
objectPreview.description = new String(objectPreview.title); | |
objectPreview.title = title; | |
} | |
return objectPreview; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
private boolean isValidURL(String urlString) { | |
try { | |
final URL url = new URL(urlString); | |
return URLUtil.isValidUrl(urlString) && Patterns.WEB_URL.matcher(urlString).matches(); | |
} catch (MalformedURLException e) { | |
return false; | |
} | |
} | |
@Override | |
protected void onPostExecute(ObjectPreview objectPreview) { | |
if(objectPreview != null){ | |
this.mHolder.setObject(objectPreview); | |
} | |
} | |
public static class ObjectPreview { | |
public String title; | |
public String description; | |
public String image; | |
public String url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment