Skip to content

Commit

Permalink
added a second Google search result pattern to parse image urls from …
Browse files Browse the repository at this point in the history
…Google

It now uses two regexp
  • Loading branch information
Ivan Nariño Del Castillo authored and Ivan Nariño Del Castillo committed Aug 2, 2016
1 parent b739476 commit eae9b5d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit eae9b5d

Please sign in to comment.