Skip to content
This repository has been archived by the owner on Jul 11, 2021. It is now read-only.

Commit

Permalink
Handle non-existent log file or empty logs in auto playlist
Browse files Browse the repository at this point in the history
Previously, errors weren't handled.
  • Loading branch information
NISH1001 committed May 26, 2020
1 parent af52154 commit 27019a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 11 additions & 3 deletions playx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,17 @@ def main():

# first check for auto playlist
elif args.auto:
# ap = CountBasedAutoPlaylist('~/.playx/logs/log.cat')
ap = MarkovBasedAutoPlaylist("~/.playx/logs/log.cat")
song = ap.generate()
try:
# ap = CountBasedAutoPlaylist('~/.playx/logs/log.cat')
log_path = "~/.playx/logs/log.cat"
ap = MarkovBasedAutoPlaylist(log_path)
song = ap.generate()
except (AssertionError):
logger.info("Unable to generate playlist...")
logger.info(
f"Either log at [{log_path}] is not in existence or is a pure void... :/"
)
exit(0)
else:
song = ""

Expand Down
5 changes: 5 additions & 0 deletions playx/playlist/autoplaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
class AbstractAutoPlaylist(ABC):
def __init__(self, log_path):
self.log_path = pathlib.Path(log_path).expanduser()
assert self.log_path.exists()
self.data = []

@abstractmethod
Expand All @@ -44,6 +45,8 @@ def get_timeseries_data(self, log_path):
with open(log_path) as f:
for line in f:
line = line.strip().lower()
if not line:
continue
if "playing" not in line:
continue
matches = re.findall(r"\[.*?\]", line)
Expand Down Expand Up @@ -71,6 +74,7 @@ def __init__(self, log_path):

def generate(self):
data = self.get_timeseries_data(self.log_path)
assert data
ts, songs = zip(*data)
counter = Counter(songs)
songs_frequent, c = zip(*counter.most_common())
Expand Down Expand Up @@ -101,6 +105,7 @@ def __init__(self, log_path):

def generate(self):
data = self.get_timeseries_data(self.log_path)
assert data
ts, songs = zip(*data)

# build trie and probabilities
Expand Down

0 comments on commit 27019a4

Please sign in to comment.