added file edit

This commit is contained in:
Marvin Zhang
2019-07-24 19:51:52 +08:00
parent 2dc51c48fc
commit bd7a9e27c7
3 changed files with 38 additions and 2 deletions

View File

@@ -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) // 任务列表

View File

@@ -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",
})
}

View File

@@ -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']