-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwuxiapedia.pm
204 lines (163 loc) · 5.33 KB
/
wuxiapedia.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
# PLUGIN STUFF GOES HERE---------------------------------------------------------
use Data::Dumper;
sub new {
my $type = shift;
my $self = { };
# Required variables when object is created
$self->{BOOK_ID} = shift;
my $incode = shift;
my $outcode = shift;
$self->{VERBOSE} = shift;
if ($incode) {
$self->{FROM_CODE} = $incode;
}else{
$self->{FROM_CODE} = "utf-8";
};
if ($outcode) {
$self->{TO_CODE} = $outcode
}else{
$self->{TO_CODE} = "utf-8";
};
# Plugin Variables
$self->{PLUGIN_NAME} = "wuxiapedia";
# The name of the site that this plugin is for
$self->{PLUGIN_DESC} = "wuxiapedia.com plugin";
# The url to use for the index page (The one that contains all links, or the starting page). %s is replaced with the bookId.
$self->{INDEX_URL} = "http://wuxiapedia.com/Novels/".$self->{BOOK_ID};
$self->{RX_INTERMEDIATE_URL} = "/Novels/".$self->{BOOK_ID}."/[A-z0-9-]+\$";
# The url pattern to match for each chapter. Use groups to parse out additional data from the url.
$self->{RX_CHAPTER_URL} = "/Novels/".$self->{BOOK_ID}."/.*/Chapter-(\\d+)";
#$self->{RX_CHAPTER_URL_INDEX} = "/novel/".$self->{BOOK_ID}."\.html";
$self->{RX_CHAPTER_TITLE} = ">Chapter \\d+ : (.*?)<";
$self->{RX_BOOK_TITLE} = "Wuxiapedia \\/ Novels \\/ (\\S+ \\/ \\S+) \\/";
$self->{RX_BOOK_ID} = "(\\S+\\/\\S+)";
$self->{USE_INDEX_TITLE_AS_BOOK_TITLE} = 1;
$self->{BOOK_TITLE} = "";
$self->{BOOK_DATA} = {};
return bless $self, $type;
}
sub isMatchingUrl {
my ($self, $url) = @_;
my $rxChapterUrl = $self->{RX_CHAPTER_URL};
if ($url =~ m|$rxChapterUrl|) {
return 1;
}
my $rxIntermediateUrl = $self->{RX_INTERMEDIATE_URL};
if ($url =~ m|$rxIntermediateUrl|) {
return 1;
}
return 0;
}
sub hasValidBookId {
my $self = shift;
my $rxBookId = $self->{RX_BOOK_ID};
if ($self->{BOOK_ID} =~ m|$rxBookId|) {
return 1;
} else {
return 0;
}
return 1;
}
sub parseIndex {
my ($self, $mech, $origUrl) = @_;
# if (($self->{USE_INDEX_TITLE_AS_BOOK_TITLE})) {
# my $title = $mech->title();
# my $rxBookTitle = $self->{RX_BOOK_TITLE};
# if ($title =~ m|$rxBookTitle|) {
# if ($1) {
# $title = $1;
# }
# print STDERR "Found book title: $title\n" if ($self->{VERBOSE});
# $self->{BOOK_TITLE} = $title;
# }
# }
#$self->parseDocument($mech, $origUrl);
return 1;
}
sub parseCharset {
my $self = shift;
my $buffer = shift;
my $encoding = $self->{FROM_CODE};
if ($buffer =~ m/.*; charset=(.*?)[;\$]/i) {
$encoding = $1;
}
return $encoding;
}
sub parseDocument {
my ($self, $mech, $origUrl) = @_;
my $chapterNum;
my $chapterName;
my $content = $mech->content();
#my $title = $mech->title();
my $url = $mech->uri();
#print Dumper($mech);
my $rxIntermediateUrl = $self->{RX_INTERMEDIATE_URL};
if ($url =~ m|$rxIntermediateUrl|) {
print STDERR "Not parsing intermediate url: $url\n" if ($self->{VERBOSE});
return 1;
}
my $rxChapterUrl = $self->{RX_CHAPTER_URL};
my $rxChapterUrlIndex = $self->{RX_CHAPTER_URL_INDEX};
if ($url =~ m|$rxChapterUrl|) {
print STDERR "Found chapter number: $1\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'number'} = $1;
# } elsif ($url =~ m|$rxChapterUrlIndex|) {
# print STDERR "Found chapter number: 1\n" if ($self->{VERBOSE});
# $self->{BOOK_DATA}{$url}{'number'} = 1;
} else {
print STDERR "Error: Could not parse chapter number from $url using $rxChapterUrl" if ($self->{VERBOSE});
}
my $rxChapterTitle = $self->{RX_CHAPTER_TITLE};
if (($self->{BOOK_DATA}{$url}{'linkText'}) && ($self->{BOOK_DATA}{$url}{'linkText'} =~ m|$rxChapterTitle|)) {
print STDERR "Found chapter number in linkText: $1\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'number'} = $1;
}
# Tried this when I was having utf-8 conversion issues, got roughly same effect, so switched back.
# use HTML::TokeParser;
#
# my $tree = HTML::TokeParser->new(\$content);
#
# while (my $tag = $tree->get_tag("div")) {
# if (($tag->[1]{class}) && ($tag->[1]{class} eq "attribute-long")) {
# print "Found attribute-long DIV\n";
# while (my $contents = $tree->get_text("div") ) {
# $text = $text."$contents\n";
# }
# }
# }
# my $f = HTML::FormatText::WithLinks->new();
# $text = $f->parse($text);
my $tree = HTML::TreeBuilder::XPath->new();
$tree->parse_content($content);
my $text;
my $f = HTML::FormatText::WithLinks->new();
foreach my $node ($tree->findnodes_as_string("//div[\@class='attribute-long']/p")) {
#Strip out H2 tags (they contain chapters, which we already have)
#$text =~ s|<h2>.*?</h2>||g;
$text = $text.$f->parse($node);
#$text = $text.$node;
}
my $encoding = $self->parseCharset($mech->response()->header('Content-Type'));
$self->{FROM_CODE} = $encoding;
$text = $self->convert_encoding($text);
$self->{BOOK_DATA}{$url}{'content'} = $text;
my $chtitle;
if ($content =~ m|$rxChapterTitle|m) {
if ($1) {
$chtitle = $1;
}
print STDERR "Found chapter title: $chtitle\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'title'} = $chtitle;
}
# return {
# 'content' => $content,
# 'chapterName' => $chapterName,
# 'chapterNum' => $chapterNum,
# }
return 1;
}
sub print_chapter {
my ($self, $bookData) = @_;
return $bookData->{'title'} . " (" . $bookData->{'number'} . ")\n\n" . $bookData->{'content'} . "\n";
}
# PLUGIN STUFF ENDS HERE---------------------------------------------------------