storage.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // /*
  2. // 存储;
  3. // 暂时提供3种存储方式:
  4. // 1)控制台
  5. // 2) MongoDb
  6. // 3) 文件
  7. // */
  8. package db
  9. // import (
  10. // "bytes"
  11. // "context"
  12. // "encoding/json"
  13. // "fmt"
  14. // "io"
  15. // "log"
  16. // "net/http"
  17. // "time"
  18. // "github.com/bmaupin/go-epub"
  19. // "github.com/xuri/excelize/v2"
  20. // "go.mongodb.org/mongo-driver/mongo"
  21. // "go.mongodb.org/mongo-driver/mongo/options"
  22. // )
  23. // type (
  24. // //Storage
  25. // Storage interface {
  26. // //保存数据
  27. // Save(siteName, spiderCode, siteChannelName, siteChannelUrl string, data map[string]interface{}) error
  28. // //SaveUp(query string, data map[string]interface{}) error
  29. // }
  30. // //MongoStorage
  31. // MongoStorage struct {
  32. // c *mongo.Collection
  33. // }
  34. // //调试模式下的数据存储
  35. // ConsoleStorage struct {
  36. // }
  37. // //FileStorage
  38. // FileStorage struct {
  39. // f io.WriteCloser
  40. // }
  41. // //企业微信机器人消息
  42. // WeChatMsgStorage struct {
  43. // RobotURL string
  44. // }
  45. // //输出到Excel文件中
  46. // ExcelStorage struct {
  47. // FieldMapping map[string]string
  48. // ExcelFile *excelize.File
  49. // RowIndex int
  50. // SheetName string
  51. // FilePath string
  52. // }
  53. // //输出到EPUB文档
  54. // EpubStorage struct {
  55. // EpubFilePath string
  56. // EpubFile *epub.Epub
  57. // }
  58. // )
  59. // // NewMongoStorage
  60. // func NewMongoStorage(mongoURI, db, collection string) *MongoStorage {
  61. // client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoURI))
  62. // if err != nil {
  63. // panic(err)
  64. // }
  65. // return &MongoStorage{
  66. // c: client.Database(db).Collection(collection),
  67. // }
  68. // }
  69. // // NewDebugerStorage
  70. // func NewConsoleStorage() *ConsoleStorage {
  71. // return &ConsoleStorage{}
  72. // }
  73. // // NewFileStorage
  74. // func NewFileStorage(f io.WriteCloser) *FileStorage {
  75. // return &FileStorage{f: f}
  76. // }
  77. // // NewWeChatMsgStorage
  78. // func NewWeChatMsgStorage(robotUrl string) *WeChatMsgStorage {
  79. // return &WeChatMsgStorage{robotUrl}
  80. // }
  81. // // Save
  82. // func (ms *MongoStorage) Save(siteName, spiderCode, siteChannelName, siteChannelUrl string, data map[string]interface{}) error {
  83. // data["site"] = siteName
  84. // data["spidercode"] = spiderCode
  85. // data["channel"] = siteChannelName
  86. // data["channelurl"] = siteChannelUrl
  87. // data["comeintime"] = time.Now().Add(time.Duration(1) * time.Hour).Unix()
  88. // t, _ := time.Parse("2006-01-02 15:04:05", fmt.Sprint(data["publishtime"]))
  89. // if t.Unix() > 0 {
  90. // data["l_np_publishtime"] = t.Unix()
  91. // }
  92. // _, err := ms.c.InsertOne(context.TODO(), data)
  93. // return err
  94. // }
  95. // func (ms *MongoStorage) SaveUp(query map[string]interface{}, data map[string]interface{}) error {
  96. // _, err := ms.c.UpdateOne(context.TODO(), query, data)
  97. // return err
  98. // }
  99. // // Save
  100. // func (ds *ConsoleStorage) Save(spiderCode, siteName, siteChannelName, siteChannelUrl string, data map[string]interface{}) error {
  101. // data["spider_code"] = spiderCode
  102. // data["site_name"] = siteName
  103. // data["site_channel_name"] = siteChannelName
  104. // data["site_channel_url"] = siteChannelUrl
  105. // fmt.Println(" data=")
  106. // for k, v := range data {
  107. // fmt.Printf("\t%s = %v \n", k, v)
  108. // }
  109. // return nil
  110. // }
  111. // // Save
  112. // func (fs *FileStorage) Save(spiderCode, siteName, siteChannelName, siteChannelUrl string, data map[string]interface{}) error {
  113. // data["spider_code"] = spiderCode
  114. // data["site_name"] = siteName
  115. // data["site_channel_name"] = siteChannelName
  116. // data["site_channel_url"] = siteChannelUrl
  117. // buf := new(bytes.Buffer)
  118. // json.NewEncoder(buf).Encode(data)
  119. // fs.f.Write([]byte(","))
  120. // fs.f.Write(buf.Bytes())
  121. // fs.f.Write([]byte("\n"))
  122. // return nil
  123. // }
  124. // // Save
  125. // func (ws *WeChatMsgStorage) Save(spiderCode, siteName, siteChannelName, siteChannelUrl string, data map[string]interface{}) error {
  126. // msg := fmt.Sprintf("%v", data)
  127. // body := struct {
  128. // MsgType string `json:"msgtype"`
  129. // Text struct {
  130. // Content string `json:"content"`
  131. // } `json:"text"`
  132. // }{
  133. // MsgType: "text",
  134. // Text: struct {
  135. // Content string `json:"content"`
  136. // }{msg},
  137. // }
  138. // bs, err := json.Marshal(body)
  139. // if err != nil {
  140. // return err
  141. // }
  142. // client := new(http.Client)
  143. // req, err := http.NewRequest("POST", ws.RobotURL, bytes.NewReader(bs))
  144. // if err != nil {
  145. // return err
  146. // }
  147. // req.Header.Set("Content-Type", "application/json")
  148. // resp, err := client.Do(req)
  149. // if err != nil {
  150. // return err
  151. // }
  152. // return resp.Body.Close()
  153. // }
  154. // // NewExcelStorage
  155. // func NewExcelStorage(filepath string) *ExcelStorage {
  156. // f, err := excelize.OpenFile(filepath)
  157. // if err != nil {
  158. // panic(err)
  159. // }
  160. // sheetName := f.GetSheetName(0)
  161. // //第一行是表头
  162. // rows, err := f.GetRows(sheetName)
  163. // if err != nil {
  164. // panic(err)
  165. // }
  166. // colnames := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  167. // mapping := map[string]string{}
  168. // for i, title := range rows[0] {
  169. // mapping[title] = string(colnames[i])
  170. // }
  171. // return &ExcelStorage{
  172. // FieldMapping: mapping,
  173. // ExcelFile: f,
  174. // RowIndex: 1,
  175. // SheetName: sheetName,
  176. // FilePath: filepath,
  177. // }
  178. // }
  179. // // 保存数据
  180. // func (es *ExcelStorage) Save(spiderCode, siteName, siteChannelName, siteChannelUrl string, data map[string]interface{}) error {
  181. // es.RowIndex += 1
  182. // for k, v := range data {
  183. // if col, ok := es.FieldMapping[k]; ok {
  184. // cellName := fmt.Sprintf("%s%d", col, es.RowIndex)
  185. // value, ok := v.(string)
  186. // //fmt.Println(" write::", cellName, value)
  187. // if ok {
  188. // es.ExcelFile.SetCellStr(es.SheetName, cellName, value)
  189. // }
  190. // }
  191. // }
  192. // es.ExcelFile.Save()
  193. // return nil
  194. // }
  195. // // 电子书,存储
  196. // func NewEpubStorage(filepath string) *EpubStorage {
  197. // output := epub.NewEpub("")
  198. // return &EpubStorage{
  199. // EpubFilePath: filepath,
  200. // EpubFile: output,
  201. // }
  202. // }
  203. // // 保存数据
  204. // func (es *EpubStorage) Save(spiderCode, siteName, siteChannelName, siteChannelUrl string, data map[string]interface{}) error {
  205. // title, _ := data["title"].(string)
  206. // body, _ := data["body"].(string)
  207. // index, _ := data["index"].(int64)
  208. // filename := fmt.Sprintf("%08d.xhtml", index)
  209. // if title == "init" {
  210. // bookName, _ := data["book_name"].(string)
  211. // author, _ := data["book_author"].(string)
  212. // log.Println("初始化电子书", bookName, author)
  213. // es.EpubFile.SetTitle(bookName)
  214. // es.EpubFile.SetAuthor(author)
  215. // } else if title == "save2epubfile" {
  216. // log.Println("开始写入epubfile", es.EpubFilePath)
  217. // es.EpubFile.Write(es.EpubFilePath)
  218. // } else {
  219. // es.EpubFile.AddSection(body, title, filename, "")
  220. // }
  221. // return nil
  222. // }