package yandex import ( "errors" "fmt" "net/url" "strings" "github.com/karust/openserp/core" ) const baseURL = "https://www.yandex.com" // BuildURL builds a Yandex web search URL for the provided query and page // index. It returns an error when the resulting query text is empty. func BuildURL(q core.Query, page int) (string, error) { base, _ := url.Parse(baseURL) base.Path += "search/" params := url.Values{} if q.Text != "" || q.Site != "" || q.Filetype != "" { text := q.Text if q.Site != "" { text += " site:" + q.Site } if q.Filetype != "" { text += " mime:" + q.Filetype } if q.DateInterval != "" { text += " date:" + q.DateInterval } if locale := core.ParseLocale(q.LangCode); locale.Language != "" { // Yandex's lang: operator accepts the lowercase language subtag // only; region modifiers must go through the lr= parameter. text += " lang:" + locale.Language } params.Add("text", text) params.Add("p", fmt.Sprint(page)) } if len(params.Get("text")) == 0 { return "", errors.New("empty query built") } if lr := yandexLR(q.Region); lr != "" { params.Add("lr", lr) } base.RawQuery = params.Encode() return base.String(), nil } // BuildImageURL builds a Yandex image search URL for the provided query and // page index. It returns an error when the resulting query text is empty. func BuildImageURL(q core.Query, page int) (string, error) { // TODO: Add other parameters base, _ := url.Parse(baseURL) base.Path += "images/search/" params := url.Values{} if q.Text != "" { text := q.Text if q.DateInterval != "" { text += " date:" + q.DateInterval } params.Add("text", text) params.Add("p", fmt.Sprint(page)) } if len(params.Get("text")) == 0 { return "", errors.New("empty query built") } if q.Site != "" { params.Add("site", q.Site) } if q.Filetype != "" { params.Add("itype", q.Filetype) } if lr := yandexLR(q.Region); lr != "" { params.Add("lr", lr) } base.RawQuery = params.Encode() return base.String(), nil } func yandexLR(region string) string { region = strings.TrimSpace(region) if region == "" { return "" } for i := 0; i < len(region); i++ { if region[i] < '0' || region[i] > '9' { return "" } } return region }