package strategy; import model.Paper; import com.fasterxml.jackson.databind.ObjectMapper; import utils.Utils; import java.util.ArrayList; import java.util.List; public class SemanticScholarStrategy extends AbstractCrawlerStrategy { @Override public String getPlatformName() { return "Semantic Scholar"; } @Override public boolean supportsUrl(String url) { return url != null && url.contains("semanticscholar.org"); } @Override protected List fetchPapers(String url, int count) throws Exception { List papers = new ArrayList<>(); System.out.println("=== 开始使用Semantic Scholar获取论文 ==="); addDelay(1000, 1500); try { String response = Utils.sendGetRequest(url); if (response.isEmpty()) return papers; ObjectMapper objectMapper = new ObjectMapper(); SemanticScholarResponse apiResponse = objectMapper.readValue(response, SemanticScholarResponse.class); if (apiResponse != null && apiResponse.getItems() != null) { int collected = 0; for (SemanticScholarPaper apiPaper : apiResponse.getItems()) { if (collected >= count) break; String title = apiPaper.getTitle(); String paperUrl = apiPaper.getUrl(); String abstractText = apiPaper.getAbstractText(); StringBuilder authorsBuilder = new StringBuilder(); if (apiPaper.getAuthors() != null) { for (SemanticScholarAuthor author : apiPaper.getAuthors()) { if (author != null && author.getName() != null) { if (authorsBuilder.length() > 0) { authorsBuilder.append(", "); } authorsBuilder.append(author.getName()); } } } String authors = authorsBuilder.toString(); if (title == null || title.length() < 5 || paperUrl == null || paperUrl.isEmpty()) continue; papers.add(new Paper(title, authors, abstractText != null ? abstractText : "", paperUrl, getPlatformName())); collected++; } } } catch (Exception e) { System.out.println("Semantic Scholar解析失败: " + e.getMessage()); } return papers; } private static class SemanticScholarResponse { private List items; public List getItems() { return items; } @SuppressWarnings("unused") public void setItems(List items) { this.items = items; } } private static class SemanticScholarPaper { private String title; private String url; private List authors; private String abstractText; public String getTitle() { return title; } @SuppressWarnings("unused") public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } @SuppressWarnings("unused") public void setUrl(String url) { this.url = url; } public List getAuthors() { return authors; } @SuppressWarnings("unused") public void setAuthors(List authors) { this.authors = authors; } public String getAbstractText() { return abstractText; } @SuppressWarnings("unused") public void setAbstractText(String abstractText) { this.abstractText = abstractText; } } private static class SemanticScholarAuthor { private String name; public String getName() { return name; } @SuppressWarnings("unused") public void setName(String name) { this.name = name; } } }