Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package joy.reactor.data.mappers.post
- import android.text.TextUtils
- import joy.reactor.data.enteties.Image
- import joy.reactor.data.http.HttpClient
- import org.jsoup.nodes.Element
- import java.util.*
- /**
- * Created by omazhukin on 4/14/2016.
- */
- class PostImagesThumbnailParser(private val element: Element) {
- var imagesList = ArrayList<Image>();
- fun getExpandedImages(): MutableList<Image> {
- return imagesList;
- }
- fun load(): Image? {
- val imgList = element.getElementsByClass("image")
- for (img in imgList) {
- if (img != null) {
- val gif = GifThumbnailParser(img).load()
- if (gif != null) {
- // This prevents from gif losing due to the preview
- imagesList.add(gif)
- } else {
- val imageScr = img.select("a[href] img")
- if (imageScr.isEmpty()) {
- continue
- }
- val attr = imageScr.attr("height")
- if (attr.contains("%")) {
- throw IllegalStateException("Unsupported Height")
- }
- val height = parseDimen(imageScr[0], "height")
- val width = parseDimen(imageScr[0], "width");
- imagesList.add(Image(getImageLink(imageScr[0]), width, height))
- }
- }
- }
- if (imagesList.size > 0) {
- return imagesList[0];
- } else {
- return null;
- }
- }
- private fun getImageLink(img: Element): String {
- return if (hasFull(img)) {
- img.parent().attr("href").replace("(/full/).+(-\\d+\\.)".toRegex(), "$1$2")
- } else {
- img.attr("src").replace("(/post/).+(-\\d+\\.)".toRegex(), "$1$2")
- }
- }
- private fun parseDimen(img: Element, attrName: String): Int {
- val attr = img.attr(attrName)
- if (attr == null || TextUtils.isEmpty(attr)) {
- return 0;
- } else {
- return Integer.parseInt(attr);
- }
- }
- private fun hasFull(img: Element): Boolean {
- return "a" == img.parent().tagName()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement