Toggle navigation
Toggle navigation
This project
Loading...
Sign in
송용우
/
oss-Jaksimsamil
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
송용우
2020-06-12 16:53:32 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
eb044ee7d432a43ec207c6273c08b3b28d0df85d
eb044ee7
1 parent
93587b51
Update getBJ with date
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
23 deletions
jaksimsamil-server/src/util/StringToDate.js
jaksimsamil-server/src/util/getBJ.js
jaksimsamil-server/src/util/StringToDate.js
0 → 100644
View file @
eb044ee
exports
.
StringToDate_BJ
=
function
(
date_str
)
{
let
arr_date
=
date_str
.
split
(
" "
);
//yyyy m dd tt MM SS Fomat LIST
let
arr_date_r
=
arr_date
.
map
(
function
(
str
)
{
let
str_r
=
str
.
slice
(
0
,
-
1
);
return
str_r
.
length
==
1
?
"0"
+
str_r
:
str_r
;
});
return
arr_date_r
[
0
]
+
arr_date_r
[
1
]
+
arr_date_r
[
2
];
//YYYYMMDD 형식으로 반환
};
jaksimsamil-server/src/util/getBJ.js
View file @
eb044ee
const
axios
=
require
(
"axios"
);
const
cheerio
=
require
(
"cheerio"
);
const
StringToDate
=
require
(
"./StringToDate"
);
/*
ToDO
- 유저 네임 검증
- 예외 처리
*/
exports
.
getBJ
=
async
function
(
userid
)
{
let
data_list
=
[];
let
next_page_link
=
""
;
exports
.
getBJ
=
function
(
userid
)
{
const
getHtml
=
async
(
userid
)
=>
{
try
{
return
await
axios
.
get
(
"https://www.acmicpc.net/user/"
+
userid
);
}
catch
(
error
)
{
console
.
log
(
error
);
}
};
getHtml
(
userid
).
then
((
html
)
=>
{
let
psList
=
[];
const
$
=
cheerio
.
load
(
html
.
data
);
const
$bodyList
=
$
(
"div.panel-body"
).
children
();
$bodyList
.
each
(
function
(
i
)
{
if
(
i
%
2
==
0
)
{
psList
[
i
/
2
]
=
{
problem_number
:
$
(
this
).
children
().
text
(),
problem_title
:
$
(
this
).
next
().
children
().
text
(),
};
}
await
getStartPage
(
userid
).
then
((
html
)
=>
{
//시작 페이지를 가져온다.
//같은 객체를 두번 선언한다. 퍼포먼스에 문제 생길수도
//함수에 객체를 넘기는 방법도 있다.
//첫 페이지 가져온다.
data_list
.
push
(
getData
(
html
));
next_page_link
=
getNextPageLink
(
html
);
});
while
(
next_page_link
!=
-
1
)
{
//다음 페이지를 가져온다.
await
getNextPage
(
next_page_link
).
then
((
html
)
=>
{
data_list
.
push
(
getData
(
html
));
next_page_link
=
getNextPageLink
(
html
);
});
}
return
data_list
.
flat
(
1
);
};
const
getStartPage
=
async
(
userid
)
=>
{
//유저 아이디 입력
try
{
return
await
axios
.
get
(
"https://www.acmicpc.net/status?user_id="
+
userid
+
"&result_id=4"
);
}
catch
(
error
)
{
console
.
log
(
error
);
}
};
console
.
log
(
psList
);
return
psList
;
const
getNextPage
=
async
(
link
)
=>
{
//링크 입력
try
{
return
await
axios
.
get
(
link
);
}
catch
(
error
)
{
console
.
log
(
error
);
}
};
const
getData
=
(
html
)
=>
{
//페이지 데이터 파싱
let
psArr
=
[];
const
$
=
cheerio
.
load
(
html
.
data
);
const
$bodyList
=
$
(
"#status-table > tbody"
);
$bodyList
.
children
().
each
((
index
,
element
)
=>
{
psArr
.
push
({
problem_number
:
$
(
element
).
find
(
"a.problem_title"
).
text
(),
problem_title
:
$
(
element
).
find
(
"a.problem_title"
).
attr
(
"title"
),
solved_date
:
StringToDate
.
StringToDate_BJ
(
$
(
element
).
find
(
"a.real-time-update"
).
attr
(
"title"
)
),
});
});
return
psArr
;
};
const
getNextPageLink
=
(
html
)
=>
{
//다음 페이지가 있으면 다음 페이지 주소 return, 없으면 -1 return
const
$
=
cheerio
.
load
(
html
.
data
);
return
$
(
"#next_page"
).
attr
(
"href"
)
?
"https://www.acmicpc.net/"
+
$
(
"#next_page"
).
attr
(
"href"
)
:
-
1
;
};
...
...
Please
register
or
login
to post a comment