diff options
| author | Julian Hurst <julian.hurst92@gmail.com> | 2021-01-09 23:30:12 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst92@gmail.com> | 2021-01-09 23:30:12 +0100 |
| commit | 8f3ebc172d4930953b67f3c8acfd00bfa825f4de (patch) | |
| tree | 78d087decefd43abe641c49bd25691b1388131ad | |
| parent | 9b9cfc7555277ceab79805933b57ae82c9750d01 (diff) | |
| download | ytparser-8f3ebc172d4930953b67f3c8acfd00bfa825f4de.tar.gz | |
Add search order support
| -rw-r--r-- | ytparser.go | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/ytparser.go b/ytparser.go index 8edbf6a..df688dc 100644 --- a/ytparser.go +++ b/ytparser.go @@ -169,9 +169,9 @@ func PrintItems(items []Item, format string) { } } -func request(query string, page int, lang string) (string, error) { +func request(query string, page int, lang string, order string) (string, error) { q := url.QueryEscape(query) - url := fmt.Sprintf("https://www.youtube.com/results?search_query=%s&page=%d&hl=%s", q, page, lang) + url := fmt.Sprintf("https://www.youtube.com/results?search_query=%s&page=%d&hl=%s&sp=%s", q, page, lang, order) res, err := http.DefaultClient.Get(url) if err != nil { return "", err @@ -191,6 +191,8 @@ func request(query string, page int, lang string) (string, error) { pattern := regexp.MustCompile(`; *(\n|<\/script>)`) loc := pattern.FindIndex(startData) startData = startData[:loc[0]] + //fmt.Printf("%s\n", string(startData)) + //os.Exit(0) return string(startData), nil } @@ -198,26 +200,44 @@ func isValidData(data string) bool { return data != "" } -// Launch a search on the given query, page and language and return an array of -// items and/or an error. +func translateOrder(order string) string { + switch order { + case "relevance": + return "CAASAhAB" + case "date": + return "CAISAhAB" + case "views": + return "CAMSAhAB" + case "rating": + return "CAASAhAB" + default: + return "CAESAhAB" + } +} + +// Launch a search on the given query, page, language and order and return an +// array of items and/or an error. // // The lang parameter must be a youtube supported language code ("en", "fr", // "de"...) and allows getting certain information such as the published date // in the selected language. If empty, youtube should detect the language based // on location. // +// The order parameter can be any of the following: relevance, date, views, +// rating. The default value is relevance. +// // This function may return items even if there is an error, allowing the // search to be considered partially successful. // If this is the case it most likely means there was an error during the // parse, but some items were still successfully parsed before the error. -func Search(query string, page int, lang string) ([]Item, error) { +func Search(query string, page int, lang string, order string) ([]Item, error) { var data string = "" var err error for i := 1; i < 4 && !isValidData(data); i++ { if i > 1 { fmt.Fprintf(os.Stderr, "Yt data invalid, retrying (attempt %d)\n", i) } - data, err = request(query, page, lang) + data, err = request(query, page, lang, translateOrder(order)) if err != nil { return nil, err } |
