#!/usr/bin/py

import json
from requests import get
from pandas import DataFrame

def balance_data(code,years):
    #query = 'http://HKf10.eastmoney.com/F9HKStock/GetFinanceAssetData.do?securityCode={}.HK&comType=127000000606281483&yearList={}&reportTypeList=1,5,3,6&dateSearchType=1&listedType=0,1&reportTypeInScope=1&reportType=0&rotate=0&seperate=0&order=desc&cashType=1&exchangeValue=1&customSelect=0&CurrencySelect=0'.format(code,years)
    query = 'http://emweb.securities.eastmoney.com/PC_HKF10/NewFinancialAnalysis/GetZCFZB?code={}&startdate={}&ctype=4&rtype=0'.format(code,years)
    ct = get(query).text.replace('\\u0026nbsp','')
    ct = ct.replace('\\u003cb\\u003e','').replace('\\u003c/b\\u003e','')
    d = json.loads(ct)['data']
    df = DataFrame(d)
    print(df)
    hdr = df.iloc[0,:].values
    hdr[0] = "名目"
    df.columns = hdr
    df = df.iloc[1:,:]
    df.index = df.iloc[:,0]
    df = df.iloc[:,1:]
    df = df.sort_values('截止日期',axis=1,ascending=False)
    return df

def income_data(code,years):
    query = 'http://hkf10.eastmoney.com/F9HKStock/GetFinanceProfitData.do?securityCode={c}.HK&comType=127000000606281483&yearList={ny}&reportTypeList=1,5,3,6&dateSearchType=1&listedType=0,1&reportTypeInScope=1&reportType=0&rotate=0&seperate=0&order=desc&cashType=1&exchangeValue=1&customSelect=0&CurrencySelect=0'.format(c=code,ny=years)
    ct = get(query).text.replace('\\u0026nbsp','')
    ct = ct.replace('\\u003cb\\u003e','').replace('\\u003c/b\\u003e','')
    d = json.loads(ct)['resultList']
    df = DataFrame(d)
    hdr = df.iloc[0,:].values
    hdr[0] = "名目"
    df.columns = hdr
    df = df.iloc[1:,:]
    df.index = df.iloc[:,0]
    df = df.iloc[:,1:]
    df = df.sort_values('截止日期',axis=1,ascending=False)
    return df

def cash_data(code,years):
    query = 'http://hkf10.eastmoney.com/F9HKStock/GetFinanceCashflowData.do?securityCode={c}.HK&comType=127000000606281483&yearList={ny}&reportTypeList=1,5,3,6&dateSearchType=1&listedType=0,1&reportTypeInScope=1&reportType=0&rotate=0&seperate=0&order=desc&cashType=1&exchangeValue=1&customSelect=0&CurrencySelect=0'.format(c=code,ny=years)
    ct = get(query).text.replace('\\u0026nbsp','')
    ct = ct.replace('\\u003cb\\u003e','').replace('\\u003c/b\\u003e','')
    d = json.loads(ct)['resultList']
    df = DataFrame(d)
    hdr = df.iloc[0,:].values
    hdr[0] = "名目"
    df.columns = hdr
    df = df.iloc[1:,:]
    df.index = df.iloc[:,0]
    df = df.iloc[:,1:]
    df = df.sort_values('截止日期',axis=1,ascending=False)    
    return df
