Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#27] Feat: 백준 문제 크롤링 및 DB 저장 #34

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.jsoup:jsoup:1.15.3'
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

// Swagger API
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2")
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/sajang/devracebackend/domain/Problem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sajang.devracebackend.domain.common.BaseEntity;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -20,7 +21,7 @@ public class Problem extends BaseEntity implements Serializable {
private Long id;

@Column(name = "number", unique = true)
private Integer number;
private Integer number; //백준 문제번호

private String title;

Expand All @@ -45,4 +46,18 @@ public class Problem extends BaseEntity implements Serializable {
@Column(name = "sample_output")
private String sampleOutput; // 예제 출력


@Builder
public Problem(Long id, Integer number, String title, String content, String imageUrl, String problemInput, String problemOutput, String problemLimit, String sampleInput, String sampleOutput) {
this.id = id;
this.number = number;
this.title = title;
this.content = content;
this.imageUrl = imageUrl;
this.problemInput = problemInput;
this.problemOutput = problemOutput;
this.problemLimit = problemLimit;
this.sampleInput = sampleInput;
this.sampleOutput = sampleOutput;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sajang.devracebackend.dto.problem;

import com.sajang.devracebackend.domain.Problem;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ProblemDto {

private String title;
private String content;
private String imageUrl;
private String problemInput;
private String problemOutput;
private String problemLimit;
private String sampleInput;
private String sampleOutput;
private Integer number;

@Builder
public ProblemDto(String imageUrl, String title, Integer number,String content, String problemInput, String problemOutput, String problemLimit, String sampleInput, String sampleOutput){
this.imageUrl = imageUrl;
this.title = title;
this.number = number;
this.content =content;
this.problemInput = problemInput;
this.problemOutput = problemOutput;
this.problemLimit = problemLimit;
this.sampleInput = sampleInput;
this.sampleOutput = sampleOutput;
}
public Problem toEntity(){
return Problem.builder()
.imageUrl(imageUrl)
.title(title)
.number(number)
.content(content)
.problemInput(problemInput)
.problemOutput(problemOutput)
.problemLimit(problemLimit)
.sampleInput(sampleInput)
.sampleOutput(sampleOutput)
.build();
}
public ProblemDto(Problem problem){
this.number = problem.getNumber();
this.imageUrl = problem.getImageUrl();
this.title = problem.getTitle();
this.content = problem.getContent();
this.problemInput = problem.getProblemInput();
this.problemOutput = problem.getProblemOutput();
this.problemLimit = problem.getProblemLimit();
this.sampleInput = problem.getSampleInput();
this.sampleOutput = problem.getSampleOutput();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package com.sajang.devracebackend.service;

import com.sajang.devracebackend.dto.problem.ProblemDto;

public interface ProblemService {
//ProblemDto crawlProblem(Integer problemNumber);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
package com.sajang.devracebackend.service.impl;

import com.sajang.devracebackend.domain.Problem;
import com.sajang.devracebackend.dto.problem.ProblemDto;
import com.sajang.devracebackend.repository.ProblemRepository;
import com.sajang.devracebackend.service.ProblemService;
import lombok.RequiredArgsConstructor;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import org.jsoup.select.Elements;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
@RequiredArgsConstructor
public class ProblemServiceImpl implements ProblemService {

private final ProblemRepository problemRepository;

@Transactional
//@Override
public ProblemDto crawlProblem(Integer problemNumber) throws IOException{
String Url = "https://www.acmicpc.net/problem/" + problemNumber;
Document doc = Jsoup.connect(Url).get();

//#problem-body에 접근
Elements contents = doc.select("#problem-body"); //body에 접근
Elements contentHead = doc.select(".page-header h1"); //문제 title 추출을 위한 page-header에 접근
Elements sampleDataElement = doc.getElementsByClass("col-md-6"); //예제 문제쪽 요소에 따로 접근

int Amount = sampleDataElement.size(); //예제 문제 개수 추출

JSONObject sampleInputJson = new JSONObject();
JSONObject sampleOutputJson = new JSONObject();
for(int i =1;i<=Amount/2;i++){
sampleInputJson.put("sampleInput"+i,contents.select(".col-md-6 #sample-input-"+i).text()); //예제객체 담기
sampleOutputJson.put("sampleOutput"+i,contents.select(".col-md-6 #sample-output-"+i).text()); //예제객체 담기
}

String sampleInput = sampleInputJson.toString(); //Json -> String
String sampleOutput = sampleOutputJson.toString(); //Json -> String

String problemTitle = contentHead.select("#problem_title").text(); //title 은 따로 접근. body가 아닌 header 부분
ProblemDto data = ProblemDto.builder() //Builder를 이용해 ProblemDto 값으로 저장
.imageUrl(contents.select("p img").attr("abs:src"))
.number(problemNumber)
.title(problemTitle)
.content(contents.select("#problem_description").toString())
.problemInput(contents.select("#problem_input").toString())
.problemOutput(contents.select("#problem_output").toString())
.problemLimit(contents.select("#problem_limit").toString())
.sampleInput(sampleInput)
.sampleOutput(sampleOutput)
.build();

ProblemDto problemDto = new ProblemDto(
data.getImageUrl(),
data.getTitle(),
data.getNumber(),
data.getContent(),
data.getProblemInput(),
data.getProblemOutput(),
data.getProblemLimit(),
data.getSampleInput(),
data.getSampleOutput()
);

Problem problemEntity = problemRepository.save(problemDto.toEntity());

return new ProblemDto(problemEntity);
}
}
Loading