types.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package backend
  2. import (
  3. "container/list"
  4. "encoding/json"
  5. "regexp"
  6. )
  7. const (
  8. JOB_RUNNING_EVENT_PROGRESS = 1
  9. JOB_RUNNING_EVENT_DEBUG = 0
  10. //动作执行结果
  11. RUN_ACTION_NOTRUN = 0 //未执行
  12. RUN_ACTION_SUCCESS = 1 //执行成功
  13. RUN_ACTION_ERROR = 2 //执行异常
  14. RUN_ACTION_TIMEOUT = 3 //执行超时
  15. //动作执行检查结果
  16. CHECK_ACTION_NOTCHECK = 0
  17. CHECK_ACTION_SUCCESS = 1
  18. CHECK_ACTION_ERROR = 2
  19. CHECK_ACTION_TIMEOUT = 3
  20. CHECH_RESULT = "true"
  21. )
  22. var (
  23. DataResults = map[string]*list.List{} //调试运行结果
  24. VerifyResults = map[string]*SpiderConfigVerifyResult{} //调试验证结果
  25. RegSpace = regexp.MustCompile("[\\s\u3000\u2003\u00a0]+")
  26. )
  27. type (
  28. //爬虫配置信息
  29. SpiderConfig struct {
  30. Site string `json:"site"`
  31. Channel string `json:"channel"`
  32. ModifyUser string `json:"modifyuser"`
  33. Href string `json:"href"`
  34. Code string `json:"code"`
  35. InitList []*Actions `json:"initList"`
  36. ListBodyCss string `json:"listBodyCss"` //用于判断是否翻页成功
  37. ListItemCss string `json:"listItemCss"`
  38. ListLinkCss string `json:"listLinkCss"`
  39. ListPubtimeCss string `json:"listPublishTimeCss"`
  40. ListNextPageCss string `json:"listNextPageCss"`
  41. TitleCss string `json:"titleCss"`
  42. PublishUnitCss string `json:"publishUnitCss"`
  43. PublishTimeCss string `json:"publishTimeCss"`
  44. ContentCss string `json:"contentCss"`
  45. AttachCss string `json:"attachCss"`
  46. ListJSCode string `json:"listJs"`
  47. ContentJSCode string `json:"contentJs"`
  48. AttachJSCode string `json:"attachJs"` //无效
  49. ListTurnPageJSCode string `json:"listTurnPageJs"`
  50. MaxPages int64 `json:"maxPages"`
  51. //延时
  52. ListDelayTime int64 `json:"listDelayTime"`
  53. ListTurnDelayTime int64 `json:"listTurnDelayTime"`
  54. ContentDelayTime int64 `json:"contentDelayTime"`
  55. }
  56. //附件链接
  57. AttachLink struct {
  58. Title string `json:"title"`
  59. Href string `json:"href"`
  60. FileName string `json:"fileName"`
  61. FileType string `json:"fileType"`
  62. FileSize string `json:"fileSize"`
  63. FilePath string `json:"filePath"`
  64. }
  65. Actions struct {
  66. ActionJs string `json:"actionJs"`
  67. CheckJs string `json:"checkJs"`
  68. SleepTime int64 `json:"sleepTime"`
  69. }
  70. //动作执行与检查
  71. ActionRunResult struct {
  72. ActionNum int `json:"actionNum"`
  73. ActionJs string `json:"actionJs`
  74. CheckJs string `json:"checkJs"`
  75. Result bool `json:"result"`
  76. RunResult int `json:"runResult"`
  77. CheckResult int `json:"checkResult"`
  78. }
  79. //爬取结果信息
  80. ResultItem struct {
  81. No int `json:"no"` //编号
  82. Site string `json:"site"`
  83. Channel string `json:"channel"`
  84. Href string `json:"href"`
  85. ListTitle string `json:"listTitle"`
  86. ListPubTime string `json:"listPublishTime"`
  87. Title string `json:"title"`
  88. PublishUnit string `json:"publishUnit"`
  89. PublishTime string `json:"publishTime"`
  90. Content string `json:"content"`
  91. ContentHtml string `json:"contentHtml"`
  92. AttachLinks []*AttachLink `json:"attachLinks"` //存放附件的标题,链接
  93. AttachJson string `json:"attachJson"` //存放附件的OSS元信息
  94. }
  95. //爬取结果信息清单
  96. ResultItems []*ResultItem
  97. SpiderConfiges []*SpiderConfig
  98. //
  99. JobItem struct {
  100. //code/site与爬虫配置一致
  101. SpiderCode string `json:"code"`
  102. SpiderSite string `json:"site"`
  103. //以下是运行参数(不配置时,默认使用通用配置)
  104. Channel string `json:"channel"`
  105. Url string `json:"url"`
  106. ProxyServe string `json:"proxyServe"`
  107. MaxPages int `json:"maxPages"`
  108. Threads int `json:"threads"`
  109. ListDelay int64 `json:"listDelay"`
  110. TrunPageDelay int64 `json:"trunPageDelay"`
  111. ContentDelay int64 `json:"contentDelay"`
  112. NeedDownloadAttaches bool `json:"needDownloadAttaches"`
  113. }
  114. //作业定义
  115. Job struct {
  116. Code string `json:"code"`
  117. Name string `json:"name"`
  118. Items []*JobItem `json:"items"`
  119. //通用参数
  120. ProxyServe string `json:"proxyServe"`
  121. MaxPages int `json:"maxPages"`
  122. Threads int `json:"threads"`
  123. ListDelay int64 `json:"listDelay"`
  124. TrunPageDelay int64 `json:"trunPageDelay"`
  125. ContentDelay int64 `json:"contentDelay"`
  126. State int `json:"state"`
  127. StateType string `json:"stateType"`
  128. Progress int `json:"progress"`
  129. NeedDownloadAttaches bool `json:"needDownloadAttaches"`
  130. }
  131. Jobs []*Job
  132. //推送事件
  133. JobRunningEvent struct {
  134. Progress int `json:"progress"`
  135. Msg string `json:"msg"`
  136. Act int `json:"act"`
  137. Code string `json:"code"`
  138. }
  139. //Job运行时状态,中间结果
  140. JobRunningState struct {
  141. Code string
  142. State int
  143. Progress int
  144. ResultCache *list.List //缓存
  145. ExitCh chan bool
  146. }
  147. //爬虫配置验证结果
  148. SpiderConfigVerifyResult struct {
  149. Title bool `json:"title"`
  150. PublishUnit bool `json:"publishUnit"`
  151. PublishTime bool `json:"publishTime"`
  152. Content bool `json:"content"`
  153. Attaches bool `json:"attaches"`
  154. ListItems bool `json:"listItems"`
  155. ListTrunPage bool `json:"listTrunPage"`
  156. }
  157. )
  158. func (sc SpiderConfiges) Len() int {
  159. return len(sc)
  160. }
  161. func (sc SpiderConfiges) Swap(i, j int) {
  162. sc[i], sc[j] = sc[j], sc[i]
  163. }
  164. func (sc SpiderConfiges) Less(i, j int) bool {
  165. return sc[i].Code > sc[j].Code
  166. }
  167. func (js Jobs) Len() int {
  168. return len(js)
  169. }
  170. func (js Jobs) Swap(i, j int) {
  171. js[i], js[j] = js[j], js[i]
  172. }
  173. func (js Jobs) Less(i, j int) bool {
  174. return js[i].Code > js[j].Code
  175. }
  176. // CopyAttribute
  177. func CopyAttribute(dst *string, value1, value2 string) {
  178. if value1 == "EMPTY" { //特殊符号,强制修改
  179. *dst = ""
  180. } else if value1 != "" {
  181. *dst = value1
  182. } else if value2 != "" {
  183. *dst = value2
  184. }
  185. }
  186. // NewSpiderConfig 生成css对象
  187. func NewSpiderConfig(cssmark map[string]interface{}) (*SpiderConfig, error) {
  188. sc := &SpiderConfig{}
  189. cssmark_byte, err := json.Marshal(cssmark)
  190. if err != nil {
  191. return nil, err
  192. }
  193. err = json.Unmarshal(cssmark_byte, &sc)
  194. return sc, err
  195. //param_common := param["param_common"].(map[string]interface{})
  196. //css_list, _ := param["css_list"].(map[string]interface{})
  197. //css_content, _ := param["css_content"].(map[string]interface{})
  198. //js_list, _ := param["js_list"].(map[string]interface{})
  199. //js_nextpage, _ := param["js_nextpage"].(map[string]interface{})
  200. //js_content, _ := param["js_content"].(map[string]interface{})
  201. //sc := &SpiderConfig{
  202. // Site: qu.ObjToString(param_common["site"]),
  203. // Channel: qu.ObjToString(param_common["channel"]),
  204. // Modifyuser: qu.ObjToString(param_common["modifyuser"]),
  205. // Href: qu.ObjToString(param_common["href"]),
  206. // Code: qu.ObjToString(param_common["code"]),
  207. // ListBodyCss: qu.ObjToString(css_list["body"]),
  208. // ListItemCss: qu.ObjToString(css_list["title"]),
  209. // ListLinkCss: qu.ObjToString(css_list["href"]),
  210. // ListPubtimeCss: qu.ObjToString(css_list["ptime"]),
  211. // ListNextPageCss: qu.ObjToString(css_list["nextpage"]),
  212. // TitleCss: qu.ObjToString(css_content["title"]),
  213. // PublishUnitCss: qu.ObjToString(css_content["source"]),
  214. // PublishTimeCss: qu.ObjToString(css_content["ptime"]),
  215. // ContentCss: qu.ObjToString(css_content["content"]),
  216. // AttachCss: qu.ObjToString(css_content["file"]),
  217. // ListJSCode: qu.ObjToString(js_list["js"]),
  218. // ContentJSCode: qu.ObjToString(js_content["js"]),
  219. // ListTrunPageJSCode: qu.ObjToString(js_nextpage["js"]),
  220. // //AttachJSCode : string `json:"attachJs"` //无效
  221. //}
  222. }
  223. // MergeSpiderConfig 合并
  224. func MergeSpiderConfig(src1, src2 *SpiderConfig) *SpiderConfig {
  225. nsc := new(SpiderConfig)
  226. CopyAttribute(&nsc.Code, src2.Code, src1.Code)
  227. CopyAttribute(&nsc.Site, src2.Site, src1.Site)
  228. CopyAttribute(&nsc.Channel, src2.Channel, src1.Channel)
  229. CopyAttribute(&nsc.Href, src2.Href, src1.Href)
  230. CopyAttribute(&nsc.ModifyUser, src2.ModifyUser, src1.ModifyUser)
  231. CopyAttribute(&nsc.ListBodyCss, src2.ListBodyCss, src1.ListBodyCss)
  232. CopyAttribute(&nsc.ListItemCss, src2.ListItemCss, src1.ListItemCss)
  233. CopyAttribute(&nsc.ListLinkCss, src2.ListLinkCss, src1.ListLinkCss)
  234. CopyAttribute(&nsc.ListPubtimeCss, src2.ListPubtimeCss, src1.ListPubtimeCss)
  235. CopyAttribute(&nsc.ListNextPageCss, src2.ListNextPageCss, src1.ListNextPageCss)
  236. CopyAttribute(&nsc.TitleCss, src2.TitleCss, src1.TitleCss)
  237. CopyAttribute(&nsc.PublishTimeCss, src2.PublishTimeCss, src1.PublishTimeCss)
  238. CopyAttribute(&nsc.PublishUnitCss, src2.PublishUnitCss, src1.PublishUnitCss)
  239. CopyAttribute(&nsc.ContentCss, src2.ContentCss, src1.ContentCss)
  240. CopyAttribute(&nsc.AttachCss, src2.AttachCss, src1.AttachCss)
  241. CopyAttribute(&nsc.ListJSCode, src2.ListJSCode, src1.ListJSCode)
  242. CopyAttribute(&nsc.ContentJSCode, src2.ContentJSCode, src1.ContentJSCode)
  243. CopyAttribute(&nsc.AttachJSCode, src2.AttachJSCode, src1.AttachJSCode)
  244. CopyAttribute(&nsc.ListTurnPageJSCode, src2.ListTurnPageJSCode, src1.ListTurnPageJSCode)
  245. return nsc
  246. }
  247. // CopySpiderConfig 复制
  248. func CopySpiderConfig(src1, src2 *SpiderConfig) {
  249. src1.Code = src2.Code
  250. src1.Site = src2.Site
  251. src1.ModifyUser = src2.ModifyUser
  252. src1.Channel = src2.Channel
  253. src1.Href = src2.Href
  254. src1.ListBodyCss = src2.ListBodyCss
  255. src1.ListItemCss = src2.ListItemCss
  256. src1.ListPubtimeCss = src2.ListPubtimeCss
  257. src1.ListNextPageCss = src2.ListNextPageCss
  258. src1.ListLinkCss = src2.ListLinkCss
  259. src1.TitleCss = src2.TitleCss
  260. src1.PublishTimeCss = src2.PublishTimeCss
  261. src1.PublishUnitCss = src2.PublishUnitCss
  262. src1.ContentCss = src2.ContentCss
  263. src1.AttachCss = src2.AttachCss
  264. src1.ListJSCode = src2.ListJSCode
  265. src1.ListTurnPageJSCode = src2.ListTurnPageJSCode
  266. src1.ContentJSCode = src2.ContentJSCode
  267. src1.AttachJSCode = src2.AttachJSCode
  268. }