blib/lib/File/PatternMatch.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 81 CC 3 SCAR 18.4
CC
3
Cov
0%
patternmatch
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 6.3
1 #!/usr/bin/perl
2 package File::PatternMatch;
3  
4 use vars qw($VERSION);
5 $VERSION = '0.056';
6  
7 require Exporter;
8 @ISA = qw(Exporter);
9 our @EXPORT = qw(patternmatch);
10  
11 use strict;
12 use Term::ExtendedColor qw(fg bg);
13  
14 # Yes, this is extremely fugly.
15 our %patterns = (
16 'S[0-9]{1}[2-9]{1}E[0-9]{1}[2-9]' => {
17 256 => fg('magenta23', 'New Episode'),
18 dzen => "^fg(#ff0000)New Episode^fg()",
19 none => "New Episode",
20 weight => 20,
21 },
22 'S01E01' => {
23 256 => fg('red1', 'New Show'),
24 dzen => "^fg(#ffff00)New Show^fg()",
25 none => "New Show",
26 weight => 100,
27 },
28 'S0\dE01' => {
29 256 => fg('bold', 'Season Premiere'),
30 dzen => "^fg(#cccd05)Season Premiere^fg()",
31 none => "Season Premiere",
32 weight => 75,
33 },
34 'do(c|k?)u(ment.+)?|
35 (discovery|history)\.(channel)?|
36 national\.geographic|
37 colossal\.' => {
38 256 => fg('green16', 'Documentary'),
39 dzen => "^fg(#87d700)Documentary^fg()",
40 none => "Documentary",
41 weight => 80,
42 },
43 'EPL|WWE|UFC|UEFA|Rugby|La\Liga|
44 Superleague|Allsvenskan|
45 Formula\.Ford' => {
46 256 => fg(145, 'Sport'),
47 dzen => "^fg(#afaf87)Sport^fg()",
48 none => "Sport",
49 weight => 5,
50 },
51 '(?i)swedish|-se-' => {
52 256 => fg('cyan8', 'Swedish'),
53 dzen => "^fg(#87ffd7)Swedish^fg(#121212)",
54 none => "Swedish",
55 weight => 30,
56 },
57 '(?i)jay\.leno' => {
58 256 => fg('purple15', 'Talk Show'),
59 dzen => "^fg(87afff)Talk Show^fg()",
60 none => "Talk Show",
61 },
62 'PsyCZ|MYCEL|UPE|HiEM|PSi|gEm' => {
63 256 => "\e[38;5;192mPsychedelic\e[0m",
64 dzen => "^fg(#d7ff87)Psychedelic^fg()",
65 none => "Psychedelic",
66 weight => 70,
67 },
68 '.+-(H3X|wAx|CMS|BFHMP3|WHOA|RNS|
69 C4|CR|UMT|0MNi)(.+)?|FRAY(.+)?$' => {
70 256 => "\e[38;5;094mHip-Hop\e[0m",
71 dzen => "^fg(#309184)Hip-Hop^fg()",
72 none => "Hip-Hop",
73 weight => 85,
74 },
75 'LzY|qF|SRP|NiF' => {
76 256 => "\e[38;5;126mRock\e[0m",
77 dzen => "^fg(#af0087)Rock^fg()",
78 none => "Rock",
79 weight => 50,
80 },
81 '-sour$' => {
82 256 => "\e[38;5;166mDnB\e[0m",
83 dzen => "^fg(#d75f00)DnB^fg()",
84 none => "DnB",
85 weight => 50,
86 },
87 'VA(-|_-_).+' => {
88 256 => "\e[38;5;049mV/A\e[0m",
89 dzen => "^fg(#00ffaf)V/A^fg()",
90 none => "V/A",
91 weight => 20,
92 },
93 '\(?_?-?CDS-?_?\)?' => {
94 256 => "\e[38;5;244mSingle\e[0m",
95 dzen => "^fg(#808080)Single^fg()",
96 none => "Single",
97 weight => 50,
98 },
99 '\(?_?-?CDM-?_?\)?' => {
100 256 => "\e[38;5;233mMaxi\e[0m",
101 dzen => "^fg(#1c1c1c)Maxi^fg()",
102 none => "Maxi",
103 weight => 50,
104 },
105 '\(?_?-?CDA-?_?\)?' => {
106 256 => "\e[38;5;222mAlbum\e[0m",
107 dzen => "^fg(#ffd787)Album^fg()",
108 none => "Album",
109 weight => 55,
110 },
111 '\(?_?-?DAB-?_?\)?' => {
112 256 => "\e[38;5;211mDAB\e[0m",
113 dzen => "^fg(#ff87af)DAB^fg()",
114 none => "DAB",
115 weight => 50,
116 },
117 '\(?_?-?CABLE-?_?\)?' => {
118 256 => "\e[38;5;191mCable\e[0m",
119 dzen => "^fg(#d7ff5f)Cable\e[0m",
120 none => "Cable",
121 weight => 5,
122 },
123 '\(?_?-?VLS|Vinyl-?_?\)?' => {
124 256 => "\e[38;5;201mVinyl\e[0m",
125 dzen => "^fg(#ff00ff)Vinyl^fg()",
126 none => "Vinyl",
127 weight => 70,
128 },
129 'Live_(on|at|in)' => {
130 256 => "\e[38;5;181mLive\e[0m",
131 dzen => "^fg(#d7afaf)Live^fg()",
132 none => "Live",
133 weight => 45,
134 },
135 '-Recycled.+$' => {
136 256 => "\e[38;5;215Re-release\e[0m",
137 dzen => "^fg(#ffaf5f)Re-release^fg()",
138 none => "Re-release",
139 weight => 55,
140 },
141 'TALiON|HB|DV8' => {
142 256 => "\e[38;5;41m\e[1mHardstyle\e[0m",
143 dzen => "^fg(#f95504)Hardstyle^fg()",
144 none => "Hardstyle",
145 weight => 40,
146 },
147 );
148  
149 my(undef,undef,undef,undef,undef,$year) = localtime(time);
150 $year += 1900;
151  
152 our %wanted = (
153 'Fringe' => {
154 dzen => "^fg(#000000)^bg(#121212)",
155 256 => "\e[48;5;052m\e[1m\e[38;5;196m",
156 none => "",
157 },
158 'House' => {
159 dzen => "^fg(#d5f418)^fg(#ffff00)",
160 256 => fg('bold', fg('red1')),
161 none => "",
162 },
163 '(?:do(c|k)ument(a|รค)ry?|History\.Channel)' => {
164 dzen => "^fg(#09b33f)",
165 256 => "\e[38;5;197m",
166 none => "",
167 },
168  
169 'pilot' => {
170 dzen => "^fg(#c02d07)",
171 256 => "\e[38;5;85m\e[1m",
172 none => "",
173 },
174 'S01E01' => {
175 dzen => "^fg(#d7d75f)",
176 256 => "\e[38;5;185m",
177 none => "",
178 },
179 'hdtv' => {
180 dzen => "^fg(#cccdda)",
181 256 => "\e[38;5;32m\e[3m",
182 none => "",
183 },
184 'pdtv' => {
185 dzen => "^fg(#dacddd)",
186 256 => "\e[38;5;29m\e[3m",
187 none => "",
188 },
189 'swedish' => {
190 dzen => "^fg(#ffff00)",
191 256 => "\e[38;5;220m\e[1m",
192 none => "",
193 },
194 'DIMENSION' => {
195 dzen => "^fg(#faeec4)",
196 256 => "\e[38;5;240m\e[1m",
197 none => "",
198 },
199 'C4' => {
200 dzen => "^fg(#facddc)",
201 256 => "\e[38;5;130m\e[1m",
202 none => "",
203 },
204 'LOL' => {
205 dzen => "^fg(#facddc)",
206 256 => "\e[38;5;118m\e[1m",
207 none => "",
208 },
209 '720p' => {
210 dzen => "^fg(#ffcccd)",
211 256 => "\e[38;5;178m\e[1m",
212 none => "",
213 },
214 'Promo_CD' => {
215 dzen => "^fg(#dddc26)",
216 256 => "\e[38;5;173m",
217 none => "",
218 },
219 $year => {
220 dzen => "^fg(#93c26b)",
221 256 => "\e[1m",
222 none => "",
223 },
224 );
225  
226 our %end = (
227 dzen => "", # ^bg()^fg()
228 256 => "\e[0m",
229 none => "",
230 );
231  
232  
233 sub patternmatch {
234 my $fmt = shift // 'plain';
235 chomp(my @files = @_);
236  
237 my %results;
238 my $i = 0;
239 for my $file(@files) {
240 if( ($fmt == 256) or ($fmt eq 'extended') or ($fmt eq 'plain')) {
241 $file = sprintf("%70.70s", $file);
242 # $file = sprintf("%$ENV{COLUMNS}.$ENV{COLUMNS}s", $file);
243 }
244  
245 for my $keyword(keys(%wanted)) {
246 $file =~ s/($keyword)/$wanted{$keyword}->{$fmt}$1$end{$fmt}/gi;
247 }
248  
249 for my $pattern(sort{$patterns{$a}{weight} <=> $patterns{$b}{weight}} keys(%patterns)) {
250 if($file =~ /$pattern/x) {
251 $results{$i}{$file} = $patterns{$pattern}{$fmt};
252 next;
253 }
254 }
255 $i++;
256 }
257 return(\%results);
258 }
259  
260 1;
261  
262 =head1 NAME
263  
264 File::PatternMatch - parse media information from filenames
265  
266 =head1 SYNOPSIS
267  
268 use File::PatternMatch;
269  
270 my @files = glob("$ENV{HOME}/music/*");
271  
272 my $plain = patternmatch(@files);
273  
274 my $extended_colors = patternmatch(256, @files);
275  
276  
277 =head1 DESCRIPTION
278  
279 B<File::PatternMatch> takes a list of filenames and tries to parse relevant
280 information from them. If a filename contains the string 'S01E01' we can safely
281 assume it's a new TV show, the first episode from the first season, and thus we
282 label it 'New Show'.
283  
284 There are filters for various music genres, tv shows and music videos.
285  
286 The labels can be formatted in three ways (output formats):
287  
288 =head2 plaintext
289  
290 Raw, plain text.
291  
292 =head2 colored
293  
294 Colored using extended escape sequences (see L<Term::ExtendedColor>).
295  
296 =head2 dzen2
297  
298 Formatted using the L<dzen2(1)> notation.
299  
300 =head1 EXPORTS
301  
302 =head2 patternmatch()
303  
304 Parameters: $output_format, @files
305  
306 Returns: \%results
307  
308 B<patternmatch()> takes a list of filenames and tries to match them aginst
309 specific patterns. The result might look like:
310  
311 # Plain text
312 2 => {
313 'Prison.Break.S01E01-FOOBAR' => 'New Show',
314 },
315 42 => {
316 'Laleh-Prinsessor-FOOBAZ' => 'Pop/Rock',
317 },
318  
319 # Using extended color escape sequences
320 2 => {
321 'Prison.Break.S01E01-FOOBAR' => "\e[38;5;160mNew Show\e[0m",
322 },
323  
324 # Using dzen2 format
325 2 => {
326 'Prison.Break.S01E01-FOOBAR' => '^fg(#ff0000)New Show^fg()',
327 },
328  
329 =head1 SEE ALSO
330  
331 L<https://github.com/trapd00r/rel>
332  
333 =head1 AUTHOR
334  
335 Magnus Woldrich
336 CPAN ID: WOLDRICH
337 m@japh.se
338 http://japh.se
339  
340 =head1 REPORTING BUGS
341  
342 Report bugs and/or feature requests:
343  
344 L<https://rt.cpan.org>
345  
346 L<https://github.com/trapd00r/Foo-Bar/issues>
347  
348 L<m@japh.se>
349  
350 =head1 COPYRIGHT
351  
352 Copyright 2011, 2018- Magnus Woldrich <m@japh.se>. This program is free
353 software; you may redistribute it and/or modify it under the same terms as
354 Perl itself.
355  
356 =cut