-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest.py
More file actions
78 lines (59 loc) · 2.24 KB
/
Copy pathingest.py
File metadata and controls
78 lines (59 loc) · 2.24 KB
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
import pandas as pd
import plistlib
from dataclasses import dataclass, field
from typing import Optional
from pathlib import Path
@dataclass
class Track:
track_id: int
name: str
artist: str
album: str
genre: Optional[str] = None
year: Optional[int] = None
play_count: Optional[int] = None
loved: bool = False
rating: Optional[int] = None
duration: Optional[int] = None
def load_library(xml_path):
xml_path = Path(xml_path)
if not xml_path.exists():
raise FileNotFoundError(f'File whose path is {xml_path}, could not be found.')
with open(xml_path, "rb") as file:
plist = plistlib.load(file)
raw_tracks = plist.get("Tracks", {})
tracks = []
for tid, info in raw_tracks.items():
if info.get('Podcast') or info.get('AudioBook') or info.get('Has Video'):
continue
tracks.append(Track(
track_id= int(tid),
name= info.get('Name', ''),
artist= info.get('Artist', ''),
album= info.get('Album', ''),
genre= info.get('Genre'),
year= info.get('Year'),
play_count= info.get('Play Count', 0),
loved= info.get('Loved', False),
rating= info.get('Rating'),
duration= info.get('Total Time'),
))
print(f'\n[ingest] | Loaded {len(tracks)} tracks from {xml_path} successfully.')
return tracks
def filter_favourites(tracks, filter_by):
if filter_by == 'loved':
filtered = [track for track in tracks if track.loved]
elif filter_by == 'rated':
filtered = [track for track in tracks if track.rating and track.rating >= 50]
elif filter_by == 'time played':
counts = sorted([track.play_count or 0 for track in tracks])
limit = counts[int(len(counts) * 0.75)]
filtered = [track for track in tracks if (track.play_count or 0) >= limit]
elif filter_by == 'all':
filtered = tracks
else:
raise ValueError(f'Could not filter by that feature: {filter_by}')
print(f'[filter] | Filtered the list of tracks by {filter_by} giving {len(filtered)} items.')
return filtered
def convert_dataframe(tracks):
return pd.DataFrame([vars(track) for track in tracks])