blib/lib/File/PatternMatch.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 78 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.045';
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 '\(?_?-?WEB-?_?\)?' => {
130 256 => "\e[38;5;19mWEB\e[0m",
131 dzen => "^fg(#ffabcd)WEB^fg()",
132 none => "WEB",
133 weight => 4,
134 },
135 'Live_(on|at|in)' => {
136 256 => "\e[38;5;181mLive\e[0m",
137 dzen => "^fg(#d7afaf)Live^fg()",
138 none => "Live",
139 weight => 45,
140 },
141 '-Recycled.+$' => {
142 256 => "\e[38;5;215Re-release\e[0m",
143 dzen => "^fg(#ffaf5f)Re-release^fg()",
144 none => "Re-release",
145 weight => 55,
146 },
147 'TALiON|HB|DV8' => {
148 256 => "\e[38;5;41m\e[1mHardstyle\e[0m",
149 dzen => "^fg(#f95504)Hardstyle^fg()",
150 none => "Hardstyle",
151 weight => 40,
152 },
153 );
154  
155 my(undef,undef,undef,undef,undef,$year) = localtime(time);
156 $year += 1900;
157  
158 our %wanted = (
159 'Fringe' => {
160 dzen => "^fg(#000000)^bg(#121212)",
161 256 => "\e[48;5;052m\e[1m\e[38;5;196m",
162 none => "",
163 },
164 'House' => {
165 dzen => "^fg(#d5f418)^fg(#ffff00)",
166 256 => fg('bold', fg('red1')),
167 none => "",
168 },
169 '(?:do(c|k)ument(a|รค)ry?|History\.Channel)' => {
170 dzen => "^fg(#09b33f)",
171 256 => "\e[38;5;197m",
172 none => "",
173 },
174  
175 'pilot' => {
176 dzen => "^fg(#c02d07)",
177 256 => "\e[38;5;85m\e[1m",
178 none => "",
179 },
180 'S01E01' => {
181 dzen => "^fg(#d7d75f)",
182 256 => "\e[38;5;185m",
183 none => "",
184 },
185 'hdtv' => {
186 dzen => "^fg(#cccdda)",
187 256 => "\e[38;5;32m\e[3m",
188 none => "",
189 },
190 'pdtv' => {
191 dzen => "^fg(#dacddd)",
192 256 => "\e[38;5;29m\e[3m",
193 none => "",
194 },
195 'swedish' => {
196 dzen => "^fg(#ffff00)",
197 256 => "\e[38;5;220m\e[1m",
198 none => "",
199 },
200 'DIMENSION' => {
201 dzen => "^fg(#faeec4)",
202 256 => "\e[38;5;240m\e[1m",
203 none => "",
204 },
205 'C4' => {
206 dzen => "^fg(#facddc)",
207 256 => "\e[38;5;130m\e[1m",
208 none => "",
209 },
210 'LOL' => {
211 dzen => "^fg(#facddc)",
212 256 => "\e[38;5;118m\e[1m",
213 none => "",
214 },
215 '720p' => {
216 dzen => "^fg(#ffcccd)",
217 256 => "\e[38;5;178m\e[1m",
218 none => "",
219 },
220 'Promo_CD' => {
221 dzen => "^fg(#dddc26)",
222 256 => "\e[38;5;173m",
223 none => "",
224 },
225 $year => {
226 dzen => "^fg(#93c26b)",
227 256 => "\e[1m",
228 none => "",
229 },
230 );
231  
232 our %end = (
233 dzen => "", # ^bg()^fg()
234 256 => "\e[0m",
235 none => "",
236 );
237  
238  
239 sub patternmatch {
240 my $fmt = shift // 'plain';
241 chomp(my @files = @_);
242  
243 my %results;
244 my $i = 0;
245 for my $file(@files) {
246 if( ($fmt == 256) or ($fmt eq 'extended') or ($fmt eq 'plain')) {
247 $file = sprintf("%70.70s", $file);
248 }
249  
250 for my $keyword(keys(%wanted)) {
251 $file =~ s/($keyword)/$wanted{$keyword}->{$fmt}$1$end{$fmt}/gi;
252 }
253  
254 for my $pattern(sort{$patterns{$a}{weight} <=> $patterns{$b}{weight}} keys(%patterns)) {
255 if($file =~ /$pattern/x) {
256 $results{$i}{$file} = $patterns{$pattern}{$fmt};
257 next;
258 }
259 }
260 $i++;
261 }
262 return(\%results);
263 }
264  
265 1;
266  
267 =head1 NAME
268  
269 File::PatternMatch - parse media information from filenames
270  
271 =head1 SYNOPSIS
272  
273 use File::PatternMatch;
274  
275 my @files = glob("$ENV{HOME}/music/*");
276  
277 my $plain = patternmatch(@files);
278  
279 my $extended_colors = patternmatch(256, @files);
280  
281  
282 =head1 DESCRIPTION
283  
284 B<File::PatternMatch> takes a list of filenames and tries to parse relevant
285 information from them. If a filename contains the string 'S01E01' we can safely
286 assume it's a new TV show, the first episode from the first season, and thus we
287 label it 'New Show'.
288  
289 There are filters for various music genres, tv shows and music videos.
290  
291 The labels can be formatted in three ways (output formats):
292  
293 =head2 plaintext
294  
295 Raw, plain text.
296  
297 =head2 colored
298  
299 Colored using extended escape sequences (see L<Term::ExtendedColor>).
300  
301 =head2 dzen2
302  
303 Formatted using the L<dzen2(1)> notation.
304  
305 =head1 EXPORTS
306  
307 =head2 patternmatch()
308  
309 Parameters: $output_format, @files
310  
311 Returns: \%results
312  
313 B<patternmatch()> takes a list of filenames and tries to match them aginst
314 specific patterns. The result might look like:
315  
316 # Plain text
317 2 => {
318 'Prison.Break.S01E01-FOOBAR' => 'New Show',
319 },
320 42 => {
321 'Laleh-Prinsessor-FOOBAZ' => 'Pop/Rock',
322 },
323  
324 # Using extended color escape sequences
325 2 => {
326 'Prison.Break.S01E01-FOOBAR' => "\e[38;5;160mNew Show\e[0m",
327 },
328  
329 # Using dzen2 format
330 2 => {
331 'Prison.Break.S01E01-FOOBAR' => '^fg(#ff0000)New Show^fg()',
332 },
333  
334 =head1 SEE ALSO
335  
336 L<https://github.com/trapd00r/rel>
337  
338 =head1 AUTHOR
339  
340 Magnus Woldrich
341 CPAN ID: WOLDRICH
342 m@japh.se
343 http://japh.se
344  
345 =head1 REPORTING BUGS
346  
347 Report bugs and/or feature requests:
348  
349 L<https://rt.cpan.org>
350  
351 L<https://github.com/trapd00r/Foo-Bar/issues>
352  
353 L<m@japh.se>
354  
355 =head1 COPYRIGHT
356  
357 Copyright 2011, 2018- Magnus Woldrich <m@japh.se>. This program is free
358 software; you may redistribute it and/or modify it under the same terms as
359 Perl itself.
360  
361 =cut