mirror of
https://github.com/sigmasternchen/wikitil
synced 2025-03-14 23:58:59 +00:00
moved base url to config
This commit is contained in:
parent
9dbb96e99d
commit
999d062659
5 changed files with 13 additions and 12 deletions
|
@ -49,7 +49,7 @@ func main() {
|
||||||
for range time.Tick(time.Hour * 24) {
|
for range time.Tick(time.Hour * 24) {
|
||||||
log.Println("tick")
|
log.Println("tick")
|
||||||
|
|
||||||
page, err := wikipedia.Get()
|
page, err := wikipedia.Get(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"consumer_key": "",
|
"consumer_key": "",
|
||||||
"consumer_secret": ""
|
"consumer_secret": "",
|
||||||
|
"base_url": "https://en.wikipedia.org"
|
||||||
}
|
}
|
|
@ -8,6 +8,7 @@ import (
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ConsumerKey string `json:"consumer_key"`
|
ConsumerKey string `json:"consumer_key"`
|
||||||
ConsumerSecret string `json:"consumer_secret"`
|
ConsumerSecret string `json:"consumer_secret"`
|
||||||
|
BaseUrl string `json:"base_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccessConfig struct {
|
type AccessConfig struct {
|
||||||
|
|
|
@ -35,12 +35,10 @@ type randomReponse struct {
|
||||||
} `json:"query"`
|
} `json:"query"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseURL = "https://de.wikipedia.org"
|
|
||||||
|
|
||||||
var noDescription = errors.New("no description found")
|
var noDescription = errors.New("no description found")
|
||||||
var noPageInfo = errors.New("no page info 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 := strings.Builder{}
|
||||||
builder.WriteString(baseURL)
|
builder.WriteString(baseURL)
|
||||||
builder.WriteString("/w/api.php?")
|
builder.WriteString("/w/api.php?")
|
||||||
|
@ -79,7 +77,7 @@ func responseToPageInfo(response infoResponse) (PageInfo, error) {
|
||||||
return PageInfo{}, noPageInfo
|
return PageInfo{}, noPageInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryInfo(id int64) (PageInfo, error) {
|
func queryInfo(baseUrl string, id int64) (PageInfo, error) {
|
||||||
params := map[string]string {
|
params := map[string]string {
|
||||||
"action": "query",
|
"action": "query",
|
||||||
"pageids": strconv.FormatInt(id, 10),
|
"pageids": strconv.FormatInt(id, 10),
|
||||||
|
@ -87,7 +85,7 @@ func queryInfo(id int64) (PageInfo, error) {
|
||||||
"inprop": "url",
|
"inprop": "url",
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := request(params)
|
content, err := request(baseUrl, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return PageInfo{}, err
|
return PageInfo{}, err
|
||||||
}
|
}
|
||||||
|
@ -101,14 +99,14 @@ func queryInfo(id int64) (PageInfo, error) {
|
||||||
return responseToPageInfo(response)
|
return responseToPageInfo(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryRandom() (int64, error) {
|
func queryRandom(baseUrl string) (int64, error) {
|
||||||
params := map[string]string {
|
params := map[string]string {
|
||||||
"action": "query",
|
"action": "query",
|
||||||
"list": "random",
|
"list": "random",
|
||||||
"rnnamespace": "0",
|
"rnnamespace": "0",
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := request(params)
|
content, err := request(baseUrl, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
. "wikitil/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
const retries = 5
|
const retries = 5
|
||||||
|
@ -12,15 +13,15 @@ var illegalDescriptionParts = []string{
|
||||||
"Begriffsklärungsseite",
|
"Begriffsklärungsseite",
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get() (PageInfo, error){
|
func Get(config Config) (PageInfo, error){
|
||||||
retryLoop:
|
retryLoop:
|
||||||
for i := 0; i < retries; i++ {
|
for i := 0; i < retries; i++ {
|
||||||
id, err := queryRandom()
|
id, err := queryRandom(config.BaseUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return PageInfo{}, err
|
return PageInfo{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := queryInfo(id)
|
info, err := queryInfo(config.BaseUrl, id)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
Loading…
Reference in a new issue