-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from Recipe-Project/feature/async_thumbnail_c…
…rawling Feature/async thumbnail crawling
- Loading branch information
Showing
3 changed files
with
119 additions
and
63 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.recipe.app.src.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
@Override | ||
public Executor getAsyncExecutor() { | ||
|
||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(5); | ||
executor.setMaxPoolSize(10); | ||
executor.setQueueCapacity(100); | ||
executor.setThreadNamePrefix("AsyncExecutor-"); | ||
executor.initialize(); | ||
|
||
return executor; | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
...n/java/com/recipe/app/src/recipe/application/blog/BlogRecipeThumbnailCrawlingService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.recipe.app.src.recipe.application.blog; | ||
|
||
import com.recipe.app.src.recipe.domain.blog.BlogRecipe; | ||
import com.recipe.app.src.recipe.infra.blog.BlogRecipeRepository; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.select.Elements; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.net.URL; | ||
import java.util.List; | ||
|
||
@Service | ||
public class BlogRecipeThumbnailCrawlingService { | ||
|
||
private final BlogRecipeRepository blogRecipeRepository; | ||
|
||
public BlogRecipeThumbnailCrawlingService(BlogRecipeRepository blogRecipeRepository) { | ||
this.blogRecipeRepository = blogRecipeRepository; | ||
} | ||
|
||
@Async | ||
public void saveThumbnails(List<BlogRecipe> blogRecipes) { | ||
|
||
System.out.println("thumbnail save"); | ||
|
||
for (BlogRecipe blogRecipe : blogRecipes) { | ||
blogRecipe.changeThumbnail(getBlogThumbnailUrl(blogRecipe.getBlogUrl())); | ||
} | ||
|
||
blogRecipeRepository.saveAll(blogRecipes); | ||
} | ||
|
||
public String getBlogThumbnailUrl(String blogUrl) { | ||
|
||
if (blogUrl.contains("naver")) { | ||
return getNaverBlogThumbnailUrl(blogUrl); | ||
} else if (blogUrl.contains("tistory")) { | ||
return getTistoryBlogThumbnailUrl(blogUrl); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
|
||
private String getNaverBlogThumbnailUrl(String blogUrl) { | ||
|
||
try { | ||
|
||
URL url = new URL(blogUrl); | ||
Document doc = Jsoup.parse(url, 5000); | ||
|
||
Elements iframes = doc.select("iframe#mainFrame"); | ||
String src = iframes.attr("src"); | ||
|
||
String url2 = "http://blog.naver.com" + src; | ||
Document doc2 = Jsoup.connect(url2).get(); | ||
|
||
return doc2.select("meta[property=og:image]").get(0).attr("content"); | ||
} catch (Exception e) { | ||
return ""; | ||
} | ||
} | ||
|
||
private String getTistoryBlogThumbnailUrl(String blogUrl) { | ||
|
||
try { | ||
|
||
Document doc = Jsoup.connect(blogUrl).get(); | ||
|
||
Elements imageLinks = doc.getElementsByTag("img"); | ||
String thumbnailUrl = null; | ||
for (Element image : imageLinks) { | ||
String temp = image.attr("src"); | ||
if (!temp.contains("admin")) { | ||
thumbnailUrl = temp; | ||
break; | ||
} | ||
} | ||
|
||
return thumbnailUrl; | ||
} catch (Exception e) { | ||
return ""; | ||
} | ||
} | ||
} |