Browse Source

feat: 广东信息价造价通对接数据,支持按季度拉取数据

vian 11 months ago
parent
commit
2a2396e22a
1 changed files with 56 additions and 13 deletions
  1. 56 13
      web/over_write/crawler/guangdong_2018_price_crawler.js

+ 56 - 13
web/over_write/crawler/guangdong_2018_price_crawler.js

@@ -196,6 +196,44 @@ const monthMap = {
   '12': '12月',
 };
 
+const quaterReg = /季度/;
+
+const quaters = ['一季度', '二季度', '三季度', '四季度'];
+
+// 根据季度期数范围,获得期数数据, 2020年-一季度 - 2021年-四季度
+function getQuaterPeriodData(from, to) {
+  let [fromYear, fromQuater] = from.split('-');
+  fromYear = +fromYear.replace('年', '');
+  fromQuater = quaters.indexOf(fromQuater);// 将季度转换为数值,方便对比;
+  if (fromQuater < 0) {
+    return null;
+  }
+  let [toYear, toQuater] = to.split('-');
+  toYear = +toYear.replace('年', '');
+  toQuater = quaters.indexOf(toQuater);// 将季度转换为数值,方便对比;
+  if (toQuater < 0) {
+    return null;
+  }
+  const newFrom = [fromYear, fromQuater].join('-');
+  const newTo = [toYear, toQuater].join('-');
+  if (newFrom > newTo) {
+    return null;
+  }
+  const periods = [];
+  let curYear = fromYear;
+  let curQuater = fromQuater;
+  while ((curYear <= toYear && curQuater <= toQuater) || curYear < toYear) {
+    periods.push(`${curYear}年-${quaters[curQuater]}`);
+    if (curQuater === 3) {
+      curYear++;
+      curQuater = 0;
+    } else {
+      curQuater++;
+    }
+  }
+  return periods;
+}
+
 // 根据期数范围,获取期数数据
 function getPeriodData(from, to) {
   if (from > to) {
@@ -211,7 +249,7 @@ function getPeriodData(from, to) {
   let curYear = fromYear;
   let curMonth = fromMonth;
   const periods = [];
-  while (curYear <= toYear && curMonth <= toMonth) {
+  while ((curYear <= toYear && curMonth <= toMonth) || curYear < toYear) {
     periods.push(`${curYear}年-${monthMap[curMonth]}`);
     if (curMonth === 12) {
       curYear++;
@@ -225,27 +263,30 @@ function getPeriodData(from, to) {
 
 // 根据期刊数据,获取需要信息价接口需要的date
 function getDateForApi(journalList, period) {
-  const monthPeriod = `${period}-05`; // 月度
-  const matchMonth = journalList.find(dateItem => dateItem.date === monthPeriod);
-  if (matchMonth) {
-    return matchMonth.date;
+  const isQuaterPeriod = quaterReg.test(period);
+  if (!isQuaterPeriod) {
+    const monthPeriod = `${period}-05`; // 月度
+    const matchMonth = journalList.find(dateItem => dateItem.date === monthPeriod);
+    if (matchMonth) {
+      return matchMonth.date;
+    }
   }
   // 没匹配到月度数据,去匹配季度
   const month = period.split('-')[1];
   let quaterDate;
-  if (['1', '01', '2', '02', '3', '03'].includes(month)) {
+  if (['1', '01', '2', '02', '3', '03', '一季度'].includes(month)) {
     quaterDate = '03-15';
-  } else if (['4', '04', '5', '05', '6', '06'].includes(month)) {
+  } else if (['4', '04', '5', '05', '6', '06', '二季度'].includes(month)) {
     quaterDate = '06-15';
-  } else if (['7', '07', '8', '08', '9', '09'].includes(month)) {
+  } else if (['7', '07', '8', '08', '9', '09', '三季度'].includes(month)) {
     quaterDate = '09-15';
-  } else if (['10', '11', '12'].includes(month)) {
+  } else if (['10', '11', '12', '四季度'].includes(month)) {
     quaterDate = '12-15';
   }
   const year = period.split('-')[0];
   const matchQuater = journalList.find(dateItem => dateItem.date === `${year}-${quaterDate}`);
-  if (matchQuater) {
-    return matchQuater.date;
+  if (matchQuater || isQuaterPeriod) {
+    return matchQuater ? matchQuater.date : null;
   }
   // 没匹配到季度数据,去匹配半年数据
   if (month / 6 <= 1) {
@@ -457,8 +498,11 @@ async function saveData(compilationID, period, areaID, sourceData, classNameMap)
  * @return {Object}
  */
 async function crawlData(from, to, compilationID) {
+  // 存入信息价相关数据
   const token = await getToken();
-  const periods = getPeriodData(from, to);
+  const periods = quaterReg.test(from) ? getQuaterPeriodData(from, to) : getPeriodData(from, to);
+  console.log(`periods`);
+  console.log(periods);
   if (!periods) {
     throw '无效的期数区间。';
   }
@@ -494,7 +538,6 @@ async function crawlData(from, to, compilationID) {
         continue;
       }
 
-      // 存入信息价相关数据
       const sourceData = await getPriceInfoSource(token, sourcePeriod, city, county);
       if (typeof sourceData === 'string') {
         hintInfos.push(sourceData);