From eae9b5d3c1b7cd56a525f5b45f6ff8ed8b3dbcd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Nari=C3=B1o=20Del=20Castillo?= Date: Tue, 2 Aug 2016 02:58:20 +0200 Subject: [PATCH] added a second Google search result pattern to parse image urls from Google It now uses two regexp --- download.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/download.go b/download.go index 4f6aaee..0577144 100644 --- a/download.go +++ b/download.go @@ -14,6 +14,9 @@ import ( // matching, and the other requires an API key limited to 100 searches a day. const googleSearchFormat = `https://www.google.com.br/search?tbs=isz%3Aex%2Ciszw%3A460%2Ciszh%3A215&tbm=isch&num=5&q=` +// Possible Google result formats +var googleSearchResultPatterns = []string{`imgurl=(.+?\.(jpg|png))&imgrefurl=`, `\"ou\":\"(.+?)\",\"`} + // Returns the first steam grid image URL found by Google search of a given // game name. func getGoogleImage(gameName string) (string, error) { @@ -45,10 +48,13 @@ func getGoogleImage(gameName string) (string, error) { } response.Body.Close() - pattern := regexp.MustCompile(`imgurl=(.+?\.(jpg|png))&imgrefurl=`) - matches := pattern.FindStringSubmatch(string(responseBytes)) - if len(matches) >= 1 { - return matches[1], nil + for _, googleSearchResultPattern := range googleSearchResultPatterns { + pattern := regexp.MustCompile(googleSearchResultPattern) + matches := pattern.FindStringSubmatch(string(responseBytes)) + + if len(matches) >= 1 { + return matches[1], nil + } } return "", nil }