moved base url to config

This commit is contained in:
overflowerror 2021-11-22 18:08:34 +01:00
parent 9dbb96e99d
commit 999d062659
5 changed files with 13 additions and 12 deletions

View file

@ -49,7 +49,7 @@ func main() {
for range time.Tick(time.Hour * 24) {
log.Println("tick")
page, err := wikipedia.Get()
page, err := wikipedia.Get(config)
if err != nil {
log.Println(err)
continue

View file

@ -1,4 +1,5 @@
{
"consumer_key": "",
"consumer_secret": ""
"consumer_secret": "",
"base_url": "https://en.wikipedia.org"
}

View file

@ -8,6 +8,7 @@ import (
type Config struct {
ConsumerKey string `json:"consumer_key"`
ConsumerSecret string `json:"consumer_secret"`
BaseUrl string `json:"base_url"`
}
type AccessConfig struct {

View file

@ -35,12 +35,10 @@ type randomReponse struct {
} `json:"query"`
}
const baseURL = "https://de.wikipedia.org"
var noDescription = errors.New("no description found")
var noPageInfo = errors.New("no page info found")
func request(params map[string]string) ([]byte, error) {
func request(baseURL string, params map[string]string) ([]byte, error) {
builder := strings.Builder{}
builder.WriteString(baseURL)
builder.WriteString("/w/api.php?")
@ -79,7 +77,7 @@ func responseToPageInfo(response infoResponse) (PageInfo, error) {
return PageInfo{}, noPageInfo
}
func queryInfo(id int64) (PageInfo, error) {
func queryInfo(baseUrl string, id int64) (PageInfo, error) {
params := map[string]string {
"action": "query",
"pageids": strconv.FormatInt(id, 10),
@ -87,7 +85,7 @@ func queryInfo(id int64) (PageInfo, error) {
"inprop": "url",
}
content, err := request(params)
content, err := request(baseUrl, params)
if err != nil {
return PageInfo{}, err
}
@ -101,14 +99,14 @@ func queryInfo(id int64) (PageInfo, error) {
return responseToPageInfo(response)
}
func queryRandom() (int64, error) {
func queryRandom(baseUrl string) (int64, error) {
params := map[string]string {
"action": "query",
"list": "random",
"rnnamespace": "0",
}
content, err := request(params)
content, err := request(baseUrl, params)
if err != nil {
return 0, err
}

View file

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
. "wikitil/internal/config"
)
const retries = 5
@ -12,15 +13,15 @@ var illegalDescriptionParts = []string{
"Begriffsklärungsseite",
}
func Get() (PageInfo, error){
func Get(config Config) (PageInfo, error){
retryLoop:
for i := 0; i < retries; i++ {
id, err := queryRandom()
id, err := queryRandom(config.BaseUrl)
if err != nil {
return PageInfo{}, err
}
info, err := queryInfo(id)
info, err := queryInfo(config.BaseUrl, id)
if err != nil {
fmt.Println(err)