Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Wikipedia backlinks api support #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions gowiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,53 @@ func Summary(title string, numsentence int, numchar int, suggest bool, redirect

return res.Query.Page[strconv.Itoa(page.PageID)].Extract, nil
}

/*
Get a list of pages which link to a certain page.

Keyword arguments:

* title: The title of the page you want the backlinks

Return:

* List of titles of Wikipedia pages that links to the page.

* Error
*/
func GetBacklinks(title string) ([]string, error) {
var backlinks []string
last := ""
args := map[string]string{
"action": "query",
"list": "backlinks",
"bltitle": title,
"bllimit": "max",
}

for {
if last != "" {
args["blcontinue"] = last
}

res, err := utils.WikiRequester(args)
if err != nil {
return []string{}, err
}
if res.Error.Code != "" {
return []string{}, errors.New(res.Error.Info)
}

for _, s := range res.Query.Backlinks {
backlinks = append(backlinks, s.Title)
}

blcontinue := res.Continue["blcontinue"]
if blcontinue == nil {
break
}
last = blcontinue.(string)
}

return backlinks, nil
}
5 changes: 5 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type InnerNormalize struct {
To string `json:"to"`
}

type InnerBacklinks struct {
Title string `json:"title"`
}

type RequestQuery struct {
SearchInfo InnerSearchInfo `json:"searchinfo"`
Normalize []InnerNormalize `json:"normalized"`
Expand All @@ -75,6 +79,7 @@ type RequestQuery struct {
Page map[string]InnerPage `json:"pages"`
Random []InnerSearch `json:"random"`
Language []map[string]string `json:"languages"`
Backlinks []InnerBacklinks `json:"backlinks"`
}

/*
Expand Down
21 changes: 21 additions & 0 deletions test/backlinks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test

import (
"testing"

gowiki "github.com/trietmn/go-wiki"
"github.com/trietmn/go-wiki/utils"
)

func TestBacklinks(t *testing.T) {
utils.WikiRequester = MockRequester
expectation := utils.TurnSliceOfString(MockData["great_wall_of_china.backlinks"].([]interface{}))
res, err := gowiki.GetBacklinks("Great Wall of China")
if err != nil {
t.Errorf("%v", err)
}
if !utils.CompareSlice(res, expectation) {
t.Errorf("got %v, expect %v", res, expectation)
}

}
30 changes: 29 additions & 1 deletion test/mock_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,33 @@
"great_wall_of_china.geo_search_with_existing_article_name": [
"Great Wall of China"
],
"great_wall_of_china.geo_search_with_non_existing_article_name": []
"great_wall_of_china.geo_search_with_non_existing_article_name": [],
"great_wall_of_china.backlinks": [
"Army",
"China",
"Conscription",
"History of China",
"Civil engineering",
"Civilization (video game)",
"Empire",
"Factoid",
"Göktürks",
"Ibn Battuta",
"Keno",
"Khunjerab Pass",
"Mongols",
"Mongolia",
"Marco Polo",
"May 25",
"Microevolution",
"Mississippi River",
"Shang-Chi",
"Nanotechnology",
"Geography of China",
"Telecommunications in China",
"Qing dynasty",
"Richard Nixon",
"Sabah",
"Shaolin Monastery"
]
}
32 changes: 32 additions & 0 deletions test/mock_wiki_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -1475,5 +1475,37 @@
"query": {
"geosearch": []
}
},
"action:query;list:backlinks;bltitle:Great Wall of China;bllimit:max": {
"query": {
"backlinks": [
{"Title": "Army"},
{"Title": "China"},
{"Title": "Conscription"},
{"Title": "History of China"},
{"Title": "Civil engineering"},
{"Title": "Civilization (video game)"},
{"Title": "Empire"},
{"Title": "Factoid"},
{"Title": "Göktürks"},
{"Title": "Ibn Battuta"},
{"Title": "Keno"},
{"Title": "Khunjerab Pass"},
{"Title": "Mongols"},
{"Title": "Mongolia"},
{"Title": "Marco Polo"},
{"Title": "May 25"},
{"Title": "Microevolution"},
{"Title": "Mississippi River"},
{"Title": "Shang-Chi"},
{"Title": "Nanotechnology"},
{"Title": "Geography of China"},
{"Title": "Telecommunications in China"},
{"Title": "Qing dynasty"},
{"Title": "Richard Nixon"},
{"Title": "Sabah"},
{"Title": "Shaolin Monastery"}
]
}
}
}