diff --git a/CEDA/MacroEcon/cn.py b/CEDA/MacroEcon/cn.py index f18b960..a1f9f16 100644 --- a/CEDA/MacroEcon/cn.py +++ b/CEDA/MacroEcon/cn.py @@ -4,6 +4,7 @@ import re import demjson import requests from fake_useragent import UserAgent +from config import config # TODO need add comments diff --git a/CEDA/MacroEcon/eu.py b/CEDA/MacroEcon/eu.py index 3ff48a7..50a4a6b 100644 --- a/CEDA/MacroEcon/eu.py +++ b/CEDA/MacroEcon/eu.py @@ -1,6 +1,6 @@ import pandas as pd import numpy as np -import re +import io import demjson import requests from fake_useragent import UserAgent @@ -9,25 +9,67 @@ from fake_useragent import UserAgent url = { "eurostat": "http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/", - "ecb": "https://sdw.ecb.europa.eu/servlet/homePageChart?from=dynamic&" + "ecb": "https://sdw-wsrest.ecb.europa.eu/service/data/" } +class ecb_data(object): + def __init__(self, url=url["ecb"]): + self.url = url -def ecb_data(): - """ - Full Name: Gross Domestic Product - Description: Billions of Dollars, Quarterly, Seasonally Adjusted Annual Rate - Return: pd.DataFrame - """ - tmp_url = url["fred_econ"] - ua = UserAgent() - request_header = {"User-Agent": ua.random} - request_params = { - "id": "GDP", - "cosd": "{}".format(startdate), - "coed": "{}".format(enddate) - } - r = requests.get(tmp_url, params = request_params, headers = request_header) - data_text = r.content - df = pd.read_csv(io.StringIO(data_text.decode('utf-8'))) - return df + def codebook(self): + return "please follow the ECB's codebook: https://sdw.ecb.europa.eu/browse.do?node=9691101" + + def get_data(self, + datacode="ICP", + key="M.U2.N.000000.4.ANR", + startdate="2000-01-01", + enddate="2020-01-01"): + """ + """ + tmp_url = self.url + "{}/".format(datacode) + "{}".format(key) + ua = UserAgent() + request_header = {"User-Agent": ua.random, 'Accept': 'text/csv'} + request_params = { + "startPeriod": "{}".format(startdate), + "endPeriod": "{}".format(enddate) + } + r = requests.get(tmp_url, params = request_params, headers = request_header) + data_text = r.content + df = pd.read_csv(io.StringIO(data_text.decode('utf-8'))) + return df + +class eurostat_data(object): + def __init__(self, url=url["eurostat"]): + self.url = url + + def codebook(self): + return "please follow the EuroStat's codebook: \nhttps://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&dir=dic" + + def get_data(self, + datasetcode="nama_10_gdp", + precision="1", + unit="CP_MEUR", + na_item="B1GQ", + time="2020"): + """ + """ + tmp_url = self.url + "{}".format(datasetcode) + ua = UserAgent() + request_header = {"User-Agent": ua.random, 'Accept': 'text/csv'} + request_params = { + "precision": "{}".format(precision), + "unit": "{}".format(unit), + "na_item": "{}".format(na_item), + "time": "{}".format(time) + } + r = requests.get(tmp_url, params = request_params, headers = request_header) + data_text = r.text + data_json = demjson.decode(data_text) + value = data_json['value'] + abb = data_json['dimension']['geo']['category']['index'] + abb = {abb[k]:k for k in abb} + geo = data_json['dimension']['geo']['category']['label'] + geo_list = [abb[int(k)] for k in list(value.keys())] + geo = [geo[k] for k in geo_list] + df = pd.DataFrame({"Geo":geo, "{}".format(na_item): list(value.values())}) + return df \ No newline at end of file diff --git a/CEDA/__init__.py b/CEDA/__init__.py index 59b8c0d..61896a0 100644 --- a/CEDA/__init__.py +++ b/CEDA/__init__.py @@ -1,29 +1 @@ -from CEDA.MacroEcon.cn import ( - gdp_quarterly, - ppi_monthly, - cpi_monthly, - pmi_monthly, - fai_monthly, - hi_old_monthly, - hi_new_monthly, - ci_eei_monthly, - ig_monthly, - cgpi_monthly, - cci_csi_cei_monthly, - trscg_monthly, - ms_monthly, - ie_monthly, - stock_monthly, - fgr_monthly, - ctsf_monthly, - sao_monthly, - fdi_monthly, - gr_monthly, - ti_monthly, - nl_monthly, - dfclc_monthly, - fl_monthly, - drr_monthly, - interest_monthly, - gdc_daily -) \ No newline at end of file +from CEDA import * diff --git a/CEDA/config.py b/CEDA/config.py new file mode 100644 index 0000000..5d8d8d7 --- /dev/null +++ b/CEDA/config.py @@ -0,0 +1,15 @@ +import requests + +def config(http:str, https:str, auth:bool, user:str, passwd:str): + if auth == False: + proxies = { + "http": "{}".format(http), + "https": "{}".format(https) + } + return proxies + if auth == True: + proxies = { + "http": "http://{}:{}@{}".format(user, passwd, http), + "https": "https://{}:{}@{}".format(user, passwd, https), + } + return proxies \ No newline at end of file diff --git a/README.md b/README.md index 8ea39fd..bdcca21 100644 --- a/README.md +++ b/README.md @@ -28,4 +28,5 @@ python -m pip install CEDApy * Thanks [akshare](https://github.com/jindaxiang/akshare/) * Thanks [EastMoney](https://www.eastmoney.com) * Thanks [St.Louis Fred Reserve Bank](https://fred.stlouisfed.org/) -* Thanks [eurostat Economic Indicators](https://ec.europa.eu/eurostat/cache/infographs/economy/desktop/index.html) \ No newline at end of file +* Thanks [eurostat Economic Indicators](https://ec.europa.eu/eurostat/cache/infographs/economy/desktop/index.html) +* Thanks [Europen Central Bank](https://www.ecb.europa.eu) \ No newline at end of file