Browse Source

粮油商务网数据对接-配置

gmy 6 months ago
parent
commit
c54a3997b8

+ 12 - 24
cmd/commodity_liangyou.go → services/liangyou/commodity_liangyou.go

@@ -1,4 +1,4 @@
-package main
+package liangyou
 
 import (
 	"context"
@@ -21,19 +21,12 @@ const (
 	lyLoginPath = "https://www.fao.com.cn/"
 )
 
-// UserInfo 登录用户信息
-type UserInfo struct {
-	Username string
-	Password string
-}
-
-func main() {
-
+func LyDataDeal(cont context.Context) (err error) {
 	// 读取 JSON 文件
-	configFile, err := os.ReadFile("D:\\go\\workspace1\\eta_crawler\\static\\liangyou.json")
+	configFile, err := os.ReadFile(utils.LY_JSON_PATH)
 	if err != nil {
 		fmt.Printf("读取配置文件错误: %v\n", err)
-		return
+		return nil
 	}
 
 	// 定义通用的 map 结构体来解析 JSON
@@ -43,7 +36,7 @@ func main() {
 	err = json.Unmarshal(configFile, &data)
 	if err != nil {
 		fmt.Printf("解析配置文件错误: %v\n", err)
-		return
+		return nil
 	}
 
 	// 打印解析后的数据以验证
@@ -65,7 +58,7 @@ func main() {
 	err = login(ctx)
 	if err != nil {
 		fmt.Printf("登录错误: %v\n", err)
-		return
+		return nil
 	}
 
 	// 遍历配置并爬取数据
@@ -80,20 +73,18 @@ func main() {
 			}
 		}
 	}
+	return nil
 }
 
 func login(ctx context.Context) error {
-	userInfo := UserInfo{
-		Username: "13633849418",
-		Password: "828384Abc@",
-	}
+
 	return chromedp.Run(ctx,
 		chromedp.Navigate(lyLoginPath),
 		chromedp.Sleep(5*time.Second),
 		chromedp.Click(`a[id="btnLogin"]`, chromedp.ByQuery),
 		chromedp.Sleep(2*time.Second),
-		chromedp.SetValue(`input[id="userName"]`, userInfo.Username, chromedp.ByQuery),
-		chromedp.SetValue(`input[id="pwd"]`, userInfo.Password, chromedp.ByQuery),
+		chromedp.SetValue(`input[id="userName"]`, utils.LY_USERNAME, chromedp.ByQuery),
+		chromedp.SetValue(`input[id="pwd"]`, utils.LY_PASSWORD, chromedp.ByQuery),
 		chromedp.Sleep(2*time.Second),
 		chromedp.Click(`input[id="btn_Login"]`, chromedp.ByQuery),
 		chromedp.Sleep(5*time.Second),
@@ -147,11 +138,9 @@ func fetchReportData(ctx context.Context, product, category, report string, keyw
 		reportURLs := extractReportURLs(htmlContent, report)
 		allReportURLs = append(allReportURLs, reportURLs...)
 
-		// todo 测试环境跑部分数据,上线放开
-		break
 		// Check if next page button is disabled
-		// todo 测试环境跑部分数据,上线放开
-		/*var nextPageDisabled bool
+		//  测试环境跑部分数据,上线放开...
+		var nextPageDisabled bool
 		err = chromedp.Run(ctx,
 			chromedp.Evaluate(`document.querySelector('div.my-page-next').classList.contains('my-page-forbid')`, &nextPageDisabled),
 		)
@@ -172,7 +161,6 @@ func fetchReportData(ctx context.Context, product, category, report string, keyw
 		if err != nil {
 			return err
 		}
-		*/
 	}
 
 	logs.Info("所有报告 URLs: %s: %s: %v", product, category, allReportURLs)

+ 2 - 1
cmd/processor_business_logic.go → services/liangyou/processor_business_logic.go

@@ -1,5 +1,6 @@
+// Package liangyou
 // @Author gmy 2024/8/6 10:50:00
-package main
+package liangyou
 
 import (
 	"context"

+ 2 - 1
cmd/processor_factory.go → services/liangyou/processor_factory.go

@@ -1,5 +1,6 @@
+// Package liangyou
 // @Author gmy 2024/8/6 10:48:00
-package main
+package liangyou
 
 import (
 	"context"

+ 3 - 0
services/task.go

@@ -34,6 +34,8 @@ func Task() {
 
 		crawlerIcpi := task.NewTask("refreshData", "0 0,30 16-23 * * *", CrawlerIcpi) //居民消费价格指数
 
+		//lyData := task.NewTask("refreshData", "0 0,30 16-23 * * *", liangyou.LyDataDeal) //todo 粮油商务网 时间上线定
+
 		// 统计局-分月季年爬
 		//refreshNationalMonthA := task.NewTask("RefreshNationalMonthDbA", "0 15 2 10 * *", national_data.RefreshNationalMonthDbA)
 		//refreshNationalMonthB := task.NewTask("RefreshNationalMonthDbB", "0 15 2 16 * *", national_data.RefreshNationalMonthDbB)
@@ -51,6 +53,7 @@ func Task() {
 		task.AddTask("UN月度数据", syncYearMonthComTrade) //每月1号的3点同步
 
 		task.AddTask("居民消费价格指数", crawlerIcpi) //每月1号的3点同步
+		//task.AddTask("粮油商务网", lyData)
 	}
 
 	if utils.BusinessCode == utils.BusinessCodeFuBang {

+ 11 - 1
utils/config.go

@@ -33,6 +33,13 @@ var (
 	IS_INIT_SCI99     string
 )
 
+// 粮油商务网
+var (
+	LY_USERNAME  string
+	LY_PASSWORD  string
+	LY_JSON_PATH string
+)
+
 var (
 	EDB_LIB_URL         string
 	APP_EDB_LIB_NAME_EN string
@@ -95,7 +102,10 @@ func init() {
 	if RunMode == "release" {
 
 	} else {
-
+		// todo 粮油商务网 上线再根据环境做配置
+		LY_USERNAME = config["ly_username"]
+		LY_PASSWORD = config["ly_password"]
+		LY_JSON_PATH = config["ly_json_path"]
 	}
 	//日志配置
 	{