-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpm_file_parser.py
70 lines (48 loc) · 1.47 KB
/
gpm_file_parser.py
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
import csv
import html
import json
import logging
class Song:
def __init__(self, title, artist, album):
self._title = title
self._artist = artist
self._album = album
def __str__(self):
return f"{self._title} - {self._album} by {self._artist}"
@property
def title(self):
return self._title
@property
def artist(self):
return self._artist
@property
def album(self):
return self._album
def to_dict(self):
return {
"title": self.title,
"artist": self.artist,
"album": self.album,
}
class GpmFileParser:
def __init__(self):
self._logger = logging.getLogger("gpm2spotify")
def _csv2json(self, csv_file):
"""Converts a CSV file to JSON object
:param csv_file: String, File Path to the CSV file
:returns: JSON Object
"""
with open(csv_file, "r") as csv_file_handle:
reader = csv.DictReader(csv_file_handle)
return json.dumps([row for row in reader])
def parse_file(self, csv_file):
try:
song = self._csv2json(csv_file)
song = json.loads(song)[0]
return Song(
html.unescape(song["Title"]),
html.unescape(song["Artist"]),
html.unescape(song["Album"])
)
except Exception as e:
self._logger.exception(f"Can't parse {csv_file}")