# coding: utf8 import csv from datetime import datetime import sys from pprint import pprint import os from discord_webhook import DiscordWebhook webhook = DiscordWebhook( url='https://discordapp.com/api/webhooks/744608404848246926/FwwWUNmuWsCetFDBOcA1trCx6nSflI0yX3f9BNh7-oB5a2VYSLl882di23XK2TNWwQxP', username="billing vfcz") dirpath = "." FILE = [filename for filename in os.listdir(dirpath) if filename.startswith("VFCZ_")] FILE = FILE[0] ext_date_from_filaname = FILE.split('_')[-1] print(ext_date_from_filaname) usrmonth = int(5) dictionary = {} def add_dict(id, key, value): dict = dictionary.get(id) value_ = float(value) if key in dict: dict[key] += value_ else: print("Never should happen", id) def add_dictsms(id, pricesms): dict = dictionary.get(id) print("increasing sms dict key for dict: ",dict," for :",pricesms) dict["smscount"] += pricesms def add_dictdataamount(id, dataamount): dict = dictionary.get(id) dataamount = round(float(dataamount)/1024/1024, 2) print("increasing dataamount dict key for dict: ",dict," for :",dataamount) dict["dataamount"] += dataamount #MAIN print("Start") with open(FILE, newline='', encoding="utf-8") as csvfile: reader = csv.DictReader(csvfile, delimiter=';',fieldnames=['faktura_c','phonenumber','direction','productgroup','productser','item','service','datetime','dialnum','time_taken','time_charged','dataamount','realcost','creditcost','discount' 'servicescost','chargedwovat','chargedwvat','privatecall']) for row in reader: print("Product serie: ",row['productser']) print("Time taken ",row['time_taken']) if row['phonenumber'] == '': continue #skip empty phonenumber MSISDN = int(row['phonenumber']) price = row['chargedwovat'].replace(',','.') typ = None if row['productser'] == "Používání služeb ": typ = 'data' elif row['productser'] == "SMS ": typ = "sms" else: print("groupvar: ",row['productser']) typ = 'other' if row['direction'] != "I" and row['productser'] == "SMS ": pricesms = int(row['time_taken']) print("sms price: ",pricesms,MSISDN) print("got some countsms") else: pricesms = 0 #only if not exist create new record in dictionary for ID if not MSISDN in dictionary: print("Creating dictionary: ", MSISDN) dictionary[MSISDN] = {'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms, 'dataamount':0.00} if price != '': print("Editing dictionary: ", MSISDN, "typ: ", typ, "cena: ", price) add_dict(MSISDN, typ, price) add_dictdataamount(MSISDN, row['dataamount']) if row['direction'] != "I" and row['productser'] == "SMS ": print("pricesms: ", pricesms, " for unit: ", dictionary[MSISDN]) add_dictsms(MSISDN, pricesms) else: print("direction: ", row['direction'], " and productser: ", row['productser']) print("Count of units: ", len(dictionary.keys())) with open('output_VFCZ_'+ext_date_from_filaname, 'w') as f: writer = csv.writer(f) writer.writerow(["MSISDN","ICCID","IMSI","DATA-PRICE","DATA-AMOUNT","SMS-PRICE","SMS-COUNT","OTHER-PRICE","SIM-STATUS","OPERATOR"]) for key in dictionary.keys(): print(key) print(dictionary[key]) data = dictionary[key]['data'] sms = dictionary[key]['sms'] other = dictionary[key]['other'] smscount = dictionary[key]['smscount'] dataamount = dictionary[key]['dataamount'] writer.writerow([key, "N/A", "N/A", round(float(data)), round(float(dataamount), 2), sms, smscount, other, "N/A", "VFCZ"]) with open('output_VFCZ_'+ext_date_from_filaname, "rb") as f: webhook.add_file(file=f.read(), filename='output_VFCZ_'+ext_date_from_filaname) response = webhook.execute()