dataplatform.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package dataplatform
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. xhttp "net/http"
  8. "net/url"
  9. "sort"
  10. "strings"
  11. "time"
  12. "go-common/app/job/main/growup/model"
  13. income "go-common/app/job/main/growup/model/income"
  14. "go-common/library/conf/env"
  15. "go-common/library/log"
  16. )
  17. var (
  18. signParams = []string{"appKey", "timestamp", "version"}
  19. _userAgent = "User-Agent"
  20. )
  21. func (d *Dao) setParams() url.Values {
  22. params := url.Values{}
  23. params.Set("appKey", d.c.DPClient.Key)
  24. params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
  25. params.Set("version", "1.0")
  26. params.Set("signMethod", "md5")
  27. return params
  28. }
  29. // GetArchiveByMID get archive id by mid
  30. func (d *Dao) GetArchiveByMID(c context.Context, query string) (ids []int64, err error) {
  31. ids = make([]int64, 0)
  32. params := d.setParams()
  33. params.Set("query", query)
  34. var res struct {
  35. Code int `json:"code"`
  36. Result []*model.ArchiveID `json:"result"`
  37. }
  38. if err = d.NewRequest(c, d.url, "", params, &res); err != nil {
  39. log.Error("dataplatform.send NewRequest url(%s) error(%v)", d.url+"?"+params.Encode(), err)
  40. return
  41. }
  42. if res.Code != 200 {
  43. log.Error("dateplatform.send NewRequest error code:%d ; url(%s) ", res.Code, d.url+"?"+params.Encode())
  44. return
  45. }
  46. if res.Code == 200 && len(res.Result) > 0 {
  47. for _, archive := range res.Result {
  48. ids = append(ids, archive.ID)
  49. }
  50. }
  51. return
  52. }
  53. // Send ...
  54. func (d *Dao) Send(c context.Context, query string) (infos []*model.ArchiveInfo, err error) {
  55. log.Info("dateplatform Send start")
  56. params := url.Values{}
  57. params.Set("appKey", d.c.DPClient.Key)
  58. params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
  59. params.Set("version", "1.0")
  60. params.Set("signMethod", "md5")
  61. params.Set("query", query)
  62. var res struct {
  63. Code int `json:"code"`
  64. Result []*model.ArchiveInfo `json:"result"`
  65. }
  66. if err = d.NewRequest(c, d.url, "", params, &res); err != nil {
  67. log.Error("dataplatform.send NewRequest url(%s) error(%v)", d.url+"?"+params.Encode(), err)
  68. return
  69. }
  70. if res.Code != 200 {
  71. log.Error("dateplatform.send NewRequest error code:%d ; url(%s) ", res.Code, d.url+"?"+params.Encode())
  72. return
  73. }
  74. if res.Code == 200 && len(res.Result) > 0 {
  75. infos = res.Result
  76. }
  77. return
  78. }
  79. // SendSpyRequest send.
  80. func (d *Dao) SendSpyRequest(c context.Context, query string) (infos []*model.Spy, err error) {
  81. log.Info("dateplatform Send start")
  82. params := url.Values{}
  83. params.Set("appKey", d.c.DPClient.Key)
  84. params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
  85. params.Set("version", "1.0")
  86. params.Set("signMethod", "md5")
  87. params.Set("query", query)
  88. var res struct {
  89. Code int `json:"code"`
  90. Result []*model.Spy `json:"result"`
  91. }
  92. if err = d.NewRequest(c, d.spyURL, "", params, &res); err != nil {
  93. log.Error("dataplatform.SendSpyRequest NewRequest url(%s) error(%v)", d.spyURL+"?"+params.Encode(), err)
  94. return
  95. }
  96. if res.Code != 200 {
  97. log.Error("dateplatform.SendSpyRequest NewRequest error code:%d ; url(%s) ", res.Code, d.spyURL+"?"+params.Encode())
  98. return
  99. }
  100. if res.Code == 200 && len(res.Result) > 0 {
  101. infos = res.Result
  102. }
  103. return
  104. }
  105. // SendBGMRequest get bgm infos
  106. func (d *Dao) SendBGMRequest(c context.Context, query string) (infos []*income.BGM, err error) {
  107. params := url.Values{}
  108. params.Set("appKey", d.c.DPClient.Key)
  109. params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
  110. params.Set("version", "1.0")
  111. params.Set("signMethod", "md5")
  112. params.Set("query", query)
  113. var res struct {
  114. Code int `json:"code"`
  115. Result []*income.BGM `json:"result"`
  116. }
  117. if err = d.NewRequest(c, d.bgmURL, "", params, &res); err != nil {
  118. log.Error("dataplatform.SendBGMRequest NewRequest url(%s) error(%v)", d.bgmURL+"?"+params.Encode(), err)
  119. return
  120. }
  121. if res.Code != 200 {
  122. log.Error("dateplatform.SendBGMRequest NewRequest error code:%d ; url(%s) ", res.Code, d.bgmURL+"?"+params.Encode())
  123. return
  124. }
  125. if res.Code == 200 && len(res.Result) > 0 {
  126. infos = res.Result
  127. }
  128. return
  129. }
  130. // SendBasicDataRequest get basic data status
  131. func (d *Dao) SendBasicDataRequest(c context.Context, query string) (ok bool, err error) {
  132. params := url.Values{}
  133. params.Set("appKey", d.c.DPClient.Key)
  134. params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
  135. params.Set("version", "1.0")
  136. params.Set("signMethod", "md5")
  137. params.Set("query", query)
  138. var res struct {
  139. Code int `json:"code"`
  140. Result []*struct {
  141. Stat int `json:"stat"`
  142. } `json:"result"`
  143. }
  144. url := d.basicURL
  145. if err = d.NewRequest(c, url, "", params, &res); err != nil {
  146. log.Error("SendBasicDataRequest NewRequest url(%s) error(%v)", url+"?"+params.Encode(), err)
  147. return
  148. }
  149. if res.Code != 200 {
  150. log.Error("SendBasicDataRequest NewRequest error code:%d ; url(%s) ", res.Code, url+"?"+params.Encode())
  151. return
  152. }
  153. if res.Code == 200 && len(res.Result) > 0 {
  154. info := res.Result[0]
  155. if info.Stat == 1 {
  156. ok = true
  157. }
  158. }
  159. return
  160. }
  161. // NewRequest new http request with method, url, ip, values and headers.
  162. func (d *Dao) NewRequest(c context.Context, url, realIP string, params url.Values, res interface{}) (err error) {
  163. enc, err := d.sign(params)
  164. if err != nil {
  165. log.Error("url:%s,params:%v", url, params)
  166. return
  167. }
  168. if enc != "" {
  169. url = url + "?" + enc
  170. }
  171. req, err := xhttp.NewRequest(xhttp.MethodGet, url, nil)
  172. if err != nil {
  173. log.Error("method:%s,url:%s", xhttp.MethodGet, url)
  174. return
  175. }
  176. req.Header.Set(_userAgent, "haoguanwei@bilibili.com "+env.AppID)
  177. if err != nil {
  178. return
  179. }
  180. return d.client.Do(c, req, res)
  181. }
  182. // sign calc appkey and appsecret sign.
  183. func (d *Dao) sign(params url.Values) (query string, err error) {
  184. tmp := params.Encode()
  185. signTmp := d.encode(params)
  186. if strings.IndexByte(tmp, '+') > -1 {
  187. tmp = strings.Replace(tmp, "+", "%20", -1)
  188. }
  189. var b bytes.Buffer
  190. b.WriteString(d.c.DPClient.Secret)
  191. b.WriteString(signTmp)
  192. b.WriteString(d.c.DPClient.Secret)
  193. mh := md5.Sum(b.Bytes())
  194. // query
  195. var qb bytes.Buffer
  196. qb.WriteString(tmp)
  197. qb.WriteString("&sign=")
  198. qb.WriteString(strings.ToUpper(hex.EncodeToString(mh[:])))
  199. query = qb.String()
  200. return
  201. }
  202. // Encode encodes the values into ``URL encoded'' form
  203. // ("bar=baz&foo=quux") sorted by key.
  204. func (d *Dao) encode(v url.Values) string {
  205. if v == nil {
  206. return ""
  207. }
  208. var buf bytes.Buffer
  209. keys := make([]string, 0, len(v))
  210. for k := range v {
  211. keys = append(keys, k)
  212. }
  213. sort.Strings(keys)
  214. for _, k := range keys {
  215. found := false
  216. for _, p := range signParams {
  217. if p == k {
  218. found = true
  219. break
  220. }
  221. }
  222. if !found {
  223. continue
  224. }
  225. vs := v[k]
  226. prefix := k
  227. for _, v := range vs {
  228. buf.WriteString(prefix)
  229. buf.WriteString(v)
  230. }
  231. }
  232. return buf.String()
  233. }