# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
from typing import List
from alibabacloud_bssopenapi20171214.client import Client as BssOpenApi20171214Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_bssopenapi20171214 import models as bss_open_api_20171214_models
import dateutil.relativedelta
import datetime
import dateutil.relativedelta
import time
import json
import os
import calendar
import psycopg2
import sys
import urllib3
import subprocess
class Sample:
def __init__(self):
pass
@staticmethod
def create_client_bj(
access_key_id: str,
access_key_secret: str,
) -> BssOpenApi20171214Client:
"""
使用AK&SK初始化账号Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 您的AccessKey ID,
access_key_id='XXXXXXXXXXXXXXXXX',
# 您的AccessKey Secret,
access_key_secret='XXXXXXXXXXXXXXXXXXXXX'
)
# 访问的域名
config.endpoint = 'business.aliyuncs.com'
return BssOpenApi20171214Client(config)
@staticmethod
def create_client_tj(
access_key_id: str,
access_key_secret: str,
) -> BssOpenApi20171214Client:
"""
使用AK&SK初始化账号Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 您的AccessKey ID,
access_key_id='XXXXXXXXXXXXXXXXXXXXX',
# 您的AccessKey Secret,
access_key_secret='XXXXXXXXXXXXXXXXXXXXXXXXXX'
)
# 访问的域名
config.endpoint = 'business.aliyuncs.com'
return BssOpenApi20171214Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
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') # 上2个月时间
last_month_sdate = datetime.datetime.strftime(date, '%Y-%m-%d')
print('北京阿里云')
client = Sample.create_client_bj('accessKeyId', 'accessKeySecret')
query_monthly_bill_request = bss_open_api_20171214_models.QueryMonthlyBillRequest(
billing_cycle=last_month
)
query_monthly_bill_request_old = bss_open_api_20171214_models.QueryMonthlyBillRequest(
billing_cycle=old_month
)
# 复制代码运行请自行打印 API 的返回值
res = client.query_monthly_bill(query_monthly_bill_request)
last_money = res.body.data.new_invoice_amount
old_res = client.query_monthly_bill(query_monthly_bill_request_old)
old_money = old_res.body.data.new_invoice_amount
growth_rate = round((last_money - old_money)/old_money,2)
print(last_month + "月费用:" + str(last_money))
return res.body.data.new_invoice_amount,last_month_sdate,last_month,growth_rate
# 复制代码运行请自行打印 API 的返回值
res = client.query_account_balance()
print("账户余额:" + str(res.body.data.available_amount))
@staticmethod
async def main_async(
args: List[str],
) -> None:
client = Sample.create_client('accessKeyId', 'accessKeySecret')
query_monthly_bill_request = bss_open_api_20171214_models.QueryMonthlyBillRequest(
billing_cycle='2021-05'
)
# 复制代码运行请自行打印 API 的返回值
await client.query_monthly_bill_async(query_monthly_bill_request)
# 复制代码运行请自行打印 API 的返回值
await client.query_account_balance_async()
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 ali(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
python实现每月自动获取阿里云账单到易快报平台提报销
于 2022-03-02 15:49:44 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)