parse_handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package handlers
  2. import (
  3. "encoding/json"
  4. "git.familybaby.top/flight/csdn/third"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "git.familybaby.top/flight/csdn/database"
  9. "git.familybaby.top/flight/csdn/models"
  10. "github.com/gorilla/mux"
  11. )
  12. var (
  13. parser = third.NewParser("tzlbndf9pi7ebzky", "https://unlockdoc.smain.cn/api/api")
  14. )
  15. type ParseInfo struct {
  16. Url string `json:"url"`
  17. UserId string `json:"userId"`
  18. }
  19. func CheckUrlInvalid(w http.ResponseWriter, r *http.Request) {
  20. var info ParseInfo
  21. err := json.NewDecoder(r.Body).Decode(&info)
  22. if err != nil {
  23. http.Error(w, err.Error(), http.StatusBadRequest)
  24. return
  25. }
  26. price, err := parser.GetTaskPrice(info.Url)
  27. if err != nil {
  28. http.Error(w, err.Error(), http.StatusBadRequest)
  29. return
  30. }
  31. w.Header().Set("Content-Type", "application/json")
  32. w.WriteHeader(http.StatusCreated)
  33. json.NewEncoder(w).Encode(price)
  34. }
  35. func CreateParse(w http.ResponseWriter, r *http.Request) {
  36. var info ParseInfo
  37. err := json.NewDecoder(r.Body).Decode(&info)
  38. if err != nil {
  39. http.Error(w, err.Error(), http.StatusBadRequest)
  40. return
  41. }
  42. price, err := parser.PurchasePaperPlusProxy(info.Url)
  43. if err != nil {
  44. http.Error(w, err.Error(), http.StatusBadRequest)
  45. return
  46. }
  47. records, err := parser.GetRecords()
  48. if err != nil {
  49. http.Error(w, err.Error(), http.StatusBadRequest)
  50. return
  51. }
  52. var Parse models.Parse
  53. var hasValue = false
  54. for _, record := range records {
  55. if record.Source == info.Url && record.HtmlUrl == price {
  56. Parse.Title = record.Title
  57. ct, err := time.Parse("2006-01-02 15:04:05", record.Date)
  58. if err == nil {
  59. Parse.CreatedAt = ct
  60. }
  61. Parse.HtmlUrl = record.HtmlUrl
  62. Parse.OnlineUrl = record.OnlineHtmlUrl
  63. Parse.SourceUrl = record.Source
  64. hasValue = true
  65. break
  66. }
  67. }
  68. if !hasValue {
  69. http.Error(w, "解析失败", http.StatusInternalServerError)
  70. return
  71. }
  72. result := database.DB.Create(&Parse)
  73. if result.Error != nil {
  74. http.Error(w, result.Error.Error(), http.StatusInternalServerError)
  75. return
  76. }
  77. w.Header().Set("Content-Type", "application/json")
  78. w.WriteHeader(http.StatusCreated)
  79. json.NewEncoder(w).Encode(Parse)
  80. }
  81. func GetParses(w http.ResponseWriter, r *http.Request) {
  82. var Parses []models.Parse
  83. result := database.DB.Find(&Parses)
  84. if result.Error != nil {
  85. http.Error(w, result.Error.Error(), http.StatusInternalServerError)
  86. return
  87. }
  88. w.Header().Set("Content-Type", "application/json")
  89. json.NewEncoder(w).Encode(Parses)
  90. }
  91. func GetParse(w http.ResponseWriter, r *http.Request) {
  92. params := mux.Vars(r)
  93. id, err := strconv.Atoi(params["id"])
  94. if err != nil {
  95. http.Error(w, "Invalid ID", http.StatusBadRequest)
  96. return
  97. }
  98. var Parse models.Parse
  99. result := database.DB.First(&Parse, id)
  100. if result.Error != nil {
  101. http.Error(w, result.Error.Error(), http.StatusNotFound)
  102. return
  103. }
  104. w.Header().Set("Content-Type", "application/json")
  105. json.NewEncoder(w).Encode(Parse)
  106. }
  107. func UpdateParse(w http.ResponseWriter, r *http.Request) {
  108. params := mux.Vars(r)
  109. id, err := strconv.Atoi(params["id"])
  110. if err != nil {
  111. http.Error(w, "Invalid ID", http.StatusBadRequest)
  112. return
  113. }
  114. var Parse models.Parse
  115. err = json.NewDecoder(r.Body).Decode(&Parse)
  116. if err != nil {
  117. http.Error(w, err.Error(), http.StatusBadRequest)
  118. return
  119. }
  120. result := database.DB.Model(&models.Parse{}).Where("id = ?", id).Updates(Parse)
  121. if result.Error != nil {
  122. http.Error(w, result.Error.Error(), http.StatusInternalServerError)
  123. return
  124. }
  125. w.Header().Set("Content-Type", "application/json")
  126. w.WriteHeader(http.StatusOK)
  127. json.NewEncoder(w).Encode(Parse)
  128. }
  129. func DeleteParse(w http.ResponseWriter, r *http.Request) {
  130. params := mux.Vars(r)
  131. id, err := strconv.Atoi(params["id"])
  132. if err != nil {
  133. http.Error(w, "Invalid ID", http.StatusBadRequest)
  134. return
  135. }
  136. result := database.DB.Delete(&models.Parse{}, id)
  137. if result.Error != nil {
  138. http.Error(w, result.Error.Error(), http.StatusInternalServerError)
  139. return
  140. }
  141. w.WriteHeader(http.StatusNoContent)
  142. }