234 lines
6.6 KiB
Python
234 lines
6.6 KiB
Python
# 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 tmcz")
|
||
|
||
|
||
|
||
dirpath = "."
|
||
FILE = [filename for filename in os.listdir(dirpath) if filename.startswith("TMCZ_")]
|
||
FILE = FILE[0]
|
||
|
||
ext_date_from_filaname = FILE.split('_')[-1]
|
||
print(ext_date_from_filaname)
|
||
|
||
|
||
# get month to be processed
|
||
|
||
head, mid, tail = ext_date_from_filaname.partition('.')
|
||
print(head)
|
||
usrmonth = head[-2:]
|
||
print(usrmonth)
|
||
|
||
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)
|
||
|
||
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=';')
|
||
for row in reader:
|
||
|
||
|
||
|
||
#print(row)
|
||
if row['Telefonní číslo'] == '':
|
||
continue #skip empty MSISDN
|
||
|
||
if row['Začátek fakturačního období'] == '':
|
||
continue
|
||
|
||
my_string = row['Začátek fakturačního období']
|
||
|
||
#my_string = '2021-09-06'
|
||
print("start date: ",my_string)
|
||
|
||
monthfromfile = my_string.split(".", 3)
|
||
print(monthfromfile[1])
|
||
monthfromfile = monthfromfile[1]
|
||
|
||
|
||
|
||
|
||
try:
|
||
my_date = datetime.strptime(my_string, "%d.%m.%Y")
|
||
except:
|
||
print("An exception occurred, current my_string is: ", my_string)
|
||
sys.exit(1)
|
||
|
||
|
||
#print(row['BILLED_PERIOD_START'])
|
||
#print(my_date.month)
|
||
|
||
|
||
if usrmonth != monthfromfile:
|
||
print("skipping bad date")
|
||
|
||
continue #skip bad dates
|
||
|
||
|
||
|
||
|
||
MSISDN = int(row['Telefonní číslo'])
|
||
#ICCID = int(row['ICCID'])
|
||
price = row['Účtovaná částka'].replace(',','.')
|
||
|
||
#price = round(price, 5)
|
||
|
||
typ = None
|
||
|
||
if row['Název destinace/služby'] == "Měsíční paušál za tarif":
|
||
typ = 'data'
|
||
elif row['Název destinace/služby'] == "SMS T-Mobile":
|
||
typ = "sms"
|
||
elif row['Název destinace/služby'] == "SMS ostatní sítě":
|
||
typ = "sms"
|
||
elif row['Název destinace/služby'] == "SMS v zahraničí - EU Roaming":
|
||
typ = "sms"
|
||
elif row['Název destinace/služby'] == "SMS mezinárodní":
|
||
typ = "sms"
|
||
elif row['Název destinace/služby'] == "SMS v zahraničí - T-Mobile Roam., zóna 2": typ = "sms"
|
||
elif row['Název destinace/služby'] == "Datový provoz v zahr. - T-Mobile Roam., zóna 2":
|
||
typ = "data"
|
||
elif row['Název destinace/služby'] == "Datový provoz (z tarifu, balíčků, navýšení datového limitu)":
|
||
typ = "data"
|
||
elif row['Název destinace/služby'] == "Datový provoz v zahr. - T-Mobile Roam., zóna 3":
|
||
typ = "data"
|
||
elif row['Název destinace/služby'] == "Datový roaming Evropa":
|
||
typ = "data"
|
||
elif row['Název destinace/služby'] == "Intranet v mobilu":
|
||
typ = "data"
|
||
else:
|
||
typ = 'other'
|
||
|
||
if row['Jednotka'] == "ks":
|
||
|
||
pricesms = int(row['Počet jednotek – volné']) + int(row['Počet jednotek – účtované'])
|
||
print("rowjednotka: ",row['Jednotka'])
|
||
print("sms price: ",pricesms,MSISDN)
|
||
|
||
else:
|
||
pricesms = 0
|
||
|
||
if row['Jednotka'] == "kB":
|
||
|
||
dataamount = (int(row['Počet jednotek – volné']) + int(row['Počet jednotek – účtované'])) / 1024
|
||
print("rowjednotka: ",row['Jednotka'])
|
||
print("sms price: ",pricesms,MSISDN)
|
||
|
||
else:
|
||
dataamount = 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':dataamount}
|
||
|
||
if price != '':
|
||
print("Editing dictionary: ", MSISDN, "typ: ", typ, "cena: ", price)
|
||
|
||
|
||
add_dict(MSISDN, typ, price)
|
||
if row['Jednotka'] == "ks":
|
||
|
||
add_dictsms(MSISDN, pricesms)
|
||
|
||
if row['Jednotka'] == "kB":
|
||
add_dictdataamount(MSISDN, dataamount)
|
||
|
||
|
||
#try:
|
||
with open('output_TMCZ_'+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']
|
||
dataamount = dictionary[key]['dataamount']
|
||
|
||
|
||
|
||
|
||
writer.writerow(["420"+str(key), "N/A", "N/A", data, dataamount, sms, smscount, other, "N/A", "TMCZ"])
|
||
|
||
|
||
|
||
|
||
f.close()
|
||
|
||
#[436761890319959]) #one with both types
|
||
#except:
|
||
#print("Required unit is not in our set")
|
||
#sys.exit(1)
|
||
|
||
#print(len(dictionary.keys()))
|
||
|
||
#for key, value in dictionary.items():
|
||
#print(key, ' : ', value)
|
||
|
||
|
||
print("Count of units: ", len(dictionary.keys()))
|
||
|
||
|
||
|
||
|
||
|
||
with open('output_TMCZ_'+ext_date_from_filaname, "rb") as f:
|
||
webhook.add_file(file=f.read(), filename='output_TMCZ_'+ext_date_from_filaname)
|
||
|
||
response = webhook.execute()
|
||
|
||
|
||
|
||
"""
|
||
csv_columns = ['takhleuztoneudelam', 'data', 'sms', 'other', 'smscount']
|
||
try:
|
||
with open('output_TMCZ_'+ext_date_from_filaname, 'w') as csvfile:
|
||
w = csv.DictWriter(sys.stdout, csv_columns)
|
||
for key, val in sorted(dictionary.items()):
|
||
row = {'takhleuztoneudelam': key}
|
||
row.update(val)
|
||
|
||
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
|
||
writer.writerow(row)
|
||
|
||
except IOError:
|
||
print(f"I/O error: {IOError}")
|
||
|
||
""" |