Skip to content

Commit 0a071a1

Browse files
committed
Add useragent & improve maintenance detection
1 parent f7fe8f9 commit 0a071a1

File tree

10 files changed

+60
-2
lines changed

10 files changed

+60
-2
lines changed

docs/openapi.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
"404": {
3737
"description": "Not Found: try requesting something else."
3838
},
39+
"500": {
40+
"description": "Something went wrong. Currently is returned when server's or proxy's IP address is banned on the BDO website."
41+
},
3942
"503": {
4043
"description": "Service Unavailable: [https://naeu.playblackdesert.com](naeu.playblackdesert.com) is currently under maintenance and requested data is not cached."
4144
}
@@ -269,6 +272,9 @@
269272
"404": {
270273
"$ref": "#/components/responses/404"
271274
},
275+
"500": {
276+
"$ref": "#/components/responses/500"
277+
},
272278
"503": {
273279
"$ref": "#/components/responses/503"
274280
}
@@ -387,6 +393,9 @@
387393
"404": {
388394
"$ref": "#/components/responses/404"
389395
},
396+
"500": {
397+
"$ref": "#/components/responses/500"
398+
},
390399
"503": {
391400
"$ref": "#/components/responses/503"
392401
}
@@ -485,6 +494,9 @@
485494
"404": {
486495
"$ref": "#/components/responses/404"
487496
},
497+
"500": {
498+
"$ref": "#/components/responses/500"
499+
},
488500
"503": {
489501
"$ref": "#/components/responses/503"
490502
}
@@ -582,6 +594,9 @@
582594
"404": {
583595
"$ref": "#/components/responses/404"
584596
},
597+
"500": {
598+
"$ref": "#/components/responses/500"
599+
},
585600
"503": {
586601
"$ref": "#/components/responses/503"
587602
}

handlers/GetAdventurer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func GetAdventurer(w http.ResponseWriter, r *http.Request) {
3030
if !found {
3131
data, status = scrapers.ScrapeAdventurer(region, profileTarget)
3232

33+
if status == http.StatusInternalServerError {
34+
w.WriteHeader(status)
35+
return
36+
}
37+
3338
if ok := giveMaintenanceResponse(w, region); ok {
3439
return
3540
}

handlers/GetAdventurerSearch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func GetAdventurerSearch(w http.ResponseWriter, r *http.Request) {
3333
if !found {
3434
data, status = scrapers.ScrapeAdventurerSearch(region, query, searchType, page)
3535

36+
if status == http.StatusInternalServerError {
37+
w.WriteHeader(status)
38+
return
39+
}
40+
3641
if ok := giveMaintenanceResponse(w, region); ok {
3742
return
3843
}

handlers/GetGuild.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func GetGuild(w http.ResponseWriter, r *http.Request) {
3030
if !found {
3131
data, status = scrapers.ScrapeGuild(region, name)
3232

33+
if status == http.StatusInternalServerError {
34+
w.WriteHeader(status)
35+
return
36+
}
37+
3338
if ok := giveMaintenanceResponse(w, region); ok {
3439
return
3540
}

handlers/GetGuildSearch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func GetGuildSearch(w http.ResponseWriter, r *http.Request) {
3232
if !found {
3333
data, status = scrapers.ScrapeGuildSearch(region, name, page)
3434

35+
if status == http.StatusInternalServerError {
36+
w.WriteHeader(status)
37+
return
38+
}
39+
3540
if ok := giveMaintenanceResponse(w, region); ok {
3641
return
3742
}

scrapers/ScrapeAdventurer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func ScrapeAdventurer(region string, profileTarget string) (profile models.Profi
2222
profile.Region = region
2323
status = http.StatusNotFound
2424

25+
// Detect being banned by IP address
26+
c.OnHTML(`.type_2`, func(e *colly.HTMLElement) {
27+
status = http.StatusInternalServerError
28+
})
29+
2530
c.OnHTML(`.nick`, func(e *colly.HTMLElement) {
2631
profile.FamilyName = e.Text
2732
status = http.StatusOK

scrapers/ScrapeAdventurerSearch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ func ScrapeAdventurerSearch(region string, query string, searchType uint8, page
1616

1717
status = http.StatusNotFound
1818

19+
// Detect being banned by IP address
20+
c.OnHTML(`.type_2`, func(e *colly.HTMLElement) {
21+
status = http.StatusInternalServerError
22+
})
23+
1924
c.OnHTML(`.box_list_area li:not(.no_result)`, func(e *colly.HTMLElement) {
2025
status = http.StatusOK
2126
profile := models.Profile{

scrapers/ScrapeGuild.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func ScrapeGuild(region, name string) (guildProfile models.GuildProfile, status
1717
guildProfile.Region = region
1818
status = http.StatusNotFound
1919

20+
// Detect being banned by IP address
21+
c.OnHTML(`.type_2`, func(e *colly.HTMLElement) {
22+
status = http.StatusInternalServerError
23+
})
24+
2025
c.OnHTML(`.region_info`, func(e *colly.HTMLElement) {
2126
guildProfile.Region = e.Text
2227
})

scrapers/ScrapeGuildSearch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ func ScrapeGuildSearch(region, query string, page uint16) (guildProfiles []model
1717

1818
status = http.StatusNotFound
1919

20+
// Detect being banned by IP address
21+
c.OnHTML(`.type_2`, func(e *colly.HTMLElement) {
22+
status = http.StatusInternalServerError
23+
})
24+
2025
c.OnHTML(`.box_list_area li:not(.no_result)`, func(e *colly.HTMLElement) {
2126
createdOn := utils.ParseDate(e.ChildText(".date"))
2227
status = http.StatusOK

scrapers/scraper.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ type scraper struct {
1616
}
1717

1818
func newScraper(region string) (s scraper) {
19+
useragent := colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
20+
1921
s.region = region
20-
s.c = colly.NewCollector()
22+
s.c = colly.NewCollector(useragent)
2123
s.c.SetRequestTimeout(time.Minute / 2)
2224

2325
if len(config.GetProxyList()) > 0 {
@@ -30,7 +32,8 @@ func newScraper(region string) (s scraper) {
3032
}
3133
})
3234

33-
s.OnHTML(`.closetime_wrap`, func(e *colly.HTMLElement) {
35+
// Detect maintenance
36+
s.OnHTML(`.type_3`, func(e *colly.HTMLElement) {
3437
setCloseTime(region)
3538
})
3639

0 commit comments

Comments
 (0)