Glider
Loading...
Searching...
No Matches
GenUnmatched.py
Go to the documentation of this file.
1import ssl
2import os
3import csv
4import re
5import sys, traceback
6from datetime import datetime
7from pymongo import MongoClient
8from dotenv import load_dotenv
9from botocore.exceptions import ClientError
10from .SendEmail import send_unmatched_report
11from .UploadReports import upload_files_s3
12f_path = __file__
13index = f_path.find("report_generation/")
14f_path = f_path[:index]
15sys.path.insert(1, f_path+'importer')
16from MongoConnection import mongo_connection
17load_dotenv()
18
19
20mongo_conn = mongo_connection()
21
22collection = mongo_conn.mongo_conn_sales()
23
24
25ROOT_DIR = os.path.expanduser('~')
26
27path = ROOT_DIR+"/Reports/"
28if not os.path.exists(path):
29 os.mkdir(path)
30
31def get_month(date):
32 """
33 Generate the filename according to date in 'MonthYear' format.
34
35 Args:
36 date (str): accounting date, i.e processing files date
37 Returns: full_date (str)
38 """
39 m = re.findall(r'-\d{2}-', date)[0]
40 m = m.replace("-", "")
41 month_dic = {"01": "Jan", "02": "Feb", "03": "Mar", "04": "Apr", "05": "May", "06": "Jun", "07": "Jul", "08": "Aug", "09": "Sep", "10": "Oct", "11": "Nov", "12": "Dec"}
42 month = month_dic[m]
43 year = re.findall(r'20\d{2}-', date)[0]
44 year = year.replace("-", "")
45 return (month+year).lower()
46
47def get_num_files(csv_file):
48 """
49 Counts the total files which contains not matched lines
50
51 Args:
52 csv_file (str): path to csv report
53 Returns: num_files (int)
54 """
55 files = set()
56 with open(csv_file, newline='') as f:
57 reader = csv.reader(f, delimiter=",")
58 for row in reader:
59 files.add(row[0])
60 # print(row)
61 num_files = len(files)-1
62 return num_files
63
65 """
66 Generates the unmatched lines in compressed csv format according to sales which matches with 'accounting_date' field
67
68 Args:
69 date (str): accounting_date, i.e processing files date
70 Returns (str): Nothing.
71 """
72 date = event["date"]
73 bucket = event["bucket"]
74 # path to upload files, each month it's a different path
75 s3_path = "{}/unmatched_lines/{}/".format(event["path"], date)
76 file = get_month(date)
77 filename = f"unmatched_lines_{file}.csv"
78 print(f"Creating {filename}")
79 date = datetime.strptime(date, "%Y-%m-%d")
80 csv_columns = ["file", "line", "release_id", "isrc_id", "release_title", "track_title", "artists", "label_id", "service_id"]
81 lines = 0
82 try:
83 with open(path+filename, 'w', encoding='utf-8') as csvfile:
84 writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
85 writer.writeheader()
86 fields = {"_id":0, "file": 1, "line": 1, "release_id": 1, "isrc_id": 1, "release_title": 1, "track_title": 1, "artists": 1, "label_id": 1, "service_id": 1}
87 template_sales = collection.find({"accounting_date": {"$in": [date]}, "client_id": event["client_id"], "clean": 0},fields)
88 for post in template_sales:
89 writer.writerow(post)
90 lines+=1
91
92 num_files = get_num_files(path+filename)
93 print("Compressing file")
94 if os.path.isfile(f"{path+filename}.gz"): # Delete file if it exists to avoid duplicate files
95 os.system("rm {}.gz".format(path+filename))
96 command = f"gzip {path+filename}"
97 os.system(command) # Compress file to gzip
98 filename = filename+".gz"
99 print(f"{filename} is ready!")
100 url_to_download, final_path = upload_files_s3(bucket, s3_path, path, filename)
101 print(send_unmatched_report(event["client_email"], file, url_to_download, lines, num_files))
102 os.system("rm -r {}".format(path+filename))
103 except Exception as e:
104 print(sys.exc_info()[2])
105 print(traceback.format_exc())