123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package handlers
- import (
- "encoding/json"
- "git.familybaby.top/flight/csdn/third"
- "net/http"
- "strconv"
- "time"
- "git.familybaby.top/flight/csdn/database"
- "git.familybaby.top/flight/csdn/models"
- "github.com/gorilla/mux"
- )
- var (
- parser = third.NewParser("tzlbndf9pi7ebzky", "https://unlockdoc.smain.cn/api/api")
- )
- type ParseInfo struct {
- Url string `json:"url"`
- UserId string `json:"userId"`
- }
- func CheckUrlInvalid(w http.ResponseWriter, r *http.Request) {
- var info ParseInfo
- err := json.NewDecoder(r.Body).Decode(&info)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- price, err := parser.GetTaskPrice(info.Url)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusCreated)
- json.NewEncoder(w).Encode(price)
- }
- func CreateParse(w http.ResponseWriter, r *http.Request) {
- var info ParseInfo
- err := json.NewDecoder(r.Body).Decode(&info)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- price, err := parser.PurchasePaperPlusProxy(info.Url)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- records, err := parser.GetRecords()
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- var Parse models.Parse
- var hasValue = false
- for _, record := range records {
- if record.Source == info.Url && record.HtmlUrl == price {
- Parse.Title = record.Title
- ct, err := time.Parse("2006-01-02 15:04:05", record.Date)
- if err == nil {
- Parse.CreatedAt = ct
- }
- Parse.HtmlUrl = record.HtmlUrl
- Parse.OnlineUrl = record.OnlineHtmlUrl
- Parse.SourceUrl = record.Source
- hasValue = true
- break
- }
- }
- if !hasValue {
- http.Error(w, "解析失败", http.StatusInternalServerError)
- return
- }
- result := database.DB.Create(&Parse)
- if result.Error != nil {
- http.Error(w, result.Error.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusCreated)
- json.NewEncoder(w).Encode(Parse)
- }
- func GetParses(w http.ResponseWriter, r *http.Request) {
- var Parses []models.Parse
- result := database.DB.Find(&Parses)
- if result.Error != nil {
- http.Error(w, result.Error.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(Parses)
- }
- func GetParse(w http.ResponseWriter, r *http.Request) {
- params := mux.Vars(r)
- id, err := strconv.Atoi(params["id"])
- if err != nil {
- http.Error(w, "Invalid ID", http.StatusBadRequest)
- return
- }
- var Parse models.Parse
- result := database.DB.First(&Parse, id)
- if result.Error != nil {
- http.Error(w, result.Error.Error(), http.StatusNotFound)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(Parse)
- }
- func UpdateParse(w http.ResponseWriter, r *http.Request) {
- params := mux.Vars(r)
- id, err := strconv.Atoi(params["id"])
- if err != nil {
- http.Error(w, "Invalid ID", http.StatusBadRequest)
- return
- }
- var Parse models.Parse
- err = json.NewDecoder(r.Body).Decode(&Parse)
- if err != nil {
- http.Error(w, err.Error(), http.StatusBadRequest)
- return
- }
- result := database.DB.Model(&models.Parse{}).Where("id = ?", id).Updates(Parse)
- if result.Error != nil {
- http.Error(w, result.Error.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusOK)
- json.NewEncoder(w).Encode(Parse)
- }
- func DeleteParse(w http.ResponseWriter, r *http.Request) {
- params := mux.Vars(r)
- id, err := strconv.Atoi(params["id"])
- if err != nil {
- http.Error(w, "Invalid ID", http.StatusBadRequest)
- return
- }
- result := database.DB.Delete(&models.Parse{}, id)
- if result.Error != nil {
- http.Error(w, result.Error.Error(), http.StatusInternalServerError)
- return
- }
- w.WriteHeader(http.StatusNoContent)
- }
|