-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
135 lines (110 loc) · 4.65 KB
/
Program.cs
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
135
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using CsvHelper;
using CsvHelper.Configuration;
namespace osuuspankki_import
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("\nError: Please give either a file name or an absolute directory path to transform.");
Console.WriteLine("Eg. dotnet run new-file.csv");
Console.WriteLine("Eg. dotnet run new-files");
return;
}
var filesToImport = GetFilesToImport(args[0]);
Console.WriteLine($"\nFound files ({filesToImport.Count()}):");
foreach(var fileName in filesToImport)
{
Console.WriteLine(fileName);
}
Console.Write($"\nType 'yes' if you wish to transform all {filesToImport.Count()} files listed above: ");
var input = Console.ReadLine();
if (input != "yes") {
return;
}
foreach(var fileName in filesToImport)
{
var transformedFileName = GetTransformedFileName(fileName);
TransformCsv(fileName, transformedFileName);
}
}
private static IEnumerable<string> GetFilesToImport(string searchPath) {
var filesToImport = new List<string>();
if (Directory.Exists(searchPath))
{
var path = searchPath;
var files = Directory.GetFiles(path, "*.csv");
filesToImport.AddRange(files);
} else
{
var fileName = searchPath;
filesToImport.Add(fileName);
}
return filesToImport.Select(file => Path.GetFullPath(file));
}
private static void TransformCsv(string fileName, string transformedFileName)
{
var configuration = new Configuration()
{
BadDataFound = (data) => Console.WriteLine("Error:" + data),
Delimiter = ";"
};
configuration.RegisterClassMap<OsuuspankkiRowMap>();
var rows = new List<OsuuspankkiRow>();
Console.WriteLine($"Reading rows from {fileName}...\n");
using (var streamReader = new StreamReader(fileName, Encoding.GetEncoding("ISO-8859-1")))
using (var csvReader = new CsvReader(streamReader, configuration))
{
// Ignore header
csvReader.Read();
csvReader.ReadHeader();
rows = csvReader.GetRecords<OsuuspankkiRow>().ToList();
}
foreach (var row in rows.Take(5))
{
Console.WriteLine($"{row.Date} {row.Amount} {row.Payee} {row.PaymentTypeExplanation}");
}
Console.WriteLine("...");
Console.WriteLine($"\nRead {rows.Count} lines from {fileName}.");
Console.WriteLine($"\nWriting to {transformedFileName}...\n");
using (var stream = File.Create(transformedFileName))
using (var streamWriter = new StreamWriter(stream))
{
foreach (var row in rows)
{
var result = MapOsuuspankkiRowToHomebankRow(row);
Console.WriteLine(result);
streamWriter.WriteLine(result);
}
}
Console.WriteLine();
}
private static string GetTransformedFileName(string fileName)
{
var splitFileName = fileName.Split('.');
return splitFileName.First() + "-transformed" + "." + splitFileName.Last();
}
private static HomeBankImportRow MapOsuuspankkiRowToHomebankRow(OsuuspankkiRow OsuuspankkiRow) {
var homebankRow = new HomeBankImportRow();
homebankRow.Date = OsuuspankkiRow.Date;
if( OsuuspankkiRow.PaymentTypeExplanation == "MAKSUPALVELU" ||
OsuuspankkiRow.PaymentTypeExplanation == "TILISIIRTO") {
homebankRow.Payment = PaymentType.Transfer;
}
else if(OsuuspankkiRow.PaymentTypeExplanation == "PKORTTIMAKSU") {
homebankRow.Payment = PaymentType.DebitCard;
}
homebankRow.Amount = decimal.Parse(OsuuspankkiRow.Amount, CultureInfo.InvariantCulture);
homebankRow.Payee = OsuuspankkiRow.Payee;
return homebankRow;
}
}
}