python实现每月自动获取金山云账单到易快报平台提报销

本文介绍如何在CentOS7环境下,使用Python3和相关库,如dateutil、ksc-sdk-python和psycopg2,实现自动从金山云获取账单并导入到易快报平台进行报销。
摘要由CSDN通过智能技术生成
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))
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太生气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值