lic.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package lic
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. model "go-common/app/job/main/tv/model/pgc"
  6. "math/rand"
  7. "net/url"
  8. "time"
  9. )
  10. const (
  11. letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
  12. _serviceID = "dataSync"
  13. )
  14. // BuildLic builds the skeleton of a license
  15. func BuildLic(sign string, ps []*model.PS, count int) *model.License {
  16. var (
  17. tid = RandStringBytesRmndr(32)
  18. now = time.Now()
  19. )
  20. return &model.License{
  21. TId: tid,
  22. InputTime: now.Format("20060102"),
  23. Sign: sign,
  24. XMLData: &model.XMLData{
  25. Service: &model.Service{
  26. ID: _serviceID,
  27. Head: &model.Head{
  28. TradeID: tid,
  29. Date: now.Format("2006-01-02"),
  30. Count: count,
  31. },
  32. Body: &model.Body{
  33. ProgramSetList: &model.PSList{
  34. ProgramSet: ps,
  35. },
  36. },
  37. },
  38. },
  39. }
  40. }
  41. // RandStringBytesRmndr generates an random string
  42. func RandStringBytesRmndr(n int) string {
  43. b := make([]byte, n)
  44. for i := range b {
  45. b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
  46. }
  47. return string(b)
  48. }
  49. // DelLic creates the license message with only the Season ID, for deletion
  50. func DelLic(sign string, prefix string, sid int64) *model.License {
  51. var (
  52. ps []*model.PS
  53. programS = &model.PS{
  54. ProgramSetID: fmt.Sprintf("%s%d", prefix, sid),
  55. }
  56. )
  57. ps = append(ps, programS)
  58. return BuildLic(sign, ps, 1)
  59. }
  60. // DelEpLic creates the license message with only the Ep IDs, for deletion
  61. func DelEpLic(prefix string, sign string, delEps []int) string {
  62. // message skeleton
  63. var tid = RandStringBytesRmndr(32)
  64. type Service struct {
  65. ID string `xml:"id,attr"`
  66. Head *model.Head
  67. Body *model.DelBody `xml:"Body"`
  68. }
  69. Msg := &Service{
  70. ID: _serviceID,
  71. Head: &model.Head{
  72. TradeID: tid,
  73. Date: time.Now().Format("2006-01-02"),
  74. Count: len(delEps),
  75. },
  76. Body: &model.DelBody{
  77. ProgramList: &model.ProgramList{},
  78. },
  79. }
  80. for _, v := range delEps {
  81. pm := &model.Program{
  82. ProgramID: fmt.Sprintf("%s%d", prefix, v),
  83. }
  84. Msg.Body.ProgramList.Program = append(Msg.Body.ProgramList.Program, pm)
  85. }
  86. // combine the xml message
  87. xmlRes, _ := xml.MarshalIndent(Msg, " ", " ")
  88. params := url.Values{}
  89. params.Set("tId", tid)
  90. params.Set("inputTime", time.Now().Format("20060102"))
  91. params.Set("sign", sign)
  92. body := params.Encode()
  93. body = body + "&xmlData=<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + string(xmlRes)
  94. return body
  95. }