model.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package api
  2. import (
  3. "hash/crc32"
  4. "strconv"
  5. "strings"
  6. "go-common/library/time"
  7. )
  8. // 各属性地址见 http://syncsvn.bilibili.co/platform/doc/blob/master/archive/field/state.md
  9. // all const
  10. const (
  11. // open state
  12. StateOpen = 0
  13. StateOrange = 1
  14. AccessMember = int32(10000)
  15. // forbid state
  16. StateForbidWait = -1
  17. StateForbidRecicle = -2
  18. StateForbidPolice = -3
  19. StateForbidLock = -4
  20. StateForbidFixed = -6
  21. StateForbidLater = -7
  22. StateForbidAdminDelay = -10
  23. StateForbidXcodeFail = -16
  24. StateForbidSubmit = -30
  25. StateForbidUserDelay = -40
  26. StateForbidUpDelete = -100
  27. // copyright
  28. CopyrightUnknow = int8(0)
  29. CopyrightOriginal = int8(1)
  30. CopyrightCopy = int8(2)
  31. // attribute yes and no
  32. AttrYes = int32(1)
  33. AttrNo = int32(0)
  34. // attribute bit
  35. AttrBitNoRank = uint(0)
  36. AttrBitNoDynamic = uint(1)
  37. AttrBitNoWeb = uint(2)
  38. AttrBitNoMobile = uint(3)
  39. // AttrBitNoSearch = uint(4)
  40. AttrBitOverseaLock = uint(5)
  41. AttrBitNoRecommend = uint(6)
  42. AttrBitNoReprint = uint(7)
  43. AttrBitHasHD5 = uint(8)
  44. AttrBitIsPGC = uint(9)
  45. AttrBitAllowBp = uint(10)
  46. AttrBitIsBangumi = uint(11)
  47. AttrBitIsPorder = uint(12)
  48. AttrBitLimitArea = uint(13)
  49. AttrBitAllowTag = uint(14)
  50. // AttrBitIsFromArcApi = uint(15)
  51. AttrBitJumpUrl = uint(16)
  52. AttrBitIsMovie = uint(17)
  53. AttrBitBadgepay = uint(18)
  54. AttrBitUGCPay = uint(22)
  55. AttrBitHasBGM = uint(23)
  56. AttrBitIsCooperation = uint(24)
  57. AttrBitHasViewpoint = uint(25)
  58. AttrBitHasArgument = uint(26)
  59. )
  60. var (
  61. _emptyTags = []string{}
  62. )
  63. // IsNormal is
  64. func (a *Arc) IsNormal() bool {
  65. return a.State >= StateOpen || a.State == StateForbidFixed
  66. }
  67. // RegionArc RegionArc
  68. type RegionArc struct {
  69. Aid int64
  70. Attribute int32
  71. Copyright int8
  72. PubDate time.Time
  73. }
  74. // AllowShow AllowShow
  75. func (ra *RegionArc) AllowShow() bool {
  76. return ra.attrVal(AttrBitNoWeb) == AttrNo && ra.attrVal(AttrBitNoMobile) == AttrNo
  77. }
  78. func (ra *RegionArc) attrVal(bit uint) int32 {
  79. return (ra.Attribute >> bit) & int32(1)
  80. }
  81. // AttrVal get attr val by bit.
  82. func (a *Arc) AttrVal(bit uint) int32 {
  83. return (a.Attribute >> bit) & int32(1)
  84. }
  85. // FillDimension is
  86. func (a *Arc) FillDimension(d string) {
  87. if d == "" || d == "0,0,0" {
  88. return
  89. }
  90. ds := strings.Split(d, ",")
  91. if len(ds) != 3 {
  92. return
  93. }
  94. var (
  95. width, height, rotate int64
  96. err error
  97. )
  98. if width, err = strconv.ParseInt(ds[0], 10, 64); err != nil {
  99. return
  100. }
  101. if height, err = strconv.ParseInt(ds[1], 10, 64); err != nil {
  102. return
  103. }
  104. if rotate, err = strconv.ParseInt(ds[2], 10, 64); err != nil {
  105. return
  106. }
  107. a.Dimension = Dimension{
  108. Width: width,
  109. Height: height,
  110. Rotate: rotate,
  111. }
  112. }
  113. // FillDimension is
  114. func (v *Page) FillDimension(d string) {
  115. if d == "" || d == "0,0,0" {
  116. return
  117. }
  118. ds := strings.Split(d, ",")
  119. if len(ds) != 3 {
  120. return
  121. }
  122. var (
  123. width, height, rotate int64
  124. err error
  125. )
  126. if width, err = strconv.ParseInt(ds[0], 10, 64); err != nil {
  127. return
  128. }
  129. if height, err = strconv.ParseInt(ds[1], 10, 64); err != nil {
  130. return
  131. }
  132. if rotate, err = strconv.ParseInt(ds[2], 10, 64); err != nil {
  133. return
  134. }
  135. v.Dimension = Dimension{
  136. Width: width,
  137. Height: height,
  138. Rotate: rotate,
  139. }
  140. }
  141. // Fill file archive some field.
  142. func (a *Arc) Fill() {
  143. a.Tags = _emptyTags
  144. a.Pic = coverURL(a.Pic)
  145. a.Rights.Bp = a.AttrVal(AttrBitAllowBp)
  146. a.Rights.Movie = a.AttrVal(AttrBitIsMovie)
  147. a.Rights.Pay = a.AttrVal(AttrBitBadgepay)
  148. a.Rights.HD5 = a.AttrVal(AttrBitHasHD5)
  149. a.Rights.NoReprint = a.AttrVal(AttrBitNoReprint)
  150. a.Rights.UGCPay = a.AttrVal(AttrBitUGCPay)
  151. a.Rights.IsCooperation = a.AttrVal(AttrBitIsCooperation)
  152. if a.FirstCid == 0 ||
  153. a.Access == AccessMember ||
  154. a.AttrVal(AttrBitIsPGC) == AttrYes ||
  155. a.AttrVal(AttrBitAllowBp) == AttrYes ||
  156. a.AttrVal(AttrBitBadgepay) == AttrYes ||
  157. a.AttrVal(AttrBitOverseaLock) == AttrYes ||
  158. a.AttrVal(AttrBitUGCPay) == AttrYes ||
  159. a.AttrVal(AttrBitLimitArea) == AttrYes {
  160. return
  161. }
  162. a.Rights.Autoplay = 1
  163. }
  164. // coverURL convert cover url to full url.
  165. func coverURL(uri string) (cover string) {
  166. if uri == "" {
  167. cover = "http://static.hdslb.com/images/transparent.gif"
  168. return
  169. }
  170. cover = uri
  171. if strings.Index(uri, "http://") == 0 {
  172. return
  173. }
  174. if len(uri) >= 10 && uri[:10] == "/templets/" {
  175. return
  176. }
  177. if strings.HasPrefix(uri, "group1") {
  178. cover = "http://i0.hdslb.com/" + uri
  179. return
  180. }
  181. if pos := strings.Index(uri, "/uploads/"); pos != -1 && (pos == 0 || pos == 3) {
  182. cover = uri[pos+8:]
  183. }
  184. cover = strings.Replace(cover, "{IMG}", "", -1)
  185. cover = "http://i" + strconv.FormatInt(int64(crc32.ChecksumIEEE([]byte(cover)))%3, 10) + ".hdslb.com" + cover
  186. return
  187. }
  188. // FillStat file stat, check access.
  189. func (a *Arc) FillStat() {
  190. if a.Access > 0 {
  191. a.Stat.View = 0
  192. }
  193. }
  194. // PlayerInfo player info
  195. type PlayerInfo struct {
  196. Cid int64 `json:"cid"`
  197. ExpireTime int64 `json:"expire_time,omitempty"`
  198. FileInfo map[int][]*PlayerFileInfo `json:"file_info"`
  199. SupportQuality []int `json:"support_quality"`
  200. SupportFormats []string `json:"support_formats"`
  201. SupportDescription []string `json:"support_description"`
  202. Quality int `json:"quality"`
  203. URL string `json:"url,omitempty"`
  204. VideoCodecid uint32 `json:"video_codecid"`
  205. VideoProject bool `json:"video_project"`
  206. Fnver int `json:"fnver"`
  207. Fnval int `json:"fnval"`
  208. Dash *ResponseDash `json:"dash,omitempty"`
  209. }
  210. // PlayerFileInfo is
  211. type PlayerFileInfo struct {
  212. TimeLength int64 `json:"timelength"`
  213. FileSize int64 `json:"filesize"`
  214. Ahead string `json:"ahead,omitempty"`
  215. Vhead string `json:"vhead,omitempty"`
  216. URL string `json:"url,omitempty"`
  217. Order int64 `json:"order,omitempty"`
  218. }