From 20fc885ca6a293fa15681638a115b724d26c65aa Mon Sep 17 00:00:00 2001 From: Javier <39586924+Arilucea@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:42:34 +0200 Subject: [PATCH] Added function to use wikipedia backlinks api --- gowiki.go | 50 +++++++++++++++++++++++++++++++++++++ models/models.go | 5 ++++ test/backlinks_test.go | 21 ++++++++++++++++ test/mock_data.json | 30 +++++++++++++++++++++- test/mock_wiki_request.json | 32 ++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 test/backlinks_test.go diff --git a/gowiki.go b/gowiki.go index 6044031..d77f1bb 100644 --- a/gowiki.go +++ b/gowiki.go @@ -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 +} diff --git a/models/models.go b/models/models.go index 590a356..a32f814 100644 --- a/models/models.go +++ b/models/models.go @@ -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"` @@ -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"` } /* diff --git a/test/backlinks_test.go b/test/backlinks_test.go new file mode 100644 index 0000000..792ecae --- /dev/null +++ b/test/backlinks_test.go @@ -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) + } + +} diff --git a/test/mock_data.json b/test/mock_data.json index 75eeaba..5a37b3e 100644 --- a/test/mock_data.json +++ b/test/mock_data.json @@ -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" + ] } \ No newline at end of file diff --git a/test/mock_wiki_request.json b/test/mock_wiki_request.json index 17b09be..3279f10 100644 --- a/test/mock_wiki_request.json +++ b/test/mock_wiki_request.json @@ -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"} + ] + } } } \ No newline at end of file