other.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "net/url"
  4. "strings"
  5. "sync"
  6. "time"
  7. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  8. "github.com/IceWhaleTech/CasaOS/model"
  9. "github.com/go-resty/resty/v2"
  10. "github.com/tidwall/gjson"
  11. "go.uber.org/zap"
  12. )
  13. type OtherService interface {
  14. Search(key string) ([]model.SearchEngine, error)
  15. AgentSearch(url string) ([]byte, error)
  16. }
  17. type otherService struct{}
  18. func (s *otherService) Search(key string) ([]model.SearchEngine, error) {
  19. engines := []model.SearchEngine{}
  20. engines = append(engines, model.SearchEngine{
  21. Name: "bing",
  22. Icon: "https://files.codelife.cc/itab/search/bing.svg",
  23. SearchUrl: "https://www.bing.com/search?q=",
  24. RecoUrl: "https://www.bing.com/osjson.aspx?query=", // + keyword
  25. }, model.SearchEngine{
  26. Name: "google",
  27. Icon: "https://files.codelife.cc/itab/search/google.svg",
  28. SearchUrl: "https://www.google.com/search?q=",
  29. RecoUrl: "https://www.google.com/complete/search?client=gws-wiz&xssi=t&hl=en-US&authuser=0&dpr=1&q=", // + keyword
  30. }, model.SearchEngine{
  31. Name: "baidu",
  32. Icon: "https://files.codelife.cc/itab/search/baidu.svg",
  33. SearchUrl: "https://www.baidu.com/s?wd=",
  34. RecoUrl: "https://www.baidu.com/sugrec?json=1&prod=pc&wd=", // + keyword
  35. }, model.SearchEngine{
  36. Name: "duckduckgo",
  37. Icon: "https://files.codelife.cc/itab/search/duckduckgo.svg",
  38. SearchUrl: "https://duckduckgo.com/?q=",
  39. RecoUrl: "https://duckduckgo.com/ac/?type=list&q=", // + keyword
  40. }, model.SearchEngine{
  41. Name: "startpage",
  42. Icon: "https://www.startpage.com/sp/cdn/favicons/apple-touch-icon-60x60--default.png",
  43. SearchUrl: "https://www.startpage.com/do/search?q=",
  44. RecoUrl: "https://www.startpage.com/suggestions?segment=startpage.udog&lui=english&q=", // + keyword
  45. })
  46. client := resty.New()
  47. client.SetTimeout(3 * time.Second) // 设置全局超时时间
  48. var wg sync.WaitGroup
  49. for i := 0; i < len(engines); i++ {
  50. wg.Add(1)
  51. go func(i int, k string) {
  52. name := engines[i].Name
  53. url := engines[i].RecoUrl + url.QueryEscape(k)
  54. defer wg.Done()
  55. resp, err := client.R().Get(url)
  56. if err != nil {
  57. logger.Error("Then get search result error: %v", zap.Error(err), zap.String("name", name), zap.String("url", url))
  58. return
  59. }
  60. res := []string{}
  61. if name == "bing" {
  62. r := gjson.Get(resp.String(), "1")
  63. for _, v := range r.Array() {
  64. res = append(res, v.String())
  65. }
  66. } else if name == "google" {
  67. r := gjson.Get(strings.Replace(resp.String(), ")]}'", "", 1), "0.#.0")
  68. for _, v := range r.Array() {
  69. res = append(res, strings.ReplaceAll(strings.ReplaceAll(v.String(), "<b>", " "), "</b>", ""))
  70. }
  71. } else if name == "baidu" {
  72. r := gjson.Get(resp.String(), "g.#.q")
  73. for _, v := range r.Array() {
  74. res = append(res, v.String())
  75. }
  76. } else if name == "duckduckgo" {
  77. r := gjson.Get(resp.String(), "1")
  78. for _, v := range r.Array() {
  79. res = append(res, v.String())
  80. }
  81. } else if name == "startpage" {
  82. r := gjson.Get(resp.String(), "suggestions.#.text")
  83. for _, v := range r.Array() {
  84. res = append(res, v.String())
  85. }
  86. }
  87. engines[i].Data = res
  88. }(i, key)
  89. }
  90. wg.Wait()
  91. return engines, nil
  92. }
  93. func (s *otherService) AgentSearch(url string) ([]byte, error) {
  94. client := resty.New()
  95. client.SetTimeout(3 * time.Second) // 设置全局超时时间
  96. resp, err := client.R().Get(url)
  97. if err != nil {
  98. logger.Error("Then get search result error: %v", zap.Error(err), zap.String("url", url))
  99. return nil, err
  100. }
  101. return resp.Body(), nil
  102. }
  103. func NewOtherService() OtherService {
  104. return &otherService{}
  105. }