Browse Source

gpr风险指数定时

xyxie 2 months ago
parent
commit
2dbe9c1169
2 changed files with 10 additions and 4 deletions
  1. 7 4
      services/gpr_risk.go
  2. 3 0
      services/task.go

+ 7 - 4
services/gpr_risk.go

@@ -30,7 +30,6 @@ type GprRiskIndex struct {
 }
 
 func DownloadGprRiskFile() (err error) {
-	// todo 设置下载频率, 如果有正在处理中的,则暂停下载
 	defer func() {
 		if err != nil {
 			msg := "失败提醒" + "DownloadGprRiskFile ErrMsg:" + err.Error()
@@ -56,6 +55,7 @@ func DownloadGprRiskFile() (err error) {
 	defer out.Close()
 
 	// 发起下载请求
+	fmt.Println(time.Now().Format(utils.FormatDateTime), "开始下载 GPR地缘风险指数 文件")
 	resp, err := http.Get(url)
 	if err != nil {
 		err = fmt.Errorf("failed to make request: %w", err)
@@ -68,14 +68,16 @@ func DownloadGprRiskFile() (err error) {
 		err = fmt.Errorf("bad status: %s", resp.Status)
 		return
 	}
-
+	fmt.Println(time.Now().Format(utils.FormatDateTime), "下载 GPR地缘风险指数 文件完成")
+	fmt.Println(time.Now().Format(utils.FormatDateTime), "开始写入文件")
 	// 将内容写入文件
-	_, err = io.Copy(out, resp.Body)
+	buf := make([]byte, 4*1024*1024) // 4MB buffer
+	_, err = io.CopyBuffer(out, resp.Body, buf)
 	if err != nil {
 		err = fmt.Errorf("failed to copy content: %w", err)
 		return
 	}
-
+	fmt.Println(time.Now().Format(utils.FormatDateTime), "开始转换文件格式")
 	// 转换文件格式
 	downloadFileXlsx := destPath + "x"
 	err = ConvertXlsToXlsx(destPath, downloadFileXlsx)
@@ -83,6 +85,7 @@ func DownloadGprRiskFile() (err error) {
 		err = fmt.Errorf("文件格式转换失败 convert excel, Err:%w", err)
 		return
 	}
+	fmt.Println(time.Now().Format(utils.FormatDateTime), "文件格式转换完成")
 	// 使用通道等待解析完成
 	done := make(chan error)
 	go func() {

+ 3 - 0
services/task.go

@@ -37,14 +37,17 @@ func Task() {
 
 		crawlerIcpi := task.NewTask("refreshData", "0 0,30 16-23 * * *", CrawlerIcpi) //居民消费价格指数
 
+		downloadGPRRiskTask := task.NewTask("DownloadGPRRiskTask", "0 0,30 16-19 * * *", DownloadGPRRiskTask) //每天下午16点至19点,每隔半小时
 		// 统计局-分月季年爬
 		//refreshNationalMonthA := task.NewTask("RefreshNationalMonthDbA", "0 15 2 10 * *", national_data.RefreshNationalMonthDbA)
 		//refreshNationalMonthB := task.NewTask("RefreshNationalMonthDbB", "0 15 2 16 * *", national_data.RefreshNationalMonthDbB)
 		//refreshNationalQuarter := task.NewTask("RefreshNationalQuarterDb", "0 25 1 15 * *", national_data.RefreshNationalQuarterDb)
 		//refreshNationalYearA := task.NewTask("RefreshNationalYearDbA", "0 45 1 20 * *", national_data.RefreshNationalYearDbA)
 		//refreshNationalYearB := task.NewTask("RefreshNationalYearDbB", "0 45 1 25 * *", national_data.RefreshNationalYearDbB)
+
 		task.AddTask("美国农业部月度供需数据爬取", refreshUsdaPsd)
 		task.AddTask("美国农业部出口销售数据爬取", refreshUsdaFms)
+		task.AddTask("GPR地缘风险指数爬取", downloadGPRRiskTask)
 		task.AddTask("数据爬取", refreshData)
 		task.AddTask("欧洲天然气爬取", refreshEic)
 		task.AddTask("中国煤炭网爬取", refreshCoal)