Skip to content

Commit e03b339

Browse files
authored
Refactoring & bug fixes (#39)
1 parent fbb8f60 commit e03b339

21 files changed

+218
-312
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,35 @@ If you host the API yourself, either via Docker or natively, you can control som
2323

2424
Available flags:
2525
- `-cachettl`
26-
- Allows to specify cache TTL in minutes
26+
- Specifies cache TTL in minutes
2727
- Type: unsigned integer
2828
- Default value: `180`
2929
- `-maintenancettl`
30-
- Allows to limit how frequently scraper can check for maintenance end in minutes
30+
- Limits how frequently scraper can check for maintenance end in minutes
31+
- Type: unsigned integer
32+
- Default value: `5`
33+
- `-maxtasksperclient`
34+
- Limits the number of concurrent scraping tasks that can be executed per client
3135
- Type: unsigned integer
3236
- Default value: `5`
3337
- `-port`
34-
- Allows to specify API server's port
38+
- Specifies API server's port
3539
- Type: unsigned integer
3640
- Default value: `8001`
3741
- Also available as `PORT` environment variable (doesn't work in Docker)
3842
- `-proxy`
39-
- Allows to specify a list of proxies to make requests to BDO servers through
43+
- Specifies a list of proxies to make requests to BDO servers through
4044
- Type: string, space-separated list of IP addresses or URLs
4145
- Default value: none, requests are made directly
4246
- Also available as `PROXY` environment variable
4347
- `-ratelimit`
4448
- Sets the maximum number of requests per minute per IP address
4549
- Type: unsigned integer
4650
- Default value: 512
51+
- `taskretries`
52+
- Specifies the number of retries for a scraping task
53+
- Type: unsigned integer
54+
- Default value: `3`
4755
- `-verbose`
4856
- Allows to put the app into verbose mode and print out additional logs to stdout
4957
- Default value: none, no additional output is produced

cache/cache.go

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package cache
22

33
import (
4-
"net/http"
54
"strings"
6-
"sync"
75
"time"
86

97
goCache "github.com/patrickmn/go-cache"
8+
"github.com/spf13/viper"
109
messagebus "github.com/vardius/message-bus"
1110
"golang.org/x/exp/maps"
1211

13-
"bdo-rest-api/config"
1412
"bdo-rest-api/models"
1513
"bdo-rest-api/utils"
1614
)
@@ -31,7 +29,7 @@ func joinKeys(keys []string) string {
3129
}
3230

3331
func newCache[T any]() *cache[T] {
34-
cacheTTL := config.GetCacheTTL()
32+
cacheTTL := viper.GetDuration("cachettl")
3533

3634
return &cache[T]{
3735
Bus: messagebus.New(100), // Idk what buffer size is optimal
@@ -40,32 +38,21 @@ func newCache[T any]() *cache[T] {
4038
}
4139

4240
func (c *cache[T]) AddRecord(keys []string, data T, status int, taskId string) (date string, expires string) {
43-
ttl := config.GetCacheTTL()
41+
cacheTTL := viper.GetDuration("cachettl")
4442
entry := CacheEntry[T]{
4543
Data: data,
4644
Date: time.Now(),
4745
Status: status,
4846
}
4947

50-
c.internalCache.Add(joinKeys(keys), entry, ttl)
48+
c.internalCache.Add(joinKeys(keys), entry, cacheTTL)
5149
c.Bus.Publish(taskId, entry)
5250

53-
return utils.FormatDateForHeaders(entry.Date), utils.FormatDateForHeaders(entry.Date.Add(ttl))
54-
}
55-
56-
func (c *cache[T]) SignalBypassCache(status int, taskId string) {
57-
var data T
58-
entry := CacheEntry[T]{
59-
Data: data,
60-
Date: time.Now(),
61-
Status: status,
62-
}
63-
64-
c.Bus.Publish(taskId, entry)
51+
return utils.FormatDateForHeaders(entry.Date), utils.FormatDateForHeaders(entry.Date.Add(cacheTTL))
6552
}
6653

6754
func (c *cache[T]) GetRecord(keys []string) (data T, status int, date string, expires string, found bool) {
68-
cacheTTL := config.GetCacheTTL()
55+
cacheTTL := viper.GetDuration("cachettl")
6956
anyEntry, found := c.internalCache.Get(joinKeys(keys))
7057

7158
if !found {
@@ -77,30 +64,6 @@ func (c *cache[T]) GetRecord(keys []string) (data T, status int, date string, ex
7764
return entry.Data, entry.Status, utils.FormatDateForHeaders(entry.Date), utils.FormatDateForHeaders(entry.Date.Add(cacheTTL)), found
7865
}
7966

80-
func (c *cache[T]) WaitForRecord(taskId string) (data T, status int, date string, expires string) {
81-
var wg sync.WaitGroup
82-
wg.Add(1)
83-
84-
c.Bus.Subscribe(taskId, func(entry CacheEntry[T]) {
85-
data = entry.Data
86-
status = entry.Status
87-
date = utils.FormatDateForHeaders(entry.Date)
88-
89-
if entry.Status == http.StatusInternalServerError {
90-
expires = date
91-
} else if entry.Status == http.StatusServiceUnavailable {
92-
expires = utils.FormatDateForHeaders(entry.Date.Add(config.GetMaintenanceStatusTTL()))
93-
} else {
94-
expires = utils.FormatDateForHeaders(entry.Date.Add(config.GetCacheTTL()))
95-
}
96-
97-
wg.Done()
98-
})
99-
100-
wg.Wait()
101-
return
102-
}
103-
10467
func (c *cache[T]) GetItemCount() int {
10568
return c.internalCache.ItemCount()
10669
}

cache/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"testing"
55
"time"
66

7-
"bdo-rest-api/config"
7+
"github.com/spf13/viper"
88
)
99

1010
func init() {
11-
config.SetCacheTTL(time.Second)
11+
viper.Set("cachettl", time.Second)
1212
}
1313

1414
func TestCache(t *testing.T) {

config/config.go

Lines changed: 0 additions & 128 deletions
This file was deleted.

config/config_test.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

docs/brokenStuff.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
# Broken Stuff
2-
BDO website, where all the data is taken from, has a number of bugs, which affects by this scraper by design and, sadly, not much can be done about it.
2+
BDO website, where all the data is taken from, has a number of bugs, which affects by this API by design and, sadly, not much can be done about it.
33

44
## List of known bugs
5-
This is a list of bugs that either used to occur or still occur that I am aware of:
5+
This is a list of bugs that either used to occur that I'm aware of. They may be still occuring or be fixed.
66

77
### 🐞 Data is not updated immediately after it is updated in game
8-
This is a common problem. The website's lag is around a few hours, and you can only wait. This API introduces additional lag that depends on the cache TTL parameter.
8+
The website's lag is around a few hours, and you can only wait. This API introduces additional lag that depends on the cache TTL parameter.
99

1010
### 🐞 Members who left the guild remain on the guild members' list for some time
11-
This is a common problem. I believe maintenances remove "ghost members" from guilds. If you don't feel like waiting, request profiles of those players. Guild membership status in player profiles is more reliable, unless it's set to private.
11+
I believe maintenances remove "ghost members" from guilds. If you don't feel like waiting, request profiles of those players. Guild membership status in player profiles is more reliable, unless it's set to private.
1212

1313
### 🐞 Profile of a different guild is returned instead of the one requested
14-
This is an uncommon problem. Always check if the guild profile in response has the same name you specified. Also see next bug.
14+
Always check if the guild profile in response has the same name you specified. Also see next bug.
1515

1616
### 🐞 Guild profile returned as "Not Found" although the guild exists in the game
17-
This is an uncommon problem. You can get some information like creation date, guild master's name and population by searching for guild instead of requesting its profile. It's not much, but better than nothing.
17+
You can get some information like creation date, guild master's name and population by searching for guild instead of requesting its profile. It's not much, but better than nothing.
1818

1919
### 🐞 Player profile returned as "Not Found" although that player exists in the game
20-
This is an uncommon problem. There are no known workarounds. [See issue #5](https://github.com/man90es/BDO-REST-API/issues/5).
20+
There are no known workarounds. [See issue #5](https://github.com/man90es/BDO-REST-API/issues/5).
2121

2222
### 🐞 Players whose family name is longer than 16 characters aren't searchable
23-
This is a rare problem. Family names longer than 16 characters aren't officially allowed in BDO, but there may be a bug that allows players to take them. [See issue #13](https://github.com/man90es/BDO-REST-API/issues/13). This API won't support them unless there will be many reports of players with long family names.
23+
Family names longer than 16 characters aren't officially allowed in BDO, but there may be a bug that allows players to take them. [See issue #13](https://github.com/man90es/BDO-REST-API/issues/13). This API won't support them unless there will be many reports of players with long family names.
24+
25+
### 🐞 Some guild members are missing when requesting a guild profile
26+
BDO website hides some guild members on its website based on unknown conditions. Supposedly, all hidden members are alt accounts with zero progress on the account.
27+
28+
### 🐞 The number of guild members when searching for a guild doesn't match the number of guild members when requesting the guild profile
29+
See the previous bug on the list. The number when searching is the correct one.
2430

2531
## Contribute to this list
2632
If you found a bug on the original BDO website that affects this API and is not listed in this file, you can contribute by either:

0 commit comments

Comments
 (0)