-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenre
90 lines (83 loc) · 2.11 KB
/
genre
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use Data::Dumper::Compact qw(ddc);
use List::Util qw(uniq);
use Storable qw(retrieve);
use constant ARTIST => 'artists.dat';
use constant TRACKS => 'audio-player4.dat';
get '/' => sub ($c) {
my $query = $c->param('q') || '';
my @artists;
my %all_genres;
my $genre = {};
$genre = retrieve(ARTIST) if -e ARTIST;
my $audio = {};
$audio = retrieve(TRACKS) if -e TRACKS;
for my $artist (sort keys %$genre) {
for my $g ($genre->{$artist}->@*) {
push @artists, $artist if $g =~ /$query/;
$all_genres{$g}++;
}
}
@artists = uniq @artists;
my @tracks = _query($audio, \@artists);
$c->render(
template => 'index',
genre => ddc($genre),
genres => ddc(\%all_genres),
query => $query,
artists => ddc(\@artists),
n => scalar(@artists),
tracks => \@tracks,
m => scalar(@tracks),
);
} => 'index';
sub _query {
my ($audio, $artists) = @_;
my @match;
# Brute force through every track, looking for matches
for my $key (sort { $a <=> $b } keys %$audio) {
my $track = $audio->{$key}{track};
my @parts = split /\//, $track;
my $artist = $parts[-3]; # but not always
push @match, { $key => $track }
if grep { $artist eq $_ } @$artists;
}
return @match;
}
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Artist Genres';
<h1><%= title %></h1>
<form method="get">
<input type="text" name="q" placeholder="Search phrase" value="<%= $query %>"/>
<input type="submit" value="Query"/>
</form>
<p></p>
<%= $n %> matches:
<br>
<pre><%= $artists %></pre>
<p></p>
<%= $m %> tracks:
<ol>
% for my $track (@$tracks) {
% my $current = (keys %$track)[0];
% my $name = (values %$track)[0];
<li>
<a href="<%= url_for('index')->query(current => $current, noinc => 1, shuffle => 0, autoplay => 1, autoadvance => 0, darkmode => 1) %>"><%= $name %>
</li>
% }
</ol>
<!-- Genres:
<pre><%= $genres %></pre>
<hr>
<pre><%= $genre %></pre>
-->
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>