Calc uniq ID for artist & album + other improvment
Class method + rename
This commit is contained in:
@@ -45,6 +45,7 @@ import os
|
|||||||
import plistlib
|
import plistlib
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
class SetEncoder(json.JSONEncoder):
|
class SetEncoder(json.JSONEncoder):
|
||||||
@@ -135,8 +136,9 @@ class ITunesParser:
|
|||||||
akey = track['Artist']
|
akey = track['Artist']
|
||||||
# Add artist
|
# Add artist
|
||||||
if akey not in self._artists:
|
if akey not in self._artists:
|
||||||
|
a_id = self.calc_id(akey)
|
||||||
self._artists[akey] = {
|
self._artists[akey] = {
|
||||||
'id': len(self._artists), # TODO Calc an uniq ID (permit to replace data)
|
'Persistent ID': a_id,
|
||||||
'Name': akey,
|
'Name': akey,
|
||||||
'Track Count': 0,
|
'Track Count': 0,
|
||||||
'Play Count': 0,
|
'Play Count': 0,
|
||||||
@@ -149,7 +151,7 @@ class ITunesParser:
|
|||||||
# TODO If no Rating, don't take 0
|
# TODO If no Rating, don't take 0
|
||||||
play_count = track['Play Count'] if 'Play Count' in track else 0
|
play_count = track['Play Count'] if 'Play Count' in track else 0
|
||||||
|
|
||||||
rating = self._calc_rating(rating, self._artists[akey]['Rating'], self._artists[akey]['Track Count'])
|
rating = self.calc_rating(rating, self._artists[akey]['Rating'], self._artists[akey]['Track Count'])
|
||||||
|
|
||||||
self._artists[akey]['Track Count'] += 1
|
self._artists[akey]['Track Count'] += 1
|
||||||
self._artists[akey]['Rating'] = rating
|
self._artists[akey]['Rating'] = rating
|
||||||
@@ -173,14 +175,15 @@ class ITunesParser:
|
|||||||
|
|
||||||
akey = track['Album']
|
akey = track['Album']
|
||||||
if akey not in self._albums:
|
if akey not in self._albums:
|
||||||
|
a_id = self.calc_id(akey)
|
||||||
self._albums[akey] = {
|
self._albums[akey] = {
|
||||||
'id': len(self._albums),
|
'Persistent ID': a_id,
|
||||||
'Name': akey,
|
'Name': akey,
|
||||||
'Track Count': 0,
|
'Track Count': 0,
|
||||||
'Play Count': 0,
|
'Play Count': 0,
|
||||||
'Rating': 0,
|
'Rating': 0,
|
||||||
'Genre': set(),
|
'Genre': set(),
|
||||||
'artist': set()
|
'Artist': set()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Compute information
|
# Compute information
|
||||||
@@ -189,7 +192,7 @@ class ITunesParser:
|
|||||||
# TODO If no Rating, don't take 0
|
# TODO If no Rating, don't take 0
|
||||||
play_count = track['Play Count'] if 'Play Count' in track else 0
|
play_count = track['Play Count'] if 'Play Count' in track else 0
|
||||||
|
|
||||||
rating = self._calc_rating(rating, self._albums[akey]['Rating'], self._albums[akey]['Track Count'])
|
rating = self.calc_rating(rating, self._albums[akey]['Rating'], self._albums[akey]['Track Count'])
|
||||||
|
|
||||||
self._albums[akey]['Track Count'] += 1
|
self._albums[akey]['Track Count'] += 1
|
||||||
self._albums[akey]['Rating'] = rating
|
self._albums[akey]['Rating'] = rating
|
||||||
@@ -205,7 +208,7 @@ class ITunesParser:
|
|||||||
## Add different artists
|
## Add different artists
|
||||||
if 'Artist' not in track:
|
if 'Artist' not in track:
|
||||||
return
|
return
|
||||||
self._albums[akey]['artist'].add(track['Artist'])
|
self._albums[akey]['Artist'].add(track['Artist'])
|
||||||
return
|
return
|
||||||
|
|
||||||
def _write_artists(self):
|
def _write_artists(self):
|
||||||
@@ -215,8 +218,9 @@ class ITunesParser:
|
|||||||
|
|
||||||
file_artist = io.open('es-artist-data.json', 'wb')
|
file_artist = io.open('es-artist-data.json', 'wb')
|
||||||
for artist in self._artists:
|
for artist in self._artists:
|
||||||
|
persistent_id = self._artists[artist]['Persistent ID']
|
||||||
json_track_index = {
|
json_track_index = {
|
||||||
"index": {"_index": "itunessongs", "_type": "artist"}
|
"index": {"_index": "itunessongs", "_type": "artist", "_id": persistent_id}
|
||||||
}
|
}
|
||||||
file_artist.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
|
file_artist.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
|
||||||
file_artist.write(bytes("\n", 'UTF-8'))
|
file_artist.write(bytes("\n", 'UTF-8'))
|
||||||
@@ -231,8 +235,9 @@ class ITunesParser:
|
|||||||
|
|
||||||
file_albums = io.open('es-albums-data.json', 'wb')
|
file_albums = io.open('es-albums-data.json', 'wb')
|
||||||
for album in self._albums:
|
for album in self._albums:
|
||||||
|
persistent_id = self._albums[album]['Persistent ID']
|
||||||
json_track_index = {
|
json_track_index = {
|
||||||
"index": {"_index": "itunessongs", "_type": "album"}
|
"index": {"_index": "itunessongs", "_type": "album", "_id": persistent_id}
|
||||||
}
|
}
|
||||||
file_albums.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
|
file_albums.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
|
||||||
file_albums.write(bytes("\n", 'UTF-8'))
|
file_albums.write(bytes("\n", 'UTF-8'))
|
||||||
@@ -240,9 +245,22 @@ class ITunesParser:
|
|||||||
file_albums.write(bytes("\n", 'UTF-8'))
|
file_albums.write(bytes("\n", 'UTF-8'))
|
||||||
file_albums.close()
|
file_albums.close()
|
||||||
|
|
||||||
def _calc_rating(self, added_value, current_rating, count):
|
@classmethod
|
||||||
|
def calc_rating(cls, added_value, current_rating, count):
|
||||||
|
"""
|
||||||
|
Calculate average rating from a current rating, a rating value to add and the number of elements
|
||||||
|
"""
|
||||||
return (current_rating * count + added_value) // (count + 1)
|
return (current_rating * count + added_value) // (count + 1)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def calc_id(cls, key):
|
||||||
|
"""
|
||||||
|
Calculate a MD5 sum from a key as ID
|
||||||
|
"""
|
||||||
|
md5 = hashlib.md5()
|
||||||
|
md5.update(key.encode('UTF-8'))
|
||||||
|
return md5.hexdigest()
|
||||||
|
|
||||||
#### main block ####
|
#### main block ####
|
||||||
|
|
||||||
# Default input & output files
|
# Default input & output files
|
||||||
|
|||||||
Reference in New Issue
Block a user