/
xmltvfixuplint
executable file
·198 lines (179 loc) · 7.69 KB
/
xmltvfixuplint
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/perl
use warnings;
use strict;
my $filename = $ARGV[0];
open (my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
my $errors;
my $warnings;
my $line_num = 0;
while (my $fixup_line = <$fh>) {
$line_num++;
chomp $fixup_line;
check_fixup_line($fixup_line, $line_num);
}
sub t {
my ($message) = @_;
print $message . "\n";
}
sub check_fixup_line {
my ($line, $line_num) = @_;
if (defined $line) {
# Blank lines are allowed
return if ($line =~ m/^$/);
# Comments are allowed if they are at the start of the line
return if ($line =~ m/^#/);
# Each valid entry consists of 2 primary fields separated by "|":
# - fixup type (integer between 1-15)
# - separator (bar: |)
# - fixup text (fields separated by "~")
#
my @fields = split /\|/, $line;
if (scalar @fields != 2) {
t("E:" . $filename . ":" . $line_num . ":Need both fixup type and fixup text");
return;
}
my ($fixup_type, $fixup_text) = @fields;
if (! defined $fixup_type || $fixup_type eq '' || $fixup_type !~ m/\d+/) {
t("E:" . $filename . ":" . $line_num . ":Invalid fixup type");
return;
}
if (! defined $fixup_text || $fixup_text eq '' || $fixup_text !~ m/\w+/) {
t("E:" . $filename . ":" . $line_num . ":Invalid fixup text");
return;
}
# fixup types are documented in XMLTV::Augment
if ($fixup_type eq '1') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 1) {
t("E:" . $filename . ":" . $line_num . ":[1] Invalid number of fixup fields (have " . scalar @fields . ", need 1)");
return;
}
}
elsif ($fixup_type eq '2') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 1) {
t("E:" . $filename . ":" . $line_num . ":[2] Invalid number of fixup fields (have " . scalar @fields . ", need 1)");
return;
}
}
elsif ($fixup_type eq '3') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 1) {
t("E:" . $filename . ":" . $line_num . ":[3] Invalid number of fixup fields (have " . scalar @fields . ", need 1)");
return;
}
}
elsif ($fixup_type eq '4') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 1) {
t("E:" . $filename . ":" . $line_num . ":[4] Invalid number of fixup fields (have " . scalar @fields . ", need 1)");
return;
}
}
elsif ($fixup_type eq '5') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 2) {
t("E:" . $filename . ":" . $line_num . ":[5] Invalid number of fixup fields (have " . scalar @fields . ", need 2)");
return;
}
}
elsif ($fixup_type eq '6') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields < 2) {
t("E:" . $filename . ":" . $line_num . ":[6] Invalid number of fixup fields (have " . scalar @fields . ", need at least 2)");
return;
}
}
elsif ($fixup_type eq '7') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 3) {
t("E:" . $filename . ":" . $line_num . ":[7] Invalid number of fixup fields (have " . scalar @fields . ", need 3)");
return;
}
}
elsif ($fixup_type eq '8') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 4) {
t("E:" . $filename . ":" . $line_num . ":[8] Invalid number of fixup fields (have " . scalar @fields . ", need 4)");
return;
}
foreach my $field (@fields) {
$field = "" if ! defined $field;
}
my( $old_title, $old_ep, $new_title, $new_ep ) = @fields;
if ($old_title eq '' or $new_title eq '') {
t("W:" . $filename . ":" . $line_num . ":[8] Ignoring fixup '" . $fixup_text . "' as old/new title not given");
}
if ($old_title eq $new_title) {
t("W:" . $filename . ":" . $line_num . ":[8] Old/new title are the same - change to type 7 title fixup: '" . $fixup_text . "'");
}
if ($old_ep =~ m/^\Q$new_title\E(?::|\s-)\s\Q$new_ep\E$/) {
t("W:" . $filename . ":" . $line_num . ":[8] Old ep contains new title/ep - change to type 11 title fixup: '" . $fixup_text . "'");
}
}
elsif ($fixup_type eq '9') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 3) {
t("E:" . $filename . ":" . $line_num . ":[9] Invalid number of fixup fields (have " . scalar @fields . ", need 3)");
return;
}
}
elsif ($fixup_type eq '10') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 5) {
t("E:" . $filename . ":" . $line_num . ":[10] Invalid number of fixup fields (have " . scalar @fields . ", need 5)");
return;
}
foreach my $field (@fields) {
$field = "" if ! defined $field;
}
my( $old_title, $old_ep, $new_title, $new_ep, $desc ) = @fields;
if ($old_title eq '' or $new_title eq '' or $desc eq '') {
t("W:" . $filename . ":" . $line_num . ":[10] Ignoring fixup '" . $fixup_text . "' as titles/desc not given");
}
}
elsif ($fixup_type eq '11') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 2) {
t("E:" . $filename . ":" . $line_num . ":[11] Invalid number of fixup fields (have " . scalar @fields . ", need 2)");
return;
}
my( $brand, $new_title ) = @fields;
if ($brand eq '' or $new_title eq '') {
t("W:" . $filename . ":" . $line_num . ":[11] Ignoring fixup '" . $fixup_text . "' as brand/title not given");
}
}
elsif ($fixup_type eq '12') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields < 2) {
t("E:" . $filename . ":" . $line_num . ":[12] Invalid number of fixup fields (have " . scalar @fields . ", need at least 2)");
return;
}
}
elsif ($fixup_type eq '13') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields != 2) {
t("E:" . $filename . ":" . $line_num . ":[13] Invalid number of fixup fields (have " . scalar @fields . ", need 2)");
return;
}
}
elsif ($fixup_type eq '14') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields < 2) {
t("E:" . $filename . ":" . $line_num . ":[14] Invalid number of fixup fields (have " . scalar @fields . ", need at least 2)");
return;
}
}
elsif ($fixup_type eq '15') {
my @fields = split( /~/, $fixup_text, -1);
if (scalar @fields < 2) {
t("E:" . $filename . ":" . $line_num . ":[15] Invalid number of fixup fields (have " . scalar @fields . ", need at least 2)");
return;
}
}
else {
t("E:" . $filename . ":" . $line_num . ":Unknown fixup type (please refer to XMLTV::Augment)");
return;
}
}
}