13 changed files with 222 additions and 40 deletions
@ -1,21 +1,19 @@ |
|||
package com.example.datacollect; |
|||
|
|||
import com.example.datacollect.controller.CrawlerController; |
|||
import com.example.datacollect.model.Article; |
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class Main { |
|||
|
|||
public static void main(String[] args) { |
|||
ConsoleView view = new ConsoleView(); |
|||
List<Article> articles = new ArrayList<>(); |
|||
CrawlerController controller = new CrawlerController(view, articles); |
|||
ArticleRepository repository = new ArticleRepository(); |
|||
CrawlerController controller = new CrawlerController(view, repository); |
|||
|
|||
view.printSuccess("Welcome to CLI Crawler (w9_1)! Type help for commands."); |
|||
view.printSuccess("Welcome to CLI Crawler (w10)! Type help for commands."); |
|||
while (true) { |
|||
controller.handle(view.readLine()); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,9 +1,8 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.model.Article; |
|||
import java.util.List; |
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
|
|||
public interface Command { |
|||
String getName(); |
|||
void execute(String[] args, List<Article> articles); |
|||
void execute(String[] args, ArticleRepository repository); |
|||
} |
|||
|
|||
@ -0,0 +1,42 @@ |
|||
package com.example.datacollect.repository; |
|||
|
|||
import com.example.datacollect.model.Article; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
import java.util.Optional; |
|||
|
|||
public class ArticleRepository { |
|||
private final List<Article> articles = new ArrayList<>(); |
|||
|
|||
public void add(Article article) { |
|||
if (article != null) { |
|||
articles.add(article); |
|||
} |
|||
} |
|||
|
|||
public void addAll(List<Article> newArticles) { |
|||
if (newArticles != null && !newArticles.isEmpty()) { |
|||
articles.addAll(newArticles); |
|||
} |
|||
} |
|||
|
|||
public List<Article> getAll() { |
|||
return Collections.unmodifiableList(articles); |
|||
} |
|||
|
|||
public Optional<Article> findByTitle(String title) { |
|||
return articles.stream() |
|||
.filter(a -> a.getTitle().equals(title)) |
|||
.findFirst(); |
|||
} |
|||
|
|||
public int count() { |
|||
return articles.size(); |
|||
} |
|||
|
|||
public void clear() { |
|||
articles.clear(); |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
package com.example.datacollect.strategy; |
|||
|
|||
import com.example.datacollect.model.Article; |
|||
|
|||
import java.util.List; |
|||
|
|||
public interface CrawlStrategy { |
|||
String getName(); |
|||
List<Article> crawl(String url); |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
package com.example.datacollect.strategy; |
|||
|
|||
import com.example.datacollect.model.Article; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class MockStrategy implements CrawlStrategy { |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "mock"; |
|||
} |
|||
|
|||
@Override |
|||
public List<Article> crawl(String url) { |
|||
List<Article> articles = new ArrayList<>(); |
|||
articles.add(new Article("Mock Article 1", url, "Mock content 1", "Mock Author", "2024-01-01")); |
|||
articles.add(new Article("Mock Article 2", url, "Mock content 2", "Mock Author", "2024-01-02")); |
|||
return articles; |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
package com.example.datacollect.strategy; |
|||
|
|||
import com.example.datacollect.model.Article; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class SimpleStrategy implements CrawlStrategy { |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "simple"; |
|||
} |
|||
|
|||
@Override |
|||
public List<Article> crawl(String url) { |
|||
List<Article> articles = new ArrayList<>(); |
|||
String siteName = extractSiteName(url); |
|||
articles.add(new Article("Simple Article from " + siteName, url, "Simple content extracted from " + url, |
|||
"Anonymous", java.time.LocalDate.now().toString())); |
|||
articles.add(new Article("Another Article from " + siteName, url, "More content from " + url, |
|||
"Unknown", java.time.LocalDate.now().toString())); |
|||
articles.add(new Article("Latest News from " + siteName, url, "Breaking news content", |
|||
"Editor", java.time.LocalDate.now().toString())); |
|||
return articles; |
|||
} |
|||
|
|||
private String extractSiteName(String url) { |
|||
try { |
|||
if (url.startsWith("http://") || url.startsWith("https://")) { |
|||
int start = url.indexOf("://") + 3; |
|||
int end = url.indexOf('/', start); |
|||
if (end > start) { |
|||
return url.substring(start, end); |
|||
} |
|||
return url.substring(start); |
|||
} |
|||
return url; |
|||
} catch (Exception e) { |
|||
return url; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
package com.example.datacollect.strategy; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
public class StrategyFactory { |
|||
private final Map<String, CrawlStrategy> strategies = new HashMap<>(); |
|||
|
|||
public StrategyFactory() { |
|||
register(new MockStrategy()); |
|||
register(new SimpleStrategy()); |
|||
} |
|||
|
|||
public void register(CrawlStrategy strategy) { |
|||
strategies.put(strategy.getName().toLowerCase(), strategy); |
|||
} |
|||
|
|||
public CrawlStrategy getStrategy(String url) { |
|||
if (url == null || url.isEmpty()) { |
|||
return strategies.get("mock"); |
|||
} |
|||
|
|||
if (url.toLowerCase().contains("mock")) { |
|||
return strategies.get("mock"); |
|||
} |
|||
|
|||
if (url.toLowerCase().contains("simple")) { |
|||
return strategies.get("simple"); |
|||
} |
|||
|
|||
if (url.startsWith("http://") || url.startsWith("https://")) { |
|||
return strategies.get("simple"); |
|||
} |
|||
|
|||
return strategies.get("mock"); |
|||
} |
|||
|
|||
public CrawlStrategy getStrategyByName(String name) { |
|||
return strategies.get(name.toLowerCase()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue