forked from michaelherger/MusicArtistInfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikipedia.pm
216 lines (165 loc) · 6.45 KB
/
Wikipedia.pm
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package Plugins::MusicArtistInfo::Wikipedia;
use strict;
use HTML::FormatText;
use Text::Levenshtein;
use URI::Escape qw(uri_escape_utf8);
use Slim::Utils::Cache;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(string cstring);
use Plugins::MusicArtistInfo::Common qw(CAN_IMAGEPROXY);
use constant MIN_REVIEW_SIZE => 50;
use constant PAGE_URL => 'https://%s.wikipedia.org/wiki/%s';
# https://www.mediawiki.org/wiki/API:Search
use constant SEARCH_URL => 'https://%s.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=%s&srprop=snippet|categorysnippet'; # params: language, query string
# https://www.mediawiki.org/wiki/API:Get_the_contents_of_a_page#Method_3:_Use_the_TextExtracts_API
use constant FETCH_URL => 'https://%s.wikipedia.org/w/api.php?action=query&prop=extracts&exsentences=10&formatversion=2&format=json&pageids=%s'; # params: language, page ID
my $log = logger('plugin.musicartistinfo');
my $prefs = preferences('plugin.musicartistinfo');
sub _rank {
my $item = shift;
my ($condition, $value, $message);
my $condition = shift if scalar @_ == 3;
my ($value, $message) = @_;
if ($condition) {
main::INFOLOG && $log->is_info && $log->info($message);
$item->{ranking} += $value;
}
return $condition;
}
sub getAlbumReview {
my ( $class, $client, $cb, $args ) = @_;
Plugins::MusicArtistInfo::Common->call(
sprintf(SEARCH_URL, $args->{lang} || _language($client), uri_escape_utf8('"' . $args->{album} . '" album "' . $args->{artist} . '"')),
sub {
my $searchResults = shift;
my $candidates = eval('$searchResults->{query}->{search}') || [];
$log->warn($@) if $@;
my ($candidate) = sort {
$b->{ranking} <=> $a->{ranking}
} grep {
$_->{ranking} > 5;
} map {
$_->{snippet} = _removeMarkup($_->{snippet});
$_->{categorysnippet} = _removeMarkup($_->{categorysnippet});
my $title = lc($_->{title});
$title =~ s/\s*\(.*album\)//ig;
$_->{ranking} = 0;
if (_rank($_, $title eq lc($args->{album}), 10, 'exact title match')) {}
elsif (_rank($_, ($title =~ /^\Q$args->{album}\E/i || $args->{album} =~ /^\Q$title\E/i), 7, 'partial title match')) {}
elsif (_rank($_, Text::Levenshtein::distance($title, lc($args->{album})) < 10, 5, 'levenshtein 10')) {}
if (_rank($_, lc($_->{snippet}) eq lc($args->{artist}), 5, 'artist match')) {}
elsif (_rank($_, $_->{snippet} =~ /^\Q$args->{artist}\E/i, 3, 'snippet starts with artist')) {}
elsif (_rank($_, $_->{snippet} =~ /\Q$args->{artist}\E/i, 2, 'snippet has artist')) {}
_rank($_, $_->{snippet} =~ /\Q$args->{album}\E/i && $_->{title} =~ /album/i, 1, 'snippet has album');
_rank($_, $title eq lc($args->{album}) && length($args->{album}) > 20, 5, 'matches a long album title');
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($_));
$_;
} @$candidates;
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($candidate ? $candidate : $candidates));
$candidate ||= {};
if (!$candidate->{pageid} && !$args->{lang} && _language($client) ne 'en' && $prefs->get('fallBackToEnglish')) {
$args->{lang} = 'en';
return $class->getAlbumReview($client, $cb, $args);
}
$class->getPage($client, sub {
my $review = shift;
$review->{review} = delete $review->{content};
$review->{reviewText} = delete $review->{contentText};
$cb->($review);
}, {
title => $candidate->{title},
id => $candidate->{pageid},
lang => $args->{lang},
});
},{
cache => 1,
expires => 86400, # force caching - wikipedia doesn't want to cache by default
}
);
}
sub getBiography {
my ( $class, $client, $cb, $args ) = @_;
Plugins::MusicArtistInfo::Common->call(
sprintf(SEARCH_URL, $args->{lang} || _language($client), uri_escape_utf8($args->{artist})),
sub {
my $searchResults = shift;
my $candidates = eval('$searchResults->{query}->{search}') || [];
$log->warn($@) if $@;
my ($candidate) = grep {
$_->{title} =~ /^\Q$args->{artist}\E/i
|| Text::Levenshtein::distance(lc($_->{title}), lc($args->{artist})) < 10;
} map {
$_->{snippet} = _removeMarkup($_->{snippet});
$_->{categorysnippet} = _removeMarkup($_->{categorysnippet});
$_;
} @$candidates;
main::INFOLOG && $log->is_info && $log->info(Data::Dump::dump($candidate ? $candidate : $candidates));
$candidate ||= {};
if (!$candidate->{pageid} && !$args->{lang} && _language($client) ne 'en' && $prefs->get('fallBackToEnglish')) {
$args->{lang} = 'en';
return $class->getAlbumReview($client, $cb, $args);
}
$class->getPage($client, sub {
my $bio = shift;
$bio->{bio} = delete $bio->{content};
$bio->{bioText} = delete $bio->{contentText};
$cb->($bio);
}, {
title => $candidate->{title},
id => $candidate->{pageid},
lang => $args->{lang},
});
},{
cache => 1,
expires => 86400, # force caching - wikipedia doesn't want to cache by default
}
);
}
sub _removeMarkup {
HTML::FormatText->format_string(
$_[0],
leftmargin => 0,
);
}
sub getPage {
my ( $class, $client, $cb, $args ) = @_;
if (!$args->{id}) {
return $cb->({
error => cstring($client, 'PLUGIN_MUSICARTISTINFO_NOT_FOUND')
});
}
Plugins::MusicArtistInfo::Common->call(
sprintf(FETCH_URL, $args->{lang} || _language($client), uri_escape_utf8($args->{id})),
sub {
my $fetchResults = shift;
my $result = {};
if ( $fetchResults && ref $fetchResults && $fetchResults->{query} && (my $content = $fetchResults->{query}->{pages}) ) {
if (length($content->[0]->{extract}) > MIN_REVIEW_SIZE) {
$result->{content} = $content->[0]->{extract};
$result->{content} =~ s/\n//g;
$result->{content} = '<link rel="stylesheet" type="text/css" href="/plugins/MusicArtistInfo/html/wikipedia.css" />' . $result->{content};
$result->{contentText} = _removeMarkup($result->{content});
my $slug = $args->{title};
$slug =~ s/ /_/g;
$result->{content} .= sprintf('<p><br><a href="%s" target="_blank">%s</a></p>',
sprintf(PAGE_URL, $args->{lang} || _language($client), uri_escape_utf8($slug)),
cstring($client, 'PLUGIN_MUSICARTISTINFO_READ_MORE')
);
}
}
if ( !$result->{content} && !main::SCANNER ) {
$result->{error} ||= cstring($client, 'PLUGIN_MUSICARTISTINFO_NOT_FOUND');
}
$cb->($result);
},{
cache => 1,
expires => 86400, # force caching - wikipedia doesn't want to cache by default
}
);
}
sub _language {
my $client = shift;
return cstring($client, 'PLUGIN_MUSICARTISTINFO_WIKIPEDIA_LANGUAGE');
}
1;