Showing
2 changed files
with
76 additions
and
23 deletions
jaksimsamil-server/src/util/StringToDate.js
0 → 100644
| 1 | +exports.StringToDate_BJ = function (date_str) { | ||
| 2 | + let arr_date = date_str.split(" "); //yyyy m dd tt MM SS Fomat LIST | ||
| 3 | + let arr_date_r = arr_date.map(function (str) { | ||
| 4 | + let str_r = str.slice(0, -1); | ||
| 5 | + | ||
| 6 | + return str_r.length == 1 ? "0" + str_r : str_r; | ||
| 7 | + }); | ||
| 8 | + | ||
| 9 | + return arr_date_r[0] + arr_date_r[1] + arr_date_r[2]; //YYYYMMDD 형식으로 반환 | ||
| 10 | +}; |
| 1 | const axios = require("axios"); | 1 | const axios = require("axios"); |
| 2 | const cheerio = require("cheerio"); | 2 | const cheerio = require("cheerio"); |
| 3 | +const StringToDate = require("./StringToDate"); | ||
| 4 | +/* | ||
| 5 | +ToDO | ||
| 6 | +- 유저 네임 검증 | ||
| 7 | +- 예외 처리 | ||
| 8 | +*/ | ||
| 9 | +exports.getBJ = async function (userid) { | ||
| 10 | + let data_list = []; | ||
| 11 | + let next_page_link = ""; | ||
| 3 | 12 | ||
| 4 | -exports.getBJ = function (userid) { | 13 | + await getStartPage(userid).then((html) => { |
| 5 | - const getHtml = async (userid) => { | 14 | + //시작 페이지를 가져온다. |
| 6 | - try { | 15 | + //같은 객체를 두번 선언한다. 퍼포먼스에 문제 생길수도 |
| 7 | - return await axios.get("https://www.acmicpc.net/user/" + userid); | 16 | + //함수에 객체를 넘기는 방법도 있다. |
| 8 | - } catch (error) { | 17 | + //첫 페이지 가져온다. |
| 9 | - console.log(error); | 18 | + data_list.push(getData(html)); |
| 10 | - } | 19 | + next_page_link = getNextPageLink(html); |
| 11 | - }; | 20 | + }); |
| 12 | - | 21 | + while (next_page_link != -1) { |
| 13 | - getHtml(userid).then((html) => { | 22 | + //다음 페이지를 가져온다. |
| 14 | - let psList = []; | 23 | + await getNextPage(next_page_link).then((html) => { |
| 15 | - const $ = cheerio.load(html.data); | 24 | + data_list.push(getData(html)); |
| 16 | - const $bodyList = $("div.panel-body").children(); | 25 | + next_page_link = getNextPageLink(html); |
| 17 | - | ||
| 18 | - $bodyList.each(function (i) { | ||
| 19 | - if (i % 2 == 0) { | ||
| 20 | - psList[i / 2] = { | ||
| 21 | - problem_number: $(this).children().text(), | ||
| 22 | - problem_title: $(this).next().children().text(), | ||
| 23 | - }; | ||
| 24 | - } | ||
| 25 | }); | 26 | }); |
| 27 | + } | ||
| 28 | + return data_list.flat(1); | ||
| 29 | +}; | ||
| 30 | + | ||
| 31 | +const getStartPage = async (userid) => { | ||
| 32 | + //유저 아이디 입력 | ||
| 33 | + try { | ||
| 34 | + return await axios.get( | ||
| 35 | + "https://www.acmicpc.net/status?user_id=" + userid + "&result_id=4" | ||
| 36 | + ); | ||
| 37 | + } catch (error) { | ||
| 38 | + console.log(error); | ||
| 39 | + } | ||
| 40 | +}; | ||
| 26 | 41 | ||
| 27 | - console.log(psList); | 42 | +const getNextPage = async (link) => { |
| 28 | - return psList; | 43 | + //링크 입력 |
| 44 | + try { | ||
| 45 | + return await axios.get(link); | ||
| 46 | + } catch (error) { | ||
| 47 | + console.log(error); | ||
| 48 | + } | ||
| 49 | +}; | ||
| 50 | + | ||
| 51 | +const getData = (html) => { | ||
| 52 | + //페이지 데이터 파싱 | ||
| 53 | + let psArr = []; | ||
| 54 | + const $ = cheerio.load(html.data); | ||
| 55 | + const $bodyList = $("#status-table > tbody"); | ||
| 56 | + $bodyList.children().each((index, element) => { | ||
| 57 | + psArr.push({ | ||
| 58 | + problem_number: $(element).find("a.problem_title").text(), | ||
| 59 | + problem_title: $(element).find("a.problem_title").attr("title"), | ||
| 60 | + solved_date: StringToDate.StringToDate_BJ( | ||
| 61 | + $(element).find("a.real-time-update").attr("title") | ||
| 62 | + ), | ||
| 63 | + }); | ||
| 29 | }); | 64 | }); |
| 65 | + return psArr; | ||
| 66 | +}; | ||
| 67 | +const getNextPageLink = (html) => { | ||
| 68 | + //다음 페이지가 있으면 다음 페이지 주소 return, 없으면 -1 return | ||
| 69 | + const $ = cheerio.load(html.data); | ||
| 70 | + return $("#next_page").attr("href") | ||
| 71 | + ? "https://www.acmicpc.net/" + $("#next_page").attr("href") | ||
| 72 | + : -1; | ||
| 30 | }; | 73 | }; | ... | ... |
-
Please register or login to post a comment