From 1406c1e1a9b2165d731cf53c0b0ca544de5df70c Mon Sep 17 00:00:00 2001 From: wcq Date: Thu, 30 Mar 2023 16:49:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E4=BF=A1=E6=81=AF=E5=BD=95?= =?UTF-8?q?=E5=85=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/urban-investment-research.iml | 1 + Config/common.ini | 2 +- DataLoad/AreaDataLoad.py | 119 ++++++++++++++++-- .../Mods/AreaRealEstate/Schemas.py | 4 +- ThirdPartyApi/Tianyancha/Apis.py | 20 +++ ThirdPartyApi/Yujingtong/Apis.py | 46 +++++-- 6 files changed, 170 insertions(+), 22 deletions(-) diff --git a/.idea/urban-investment-research.iml b/.idea/urban-investment-research.iml index e247c0c..4a5e3e3 100644 --- a/.idea/urban-investment-research.iml +++ b/.idea/urban-investment-research.iml @@ -18,6 +18,7 @@ diff --git a/Config/common.ini b/Config/common.ini index de812a9..a8ddd78 100644 --- a/Config/common.ini +++ b/Config/common.ini @@ -41,4 +41,4 @@ port = 8003 token = 32737167-cb63-4ce9-9397-d66169488f51 [yujingtong] -token = "" \ No newline at end of file +token = 92229502BD4C4B3E934607D4C1C5E711 \ No newline at end of file diff --git a/DataLoad/AreaDataLoad.py b/DataLoad/AreaDataLoad.py index b4227d3..3671ae4 100644 --- a/DataLoad/AreaDataLoad.py +++ b/DataLoad/AreaDataLoad.py @@ -1,5 +1,5 @@ +from fastapi import HTTPException from sqlalchemy.orm import Session - from Context.common import yujingtong_api from Mods.RegionalEconomies.Mods.AreaDebt.Models import AreaDebt from Mods.RegionalEconomies.Mods.AreaDepositsAndLoans.Models import AreaDepositsAndLoans @@ -13,24 +13,121 @@ from Mods.RegionalEconomies.Mods.AreaRealEstate.Models import AreaRealEstate def area_economic_data_load(db: Session, area_id: int, year: int): + year = str(year) area_economic_res = yujingtong_api.get_area_economic(area_id, year) - AreaDepositsAndLoans - AreaImportAndExport - AreaIndustry - AreaInvest - AreaLivelihood - AreaRealEstate - AreaGdp + if not area_economic_res: + raise HTTPException(detail="未查询到该区域数据", status_code=403) + ### + new_area_deposits_and_loans = AreaDepositsAndLoans() + new_area_deposits_and_loans.area_id = area_id + new_area_deposits_and_loans.count_value = year + new_area_deposits_and_loans.deposit = area_economic_res.SAV_DEP_BAL + new_area_deposits_and_loans.loan = area_economic_res.LOAN_BAL + db.add(new_area_deposits_and_loans) + ### + new_area_import_and_export = AreaImportAndExport() + new_area_import_and_export.area_id = area_id + new_area_import_and_export.count_value = year + new_area_import_and_export.import_and_export_volume = area_economic_res.TOT_IPT_AND_EXP + new_area_import_and_export.export_volume = area_economic_res.TOT_EPT + new_area_import_and_export.import_volume = area_economic_res.TOT_IPT + db.add(new_area_deposits_and_loans) + ### + new_area_industry = AreaIndustry() + new_area_industry.area_id = area_id + new_area_industry.count_value = year + new_area_industry.primary_industry = area_economic_res.PRI_IND + new_area_industry.secondary_industry = area_economic_res.SEC_IND + new_area_industry.tertiary_Industry = area_economic_res.TER_IND + new_area_industry.industrial_added_value = area_economic_res.GRO_VAL_IND_INC + new_area_industry.industrial_output = area_economic_res.GRO_VAL_IND + new_area_industry.industrial_output_growth_rate = area_economic_res.GRO_VAL_IND_RATE + db.add(new_area_industry) + ## + new_area_livelihood = AreaLivelihood() + new_area_livelihood.area_id = area_id + new_area_livelihood.count_value = year + new_area_livelihood.population = area_economic_res.PERM_RESI + new_area_livelihood.consumer_goods_retail_sales = area_economic_res.TOT_RT_SALES_OF_CONS + new_area_livelihood.consumer_goods_retail_sales_growth_rate = area_economic_res.TOT_RT_SALES_OF_CONS_RATE + new_area_livelihood.per_capita_disposable_income_of_urban_residents = area_economic_res.PER_CPT_DPSB_INC_IN_UB + db.add(new_area_livelihood) + ## + new_area_real_estate = AreaRealEstate() + new_area_real_estate.area_id = area_id + new_area_real_estate.count_value = year + new_area_real_estate.second_hand_housing_average_price = area_economic_res.AVE_PRI_OF_SEC_HAND_HOUSE + new_area_real_estate.residential_land_floor_price = area_economic_res.FLO_PRI_OF_RES_LAND + new_area_real_estate.new_house_sales_price = area_economic_res.SAL_PRI_OF_NEW_BUIL + db.add(new_area_real_estate) + ### + new_area_gdp = AreaGdp() + new_area_gdp.area_id = area_id + new_area_gdp.count_value = year + new_area_gdp.GDP = area_economic_res.GDP + new_area_gdp.GDP_growth_rate = area_economic_res.GDP_GRO_ANL + new_area_gdp.GDP_per_capita = area_economic_res.PER_CPT_GDP + db.add(new_area_gdp) + ### + new_area_invest = AreaInvest() + new_area_invest.area_id = area_id + new_area_invest.count_value = year + new_area_invest.fixed_asset_investment = area_economic_res.FIXED_AST_IVM + new_area_invest.fixed_asset_investment_growth_rate = area_economic_res.FIXED_AST_IVM_RATE + new_area_invest.real_estate_investment = area_economic_res.INV_REAL_EST + db.add(new_area_invest) + db.commit() def area_finance_data_load(db: Session, area_id: int, year: int): + year = str(year) # 区域财政 area_finance_res = yujingtong_api.get_area_finance(area_id, year) - AreaFiscalRevenue + if not area_finance_res: + raise HTTPException(detail="未查询到该区域数据", status_code=403) + new_item = AreaFiscalRevenue() + new_item.area_id = area_id + new_item.count_value = year + new_item.general_public_budget_income = area_finance_res.RVN_IN_GEN_PUB_BGT + new_item.general_public_budget_expenditure = area_finance_res.EXPD_IN_GOV_GEN_PUB_BGT + new_item.transfer_income = area_finance_res.TRAN_INC + new_item.transfer_expenditure = area_finance_res.TRAN_EXP + # new_item.fiscal_revenue=area_finance_res. + new_item.general_bond_income = area_finance_res.GEN_BON_REV + new_item.general_bond_principal_repayment_expenditure = area_finance_res.GEN_BON_REP_EXP + new_item.special_bonds_income = area_finance_res.SPE_BOND_INC + new_item.principal_repayment_expenditure_of_special_bonds = area_finance_res.SPE_BOND_REP_EXP + new_item.tax_revenue = area_finance_res.TAX_RVN + new_item.transfer_payment_income = area_finance_res.TRAN_INC + # new_item.fiscal_expenditure= + new_item.government_fund_income = area_finance_res.RVN_INTO_GOV_MNGD_FD + new_item.land_transfer_income = area_finance_res.LAND_TRAN_INC + new_item.government_fund_expenditure = area_finance_res.EXP_INTO_GOV_MNGD_FD + new_item.state_owned_capital_operation_income = area_finance_res.BGT_RVN_FROM_STAT_CPT_OPR + new_item.state_owned_capital_operation_expenditure = area_finance_res.EXP_FROM_STAT_CPT_OPR + db.add(new_item) + db.commit() def area_debt_data_load(db: Session, area_id: int, year: int): + year = str(year) # 区域债务 area_debt_res = yujingtong_api.get_area_debt(area_id, year) - - pass + if not area_debt_res: + raise HTTPException(detail="未查询到该区域数据", status_code=403) + item = AreaDebt() + item.area_id = area_id + item.count_value = year + item.local_government_debt_balance = area_debt_res.OTS_DT_OF_GOV + item.local_government_debt_limit = area_debt_res.CEIL_DT_OF_GOV + item.fiscal_self_sufficiency_rate = area_debt_res.FSL_SELF_FNCG + item.general_debt_balance = area_debt_res.GEN_OTS_DT + item.special_debt_balance = area_debt_res.SPE_OTS_DT + item.general_debt_limit = area_debt_res.GEN_CEIL_DT + item.special_debt_limit = area_debt_res.SPE_CEIL_DT + item.debt_ratio = area_debt_res.GOV_DT_TO_GDP_RT + item.broad_sense_debt_ratio = area_debt_res.GOV_AND_FNC_PLTF_DT_TO_GDP_RT + item.debt_to_asset_ratio = area_debt_res.GOV_DT_TO_RVN_RT + item.broad_sense_debt_to_asset_ratio = area_debt_res.GOV_AND_FNC_PLTF_DT_TO_RVN_RT + db.add(item) + db.commit() diff --git a/Mods/RegionalEconomies/Mods/AreaRealEstate/Schemas.py b/Mods/RegionalEconomies/Mods/AreaRealEstate/Schemas.py index bf598f0..182f033 100644 --- a/Mods/RegionalEconomies/Mods/AreaRealEstate/Schemas.py +++ b/Mods/RegionalEconomies/Mods/AreaRealEstate/Schemas.py @@ -7,7 +7,7 @@ class AreaRealEstateInfo(BaseModel): id: Optional[int] area_id: Optional[str] count_type: Optional[str] - count_value: Optional[int] + count_value: Optional[str] second_hand_housing_average_price: Optional[int] residential_land_floor_price: Optional[int] new_house_sales_price: Optional[int] @@ -16,7 +16,7 @@ class AreaRealEstateInfo(BaseModel): class AreaRealEstateAddInfo(BaseModel): area_id: str count_type: Optional[str] - count_value: Optional[int] + count_value: Optional[str] second_hand_housing_average_price: Optional[int] residential_land_floor_price: Optional[int] new_house_sales_price: Optional[int] diff --git a/ThirdPartyApi/Tianyancha/Apis.py b/ThirdPartyApi/Tianyancha/Apis.py index fc9ba0b..8d3c6d0 100644 --- a/ThirdPartyApi/Tianyancha/Apis.py +++ b/ThirdPartyApi/Tianyancha/Apis.py @@ -13,6 +13,24 @@ def make_query_url(params_dict): return "&".join(str_list) +error_code_dic = { + 0: "请求成功", + 300000: "无数据", + 300001: "请求失败", + 300002: "账号失效", + 300003: "账号过期", + 300004: "访问频率过快", + 300005: "无权限访问此api", + 300006: "余额不足", + 300007: "剩余次数不足", + 300008: "缺少必要参数", + 300009: "账号信息有误", + 300010: "URL不存在", + 300011: "此IP无权限访问此api", + 300012: "报告生成中", +} + + class TianyanchaConfig: # 公司工商信息接口 company_base_info_url = "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal" @@ -63,6 +81,8 @@ class TianyanchaApi: "data": data['result'] }) return JsDict(data['result']) + else: + raise Exception(error_code_dic[data['error_code']]) def get_list(self, api_url, keyword): """ diff --git a/ThirdPartyApi/Yujingtong/Apis.py b/ThirdPartyApi/Yujingtong/Apis.py index 4b88966..ccdbf49 100644 --- a/ThirdPartyApi/Yujingtong/Apis.py +++ b/ThirdPartyApi/Yujingtong/Apis.py @@ -1,9 +1,34 @@ from typing import List - +import requests as rq from . import Schemas from Utils.DataUtils import JsDict from Utils.MongoUtils import MongoConnect +error_code_dic = { + 0: "查询成功", + 1001: "无权限访问此数据接口", + 1002: "URL不存在", + 1003: "token异常", + 1004: "返回数据为空", + 1005: "参数设置错误或缺少参数,请重新设置", + 1006: "此数据接口剩余流量不足", + 1007: "请求数据的条目超过上限", + 1008: "请求超过系统限制", + 1009: "接口正在维护中", + 1010: "接口已下线停用", + 1011: "访问频率过快", + 1012: "请求超时", + 1013: "网络超时", + 1014: "网络错误", + 1015: "服务器拒绝请求", + 1016: "数据接收失败", + 1017: "错误的应答", + 1018: "操作系统原因", + 1019: "后台服务器不存在", + 1020: "后台服务器不可用", + 1099: "系统未知错误,请联系技术客服" +} + class YujingtongConfig: # 公司股东信息获取接口 @@ -60,6 +85,7 @@ class YujingtongApi: self.token = token self.mongo_connect = mongo_connect self.mongo_db_name = mongo_db_name + self.rq = rq def get_list(self, api_url, keyword): """ @@ -75,7 +101,7 @@ class YujingtongApi: skip += size data = self.get_data_core(url) if data: - temp_list = [JsDict(item) for item in data] + temp_list = data['data'] item_list.extend(temp_list) if len(temp_list) < size: break @@ -91,18 +117,22 @@ class YujingtongApi: if self.mongo_connect: item = self.mongo_connect.db[self.mongo_db_name].find_one({"url": full_url}) if item: - return JsDict(item['data']) - headers = {'Authorization': self.token} - res = self.rq.get(full_url, headers=headers) + data_value = item['data'] + return JsDict(data_value) + headers = {'token': self.token} + res = self.rq.get(full_url + f"&token={self.token}", headers=headers) data = res.json() if data['returncode'] == 0: - main_rating_data = data['data']['data'] + data_value = data['data'] if self.mongo_connect: self.mongo_connect.db[self.mongo_db_name].insert_one({ "url": full_url, - "data": data + "data": data_value }) - return JsDict(main_rating_data) + return JsDict(data_value) + else: + # print(full_url,error_code_dic[data['returncode']]) + raise Exception(error_code_dic[data['returncode']]) def get_company_share_holder(self, keyword) -> List[Schemas.ShareHolderItemInfo]: """