1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| import sys, datetime import time import json import urllib import hmac from hashlib import sha1 import base64 import random import requests
D = { 'BillingCycle':str(time.strftime("%Y-%m", time.gmtime())), 'Action':'QuerySettleBill', 'Format':'JSON', 'Version':'2017-12-14', 'AccessKeyId':'<AccessKeyId>', 'SignatureMethod':'HMAC-SHA1', 'MaxResults' : '300', 'Timestamp':str(time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())), 'SignatureVersion':'1.0' }
now_time = str(time.strftime("%Y-%m-%d", time.gmtime()))
datakit = DFF.SRC('datakit')
def percentEncode(str): res = urllib.parse.quote(str.encode('utf8'), '') res = res.replace('+', '%20') res = res.replace('*', '%2A') res = res.replace('%7E', '~') return res
@DFF.API('getBill') def getBill(): next_token = "" for i in range(10000): random.seed() D["SignatureNonce"] = str(random.random()) D["NextToken"] = next_token sortedD = sorted(D.items(),key=lambda x: x[0]) canstring = '' for k,v in sortedD: canstring += '&' + percentEncode(k) + '=' + percentEncode(v) stringToSign = 'GET&%2F&' + percentEncode(canstring[1:]) access_key_secret = '<access_key_secret>' h = hmac.new((access_key_secret + "&").encode('utf8'), stringToSign.encode('utf8'), sha1) signature = base64.encodestring(h.digest()).strip() D['Signature'] = signature url = 'http://business.aliyuncs.com/?' + urllib.parse.urlencode(D) response = requests.get(url) billing_cycle = response.json()["Data"]["BillingCycle"] account_id = response.json()["Data"]["AccountID"] next_token = response.json()["Data"]["NextToken"] if next_token is not None: bill = response.json()["Data"]["Items"]["Item"] print(bill) for i in bill: print(i["UsageEndTime"]) time = i["UsageEndTime"].split(" ")[0] print(time,now_time) if time == now_time: measurement = "aliyunSettleBill" tags = { "BillingCycle":billing_cycle, "AccountID":account_id } fields = { "ProductName":i["ProductName"], "SubOrderId":i["SubOrderId"], "BillAccountID":i["BillAccountID"], "DeductedByCashCoupons":i["DeductedByCashCoupons"], "PaymentTime":i["PaymentTime"], "PaymentAmount":i["PaymentAmount"], "DeductedByPrepaidCard":i["DeductedByPrepaidCard"], "InvoiceDiscount":i["InvoiceDiscount"], "UsageEndTime":i["UsageEndTime"], "Item":i["Item"], "SubscriptionType":i["SubscriptionType"], "PretaxGrossAmount":i["PretaxGrossAmount"], "Currency":i["Currency"], "CommodityCode":i["CommodityCode"], "UsageStartTime":i["UsageStartTime"], "AdjustAmount":i["AdjustAmount"], "Status":i["Status"], "DeductedByCoupons":i["DeductedByCoupons"], "RoundDownDiscount":i["RoundDownDiscount"], "ProductDetail":i["ProductDetail"], "ProductCode":i["ProductCode"], "ProductType":i["ProductType"], "OutstandingAmount":i["OutstandingAmount"], "BizType":i["BizType"], "PipCode":i["PipCode"], "PretaxAmount":i["PretaxAmount"], "OwnerID":i["OwnerID"], "BillAccountName":i["BillAccountName"], "RecordID":i["RecordID"], "CashAmount":i["CashAmount"], } try: status_code, result = datakit.write_logging(measurement=measurement, tags=tags, fields=fields) print(status_code,result) except: print("插入失败!") else: break else: continue break else: break
|