From bd7a9e27c74e1fce572f5c9c93fe0ac19500d557 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Wed, 24 Jul 2019 19:51:52 +0800 Subject: [PATCH] added file edit --- backend/main.go | 3 +- backend/routes/spider.go | 36 +++++++++++++++++++ .../sinastock/spiders/sinastock_spider.py | 1 - 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/backend/main.go b/backend/main.go index 12030bb5..e4669ce1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -92,7 +92,8 @@ func main() { app.POST("/spiders/:id/publish", routes.PublishSpider) // 发布爬虫 app.DELETE("/spiders/:id", routes.DeleteSpider) // 删除爬虫 app.GET("/spiders/:id/tasks", routes.GetSpiderTasks) // 爬虫任务列表 - app.GET("/spiders/:id/file", routes.GetSpiderFile) // 爬虫文件 + app.GET("/spiders/:id/file", routes.GetSpiderFile) // 爬虫文件读取 + app.POST("/spiders/:id/file", routes.PostSpiderFile) // 爬虫目录写入 app.GET("/spiders/:id/dir", routes.GetSpiderDir) // 爬虫目录 // 任务 app.GET("/tasks", routes.GetTaskList) // 任务列表 diff --git a/backend/routes/spider.go b/backend/routes/spider.go index e61ff007..01815325 100644 --- a/backend/routes/spider.go +++ b/backend/routes/spider.go @@ -306,3 +306,39 @@ func GetSpiderFile(c *gin.Context) { Data: string(fileBytes), }) } + +type SpiderFileReqBody struct { + Path string `json:"path"` + Content string `json:"content"` +} + +func PostSpiderFile(c *gin.Context) { + // 爬虫ID + id := c.Param("id") + + // 文件相对路径 + var reqBody SpiderFileReqBody + if err := c.ShouldBindJSON(&reqBody); err != nil { + HandleError(http.StatusBadRequest, c, err) + return + } + + // 获取爬虫 + spider, err := model.GetSpider(bson.ObjectIdHex(id)) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + + // 写文件 + if err := ioutil.WriteFile(filepath.Join(spider.Src, reqBody.Path), []byte(reqBody.Content), os.ModePerm); err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + + // 返回结果 + c.JSON(http.StatusOK, Response{ + Status: "ok", + Message: "success", + }) +} diff --git a/spiders/sinastock/sinastock/spiders/sinastock_spider.py b/spiders/sinastock/sinastock/spiders/sinastock_spider.py index 9d258e6c..54daf763 100644 --- a/spiders/sinastock/sinastock/spiders/sinastock_spider.py +++ b/spiders/sinastock/sinastock/spiders/sinastock_spider.py @@ -8,7 +8,6 @@ from pymongo import MongoClient from sinastock.items import NewsItem - class SinastockSpiderSpider(scrapy.Spider): name = 'sinastock_spider' allowed_domains = ['finance.sina.com.cn']