Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for "date added" and "favourite" fields #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions itunestoND.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ def determine_userID(nd_p):
conn.close()
return users[0][0]

def update_playstats(d1, id, playcount, playdate, rating=0):
def update_playstats(d1, id, playcount, playdate, dateadded, rating=0, loved=0):
d1.setdefault(id, {})
d1[id].setdefault('play count', 0)
d1[id].setdefault('play date', datetime.datetime.fromordinal(1))
d1[id].setdefault('date added', datetime.datetime.fromordinal(1))
d1[id]['play count'] += playcount
d1[id]['rating'] = rating
d1[id]['loved'] = loved

if playdate > d1[id]['play date']: d1[id].update({'play date': playdate})
if dateadded > d1[id]['date added']: d1[id].update({'date added': dateadded})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if dateadded > d1[id]['date added']: d1[id].update({'date added': dateadded})
if dateadded > datetime.datetime.fromordinal(1) and dateadded < d1[id]['date added']: d1[id].update({'date added': dateadded})

I think you want to use the earliest date that is defined. All my files had the date I initially imported the files from the disc.


def generate_annotation_id(): # random hex number 32 characters long
character_pool = string.hexdigits[:16]
Expand All @@ -53,8 +56,9 @@ def write_to_annotation(dictionary_with_stats, entry_type):
play_count = this_entry['play count']
play_date = this_entry['play date'].strftime('%Y-%m-%d %H:%M:%S') # YYYY-MM-DD 24:mm:ss
rating = this_entry['rating']
loved = this_entry['loved']

annotation_entries.append((generate_annotation_id(), userID, item_id, entry_type, play_count, play_date, rating, 0, None))
annotation_entries.append((generate_annotation_id(), userID, item_id, entry_type, play_count, play_date, rating, loved, None))

conn = sqlite3.connect(nddb_path)
cur = conn.cursor()
Expand All @@ -64,6 +68,16 @@ def write_to_annotation(dictionary_with_stats, entry_type):
# cur.executemany('INSERT INTO consumers VALUES (?,?,?,?)', purchases)
# cur.execute("INSERT INTO consumers VALUES (1,'John Doe','[email protected]','A')")

def update_created_at_fields(dictionary_with_stats):
conn = sqlite3.connect(nddb_path)
cur = conn.cursor()

for item_id in dictionary_with_stats:
this_entry = dictionary_with_stats[item_id]
cur.execute('UPDATE media_file SET created_at = ? WHERE id = ?', (this_entry['date added'], item_id))
conn.commit()
conn.close()

_ = None
while _ != 'proceed':
print()
Expand Down Expand Up @@ -134,6 +148,10 @@ def write_to_annotation(dictionary_with_stats, entry_type):
it_song_ID = int(it_song_entry.find('key', string='Track ID').next_sibling.text)
songID_correlation.update({it_song_ID: song_id})

loved = it_song_entry.find('key', string='Loved')
if(loved != None): loved = 1
else: loved = 0

try: # get rating, play count & date from Itunes
song_rating = int(it_song_entry.find('key', string='Rating').next_sibling.text)
song_rating = int(song_rating / 20)
Expand All @@ -143,13 +161,13 @@ def write_to_annotation(dictionary_with_stats, entry_type):
play_count = int(it_song_entry.find('key', string='Play Count').next_sibling.text)
last_played = it_song_entry.find('key', string='Play Date UTC').next_sibling.text[:-1] # slice off the trailing 'Z'
last_played = datetime.datetime.strptime(last_played, '%Y-%m-%dT%H:%M:%S') # convert from string to datetime object. Example string: '2020-01-19T02:24:14Z'
date_added = it_song_entry.find('key', string='Date Added').next_sibling.text[:-1]
date_added = datetime.datetime.strptime(date_added, '%Y-%m-%dT%H:%M:%S')
except AttributeError: continue

update_playstats(artists, artist_id, play_count, last_played)
update_playstats(albums, album_id, play_count, last_played)
update_playstats(files, song_id, play_count, last_played, rating=song_rating)


update_playstats(artists, artist_id, play_count, last_played, date_added)
update_playstats(albums, album_id, play_count, last_played, date_added)
update_playstats(files, song_id, play_count, last_played, date_added, rating=song_rating, loved=loved)

conn.close()

Expand All @@ -161,6 +179,8 @@ def write_to_annotation(dictionary_with_stats, entry_type):
write_to_annotation(albums, 'album')
print('Album records saved to database.')

update_created_at_fields(files)

with open('IT_file_correlations.py', 'w') as f:
f.write('# Following python dictionary correlates the itunes integer ID to the Navidrome file ID for each song.\n')
f.write('# {ITUNES ID: ND ID} is the format. \n\n')
Expand Down