package com.poprosturonin.sites.thecodinglove;

import com.poprosturonin.data.Meme;
import com.poprosturonin.data.Page;
import com.poprosturonin.data.contents.GIFContent;
import com.poprosturonin.exceptions.PageIsEmptyException;
import com.poprosturonin.sites.PageScrapper;
import com.poprosturonin.utils.URLUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * TheCodingLove scrapper
 */
@Component
public class TheCodingLovePageScrapper implements PageScrapper {

    private Optional<Meme> parsePicture(Element post) {
        Element titleAsLink = post.select("h1.blog-post-title > a").first();
        Element realImage = post.select(".blog-post-content img").first();

        return Optional.of(
                new Meme(
                        titleAsLink.text(),
                        new GIFContent(realImage.attr("src")),
                        titleAsLink.attr("href"),
                        0,
                        0)
        );
    }

    public Page parsePage(Document document) {
        Page page = new Page();

        // Get next link page
        Element nextPageElement = document.select(".next-posts-btn").last();
        if (nextPageElement != null)
            page.setNextPage("/thecodinglove/page" + URLUtils.cutToSecondSlash(nextPageElement.attr("href")).get());

        // Get content
        Elements pictures = document.select(".blog-post");
        List<Meme> memes = pictures.stream()
                .map(this::parsePicture)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(Collectors.toList());

        page.getMemes().addAll(memes);

        page.setTitle(document.title());

        if (page.isEmpty())
            throw new PageIsEmptyException();

        return page;
    }
}

@@ -160,24 +160,77 @@
 GIFContent;%0A
+import com.poprosturonin.data.contents.VideoContent;%0A
 import com.p
@@ -811,10 +811,8 @@
 ct(%22
-h1
 .blo
@@ -851,28 +851,54 @@
     
+Optional%3C
 Element
- realI
+%3E i
 mage = 
+Optional.ofNullable(
 post
@@ -938,19 +938,57 @@
 .first()
+)
 ;%0A%0A
+        if (image.isPresent()) %7B%0A    
         
@@ -1007,16 +1007,20 @@
 nal.of(%0A
+    
         
@@ -1053,32 +1053,36 @@
                 
+    
 titleAsLink.text
@@ -1105,24 +1105,28 @@
             
+   
+ 
 new GIFConte
@@ -1132,17 +1132,19 @@
 ent(
-realImage
+image.get()
 .att
@@ -1170,32 +1170,36 @@
                 
+ 
+   
 titleAsLink.attr
@@ -1208,16 +1208,20 @@
 href%22),%0A
+    
         
@@ -1267,19 +1267,371 @@
     
-0)%0A        
+    0)%0A            );%0A        %7D%0A%0A        Optional%3CElement%3E video = Optional.ofNullable(post.select(%22.blog-post-content video %3E source%22).first());%0A%0A        return video.map(element -%3E new Meme(%0A                titleAsLink.text(),%0A                new VideoContent(element.attr(%22src%22)),%0A                titleAsLink.attr(%22href%22),%0A                0,%0A                0)
 );%0A 

Add support for video on thecodinglove