from kscore.session import get_session
import json
import datetime
import dateutil.relativedelta
import calendar
import time
import os
import psycopg2
import sys
import urllib3
import subprocess
def main():
now = datetime.datetime.now()
date = now + dateutil.relativedelta.relativedelta(months=-1)
old_date = now + dateutil.relativedelta.relativedelta(months=-2)
last_month = datetime.datetime.strftime(date, '%Y-%m') #上个月时间
old_month = datetime.datetime.strftime(old_date, '%Y-%m') #上个月时间
last_month_sdate = datetime.datetime.strftime(date, '%Y-%m-%d')
lsit1 = [],
lsit1.__len__()
ak = 'XXXXXXXXXXXXXXXXXXXXXX' #
sk = 'XXXXXXXXXXXXXXXXXXXXXX' #
region = 'cn-beijing-6'
s = get_session()
client1 = s.create_client("kingpay", region, ks_access_key_id=ak, ks_secret_access_key=sk, use_ssl=True)
result1 = client1.query_cash_wallet_action()
print('金山云')
print("账户余额:" + str(result1["data"]["availableAmount"]))
client = s.create_client("bill-union", region, ks_access_key_id=ak, ks_secret_access_key=sk, use_ssl=True)
result = client.describe_bill_summary_by_pay_mode(BillBeginMonth=last_month, BillEndMonth=last_month)
last_money = float(result["RealTotalCost"])
print(last_month + "月费用:" + str(last_money))
old_result = client.describe_bill_summary_by_pay_mode(BillBeginMonth=old_month, BillEndMonth=old_month)
old_money = float(old_result["RealTotalCost"])
growth_rate = round((last_money - old_money)/old_money,2)
return last_money,last_month_sdate,last_month,growth_rate
def ekuaibao(url,name,var='id'):
dev_null = open(os.devnull, 'w')
print(url)
response = subprocess.check_output(url, shell=True, stderr=dev_null).decode('utf-8').replace('\n', '')
res = json.loads(response)
# print(res)
for i in res['items']:
# print(i['name'])
if i['name'] == name:
return i[var]
def ks(total,sdate,last_month,growth_rate):
datetime_now = datetime.datetime.strptime(sdate, '%Y-%m-%d').date()
end = calendar.monthrange(datetime_now.year, datetime_now.month)[1]
end_date = '%s-%s-%s' % (datetime_now.year, datetime_now.month, end)
start_date = '%s-%s-01' % (datetime_now.year, datetime_now.month)
title_date = datetime.datetime.strptime(start_date, '%Y-%m-%d').date().strftime(
"%Y%m%d") + '-' + datetime.datetime.strptime(end_date, '%Y-%m-%d').date().strftime("%Y%m%d")
start_date_stamp = int(datetime.datetime.strptime(start_date, '%Y-%m-%d').timestamp() * 1000)
end_date_stamp = int(datetime.datetime.strptime(end_date, '%Y-%m-%d').timestamp() * 1000)
print(title_date, start_date_stamp, end_date_stamp)
t = time.time()
now = int(round(t * 1000))
python实现每月自动获取金山云账单到易快报平台提报销
于 2022-03-02 15:58:49 首次发布
本文介绍如何在CentOS7环境下,使用Python3和相关库,如dateutil、ksc-sdk-python和psycopg2,实现自动从金山云获取账单并导入到易快报平台进行报销。
摘要由CSDN通过智能技术生成