Showing
2 changed files
with
73 additions
and
20 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 | -let userid_test = "syw5141"; | 3 | +const StringToDate = require("./StringToDate"); |
| 4 | -const getHtml = async (userid) => { | 4 | +/* |
| 5 | +ToDO | ||
| 6 | +- 유저 네임 검증 | ||
| 7 | +- 예외 처리 | ||
| 8 | +*/ | ||
| 9 | +exports.getBJ = async function (userid) { | ||
| 10 | + let data_list = []; | ||
| 11 | + let next_page_link = ""; | ||
| 12 | + | ||
| 13 | + await getStartPage(userid).then((html) => { | ||
| 14 | + //시작 페이지를 가져온다. | ||
| 15 | + //같은 객체를 두번 선언한다. 퍼포먼스에 문제 생길수도 | ||
| 16 | + //함수에 객체를 넘기는 방법도 있다. | ||
| 17 | + //첫 페이지 가져온다. | ||
| 18 | + data_list.push(getData(html)); | ||
| 19 | + next_page_link = getNextPageLink(html); | ||
| 20 | + }); | ||
| 21 | + while (next_page_link != -1) { | ||
| 22 | + //다음 페이지를 가져온다. | ||
| 23 | + await getNextPage(next_page_link).then((html) => { | ||
| 24 | + data_list.push(getData(html)); | ||
| 25 | + next_page_link = getNextPageLink(html); | ||
| 26 | + }); | ||
| 27 | + } | ||
| 28 | + return data_list.flat(1); | ||
| 29 | +}; | ||
| 30 | + | ||
| 31 | +const getStartPage = async (userid) => { | ||
| 32 | + //유저 아이디 입력 | ||
| 5 | try { | 33 | try { |
| 6 | - return await axios.get("https://www.acmicpc.net/user/" + userid); | 34 | + return await axios.get( |
| 35 | + "https://www.acmicpc.net/status?user_id=" + userid + "&result_id=4" | ||
| 36 | + ); | ||
| 7 | } catch (error) { | 37 | } catch (error) { |
| 8 | console.log(error); | 38 | console.log(error); |
| 9 | } | 39 | } |
| 10 | }; | 40 | }; |
| 11 | 41 | ||
| 12 | -getHtml(userid_test).then((html) => { | 42 | +const getNextPage = async (link) => { |
| 13 | - let psList = []; | 43 | + //링크 입력 |
| 14 | - const $ = cheerio.load(html.data); | 44 | + try { |
| 15 | - const $bodyList = $("div.panel-body").children(); | 45 | + return await axios.get(link); |
| 46 | + } catch (error) { | ||
| 47 | + console.log(error); | ||
| 48 | + } | ||
| 49 | +}; | ||
| 16 | 50 | ||
| 17 | - $bodyList.each(function (i) { | 51 | +const getData = (html) => { |
| 18 | - if (i % 2 == 0) { | 52 | + //페이지 데이터 파싱 |
| 19 | - psList[i / 2] = { | 53 | + let psArr = []; |
| 20 | - problem_number: $(this).children().text(), | 54 | + const $ = cheerio.load(html.data); |
| 21 | - problem_title: $(this).next().children().text(), | 55 | + const $bodyList = $("#status-table > tbody"); |
| 22 | - }; | 56 | + $bodyList.children().each((index, element) => { |
| 23 | - } | 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 | + }); | ||
| 24 | }); | 64 | }); |
| 25 | - | 65 | + return psArr; |
| 26 | - console.log(psList); | 66 | +}; |
| 27 | - return psList; | 67 | +const getNextPageLink = (html) => { |
| 28 | -}); | 68 | + //다음 페이지가 있으면 다음 페이지 주소 return, 없으면 -1 return |
| 29 | - | 69 | + const $ = cheerio.load(html.data); |
| 30 | -//body > div.wrapper > div.container.content > div.row > div:nth-child(2) > div:nth-child(3) > div.col-md-9 > div:nth-child(1) > div.panel-body | 70 | + return $("#next_page").attr("href") |
| 71 | + ? "https://www.acmicpc.net/" + $("#next_page").attr("href") | ||
| 72 | + : -1; | ||
| 73 | +}; | ... | ... |
-
Please register or login to post a comment