Skip to content

Commit

Permalink
Use wikimedia to fetch images
Browse files Browse the repository at this point in the history
  • Loading branch information
isZumpo committed May 17, 2024
1 parent 295913f commit dbf7717
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ require (
)

require (
cgt.name/pkg/go-mwclient v1.3.0 // indirect
github.com/antonholmquist/jason v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -43,6 +45,7 @@ require (
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mrjones/oauth v0.0.0-20190623134757-126b35219450 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_model v0.5.0 // indirect
Expand All @@ -51,7 +54,6 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
44 changes: 29 additions & 15 deletions internal/httpcontroller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package httpcontroller

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -14,7 +13,7 @@ import (
"sync"
"time"

"github.com/shurcooL/graphql"
"cgt.name/pkg/go-mwclient"
"github.com/tphakala/birdnet-go/internal/datastore"
)

Expand Down Expand Up @@ -242,30 +241,45 @@ func parseOffset(offsetStr string, defaultOffset int) int {
}

var (
client = graphql.NewClient("https://app.birdweather.com/graphql", nil)
thumbnailMap sync.Map
thumbnailMutexMap sync.Map
)

func queryGraphQL(scientificName string) (string, error) {
log.Printf("Fetching thumbnail for bird: %s\n", scientificName)
func queryWikiMedia(scientificName string) (string, error) {
w, err := mwclient.New("https://wikipedia.org/w/api.php", "Birdnet-Go")
if err != nil {
return "", err
}

var query struct {
Species struct {
ThumbnailUrl graphql.String
} `graphql:"species(scientificName: $scientificName)"`
// Specify parameters to send.
parameters := map[string]string{
"action": "query",
"prop": "pageimages",
"piprop": "thumbnail",
"pilicense": "free",
"titles": scientificName,
"pithumbsize": "400",
"redirects": "",
}

variables := map[string]interface{}{
"scientificName": graphql.String(scientificName),
// Make the request.
resp, err := w.Get(parameters)
if err != nil {
return "", err
}

// Print the *jason.Object
pages, err := resp.GetObjectArray("query", "pages")
if err != nil {
return "", err
}

err := client.Query(context.Background(), &query, variables)
thumbnail, err := pages[0].GetString("thumbnail", "source")
if err != nil {
return "", err
}

return string(query.Species.ThumbnailUrl), nil
return string(thumbnail), nil
}

// thumbnail returns the url of a given bird's thumbnail
Expand All @@ -289,9 +303,9 @@ func thumbnail(scientificName string) (string, error) {
return thumbnail.(string), nil
}

thumbn, err := queryGraphQL(scientificName)
thumbn, err := queryWikiMedia(scientificName)
if err != nil {
return "", fmt.Errorf("error querying GraphQL endpoint: %v", err)
log.Printf("error querying wikimedia endpoint: %v", err)
}

thumbnailMap.Store(scientificName, thumbn)
Expand Down

0 comments on commit dbf7717

Please sign in to comment.