Toggle navigation
Toggle navigation
This project
Loading...
Sign in
방승일
/
mamuri-bot
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
윤준석
2022-05-15 23:28:04 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
15d1d8cb5edee6edaee039571bd9fb4ce7d61f05
15d1d8cb
1 parent
a2f3379e
FIX: error handling in servie layer
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
9 deletions
joongna/service/item.go
joongna/service/item.go
View file @
15d1d8c
...
...
@@ -24,7 +24,10 @@ func GetItemByKeyword(keyword string) ([]model.Item, error) {
var
items
[]
model
.
Item
wg
:=
sync
.
WaitGroup
{}
itemsInfo
:=
getItemsInfoByKeyword
(
keyword
)
itemsInfo
,
err
:=
getItemsInfoByKeyword
(
keyword
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
itemInfo
:=
range
itemsInfo
{
itemUrl
:=
itemInfo
.
Link
...
...
@@ -34,7 +37,7 @@ func GetItemByKeyword(keyword string) ([]model.Item, error) {
wg
.
Add
(
1
)
go
func
(
itemUrl
string
)
{
defer
wg
.
Done
()
err
,
item
:=
crawlingNaverCafe
(
itemUrl
)
item
,
err
:=
crawlingNaverCafe
(
itemUrl
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
...
...
@@ -46,13 +49,13 @@ func GetItemByKeyword(keyword string) ([]model.Item, error) {
return
items
,
nil
}
func
getItemsInfoByKeyword
(
keyword
string
)
[]
model
.
ApiResponseItem
{
func
getItemsInfoByKeyword
(
keyword
string
)
([]
model
.
ApiResponseItem
,
error
)
{
encText
:=
url
.
QueryEscape
(
"중고나라 "
+
keyword
+
" 판매중"
)
apiUrl
:=
"https://openapi.naver.com/v1/search/cafearticle.json?query="
+
encText
+
"&sort=sim"
req
,
err
:=
http
.
NewRequest
(
"GET"
,
apiUrl
,
nil
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
nil
,
err
}
req
.
Header
.
Add
(
"X-Naver-Client-Id"
,
config
.
Cfg
.
Secret
.
CLIENTID
)
req
.
Header
.
Add
(
"X-Naver-Client-Secret"
,
config
.
Cfg
.
Secret
.
CLIENTSECRET
)
...
...
@@ -60,7 +63,7 @@ func getItemsInfoByKeyword(keyword string) []model.ApiResponseItem {
client
:=
&
http
.
Client
{}
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
nil
,
err
}
defer
func
(
Body
io
.
ReadCloser
)
{
err
:=
Body
.
Close
()
...
...
@@ -75,16 +78,16 @@ func getItemsInfoByKeyword(keyword string) []model.ApiResponseItem {
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
return
apiResponse
.
Items
return
apiResponse
.
Items
,
nil
}
func
crawlingNaverCafe
(
cafeUrl
string
)
(
error
,
*
model
.
Item
)
{
func
crawlingNaverCafe
(
cafeUrl
string
)
(
*
model
.
Item
,
error
)
{
frame
:=
rod
.
New
()
.
MustConnect
()
.
MustPage
(
cafeUrl
)
.
MustElement
(
"iframe#cafe_main"
)
time
.
Sleep
(
time
.
Second
*
2
)
source
:=
frame
.
MustFrame
()
.
MustHTML
()
html
,
err
:=
goquery
.
NewDocumentFromReader
(
bytes
.
NewReader
([]
byte
(
source
)))
if
err
!=
nil
{
return
err
,
nil
return
nil
,
err
}
title
:=
html
.
Find
(
"h3.title_text"
)
.
Text
()
...
...
@@ -108,7 +111,7 @@ func crawlingNaverCafe(cafeUrl string) (error, *model.Item) {
}
fmt
.
Println
(
"crawling "
+
cafeUrl
+
" title: "
+
title
)
return
nil
,
&
item
return
&
item
,
nil
}
func
priceStringToInt
(
priceString
string
)
int
{
...
...
Please
register
or
login
to post a comment