pyfmframework/fmframework/io/csv.py

30 lines
1.1 KiB
Python
Raw Normal View History

2019-10-03 19:06:44 +01:00
from csv import DictWriter
2019-03-16 02:34:09 +00:00
import datetime
2019-10-03 19:06:44 +01:00
import logging
from typing import List
from fmframework.model import Scrobble
2019-10-03 19:06:44 +01:00
logger = logging.getLogger(__name__)
2019-03-16 02:34:09 +00:00
headers = ['track', 'album', 'artist', 'time', 'track id', 'album id', 'artist id']
2019-10-03 19:06:44 +01:00
def export_scrobbles(scrobbles: List[Scrobble], path: str):
logger.info(f'dumping {len(scrobbles)} to {path}')
date = str(datetime.date.today())
2019-03-16 02:34:09 +00:00
2019-03-17 00:00:38 +00:00
with open('{}/{}_scrobbles.csv'.format(path, date), 'w') as fileobj:
2019-03-16 02:34:09 +00:00
2019-10-03 19:06:44 +01:00
writer = DictWriter(fileobj, fieldnames=headers)
2019-03-16 02:34:09 +00:00
writer.writeheader()
2019-10-03 19:06:44 +01:00
for scrobble in scrobbles:
writer.writerow({
'track': scrobble.track.name.replace(';', '_').replace(',', '_'),
'album': scrobble.track.album.name.replace(';', '_').replace(',', '_'),
'artist': scrobble.track.artist.name.replace(';', '_').replace(',', '_'),
'time': scrobble.time,
'track id': scrobble.track.mbid,
'album id': scrobble.track.album.mbid,
'artist id': scrobble.track.artist.mbid
})