Eksi.pm

filter: Statementstmt 59.1% 87 / 147 Branchbran 22.2% 8 / 36 Conditioncond 19.1% 9 / 47 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 55.5% 15 / 27 Podpod 100% 5 / 5 totaltotal 43.0% 124 / 288 CC 41 SCAR 30.6
CC
41
Cov
45%
_download
56.0
_lengthen
42.0
_parse_entry
29.1
CRAP 21.3
↑ Summary Eksisozluk.pm »
1 package WWW::Eksi;
2 2 $WWW::Eksi::VERSION = '0.33';
3 =head1 NAME
4  
5 WWW::Eksi - Interface for Eksisozluk.com
6  
7 =head1 DESCRIPTION
8  
9 An interface for Eksisozluk, a Turkish social network.
10 Provides easy access to entries and lists of entries.
11  
12 =head1 SYNOPSIS
13  
14 use WWW::Eksi;
15 my $e = WWW::Eksi->new;
16  
17 # Last week's most popular entries
18 my @ghebe_fast = $e->ghebe; # might get rate limited
19 my @ghebe_slow = $e->ghebe(5); # add a politeness delay
20  
21 # Yesterday's most popular entries
22 my @debe_fast = $e->debe; # might get rate limited
23 my @debe_slow = $e->debe(5); # add a politeness delay
24  
25 # Alternative list of yesterday's popular entries
26 my @doludolu_fast = $e->doludolu; # might get rate limited
27 my @doludolu_slow = $e->doludolu(5); # add a politeness delay
28  
29 # Single entry
30 my $entry = $e->download_entry(1);
31  
32 =cut
33  
34 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use WWW::Lengthen;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 =head1 METHODS
49  
50 =head2 new
51  
52 Returns a new WWW::Eksi object.
53  
54 =cut
55  
56 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
57 1 my $class = shift;
58 1 my $today = DateTime->now->ymd;
59  
60 1 my $eksi = {
61 base => 'https://eksisozluk.com',
62 entry => 'https://eksisozluk.com/entry/',
63 debe => 'https://eksisozluk.com/debe',
64 ghebe => 'https://eksisozluk.com/istatistik/gecen-haftanin-en-begenilen-entryleri',
65 strp_dt => DateTime::Format::Strptime->new( pattern => '%d.%m.%Y%H:%M'),
66 strp_d => DateTime::Format::Strptime->new( pattern => '%d.%m.%Y'),
67 doludolu => 'https://eksisozluk.com/basliklar/ara?SearchForm.When.From='.$today.'T00:00:00&SearchForm.When.To='.$today.'T23:59:59&SearchForm.SortOrder=Count',
68 };
69  
70 1 return bless $eksi, $class;
71 }
72  
73 =head2 download_entry($id)
74  
75 Takes entry id as argument, returns its data (if available) as follows.
76  
77 {
78 entry_url => Str
79 topic_url => Str
80 topic_title => Str
81 topic_channels => [Str]
82  
83 author_name => Str
84 author_url => Str
85 author_id => Int
86  
87 body_raw => Str
88 body_text => Str (html tags removed)
89 body_processed => Str (html tags processed)
90 fav_count => Int
91 create_time => DateTime
92 update_time => DateTime
93 }
94  
95 =cut
96  
97 1 ▶sub download_entry {
Subroutine download_entry: called CC=4 SCAR=15.6
Pod documented
98 1 my ($self,$id) = @_;
99 1 ▶ my $data = $self->_download($self->{entry}.$id) if ($id && $id=~/^\d{1,}$/);
Branch: 1 branch
BranchTrueFalseTotal
if $id and $id =~ /^\d{1,}$/ 1 0 1
Condition33%1/3
$id and $id =~ /^\d{1,}$/
!ll&&!rl&&r
001
MC/DC0%0/2
$id and $id =~ /^\d{1,}$/
$id $id =~ /^\d{1,}$/
Truth table33%1/3
A and B
A= $idB= $id =~ /^\d{1,}$/
ABresult
0X0
100
111
100 1 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 1 1
101 1 return $self->_parse_entry($data,$id);
102 }
103  
104 1 ▶sub _parse_entry {
Subroutine _parse_entry: called CC=17 SCAR=33.7
105 1 my ($self,$data, $id) = @_;
106 1 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 1 1
107  
108 1 my $e = {};
109 1 my $dom = Mojo::DOM->new($data);
110  
111 1 ▶ unless ($id){
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 1 1
112 0 $id = $dom->at('a[class~=entry-date]')->{href};
113 0 $id =~ s/[^\d]//g;
114 0 ▶ return unless ($id && $id=~/^\d{1,}$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $id and $id =~ /^\d{1,}$/ 0 0 0
Condition0%0/3
$id and $id =~ /^\d{1,}$/
!ll&&!rl&&r
000
MC/DC0%0/2
$id and $id =~ /^\d{1,}$/
$id $id =~ /^\d{1,}$/
Truth table0%0/3
A and B
A= $idB= $id =~ /^\d{1,}$/
ABresult
0X0
100
111
115 }
116  
117 # entry_url
118 1 $e->{entry_url} = $self->{entry}.$id;
119  
120 # body_raw, body_text, body_processed
121 1 $e->{body_raw} = $dom->at('div[class=content]')->content;
122 1 $e->{body_text} = $dom->at('div[class=content]')->text;
123 1 $e->{body_processed} = $self->_process_entry($e->{body_raw});
124  
125  
126 # time_as_seen, create_time, update_time
127 1 my $time_as_seen = $dom->at('a[class~=entry-date]')->text;
128 1 $e->{time_as_seen} = $time_as_seen;
129  
130 1 $time_as_seen =~/
131 ^
132 \s*
133 (?<date_posted>\d\d\.\d\d\.\d{4})
134 \s*
135 (?<time_posted>\d\d:\d\d)? #old entries lack time
136 ( # update block
137 \s*
138 ~
139 \s*
140 (?<date_updated>\d\d\.\d\d\.\d{4})?
141 # date won't be shown if updated on the same day
142 \s*
143 (?<time_updated>\d\d:\d\d)?
144 )? # will not exist if not updated
145 \s*
146 $
147 /x;
148  
149 1 ▶ my $date_posted = $+{date_posted} // '';
Condition50%1/2
$+{"date_posted"} // ""
l!l
10
MC/DC0%0/1
$+{"date_posted"} // ""
$+{"date_posted"}
Truth table50%1/2
A
A= $+{"date_posted"}
Aresult
11
00
150 1 ▶ my $time_posted = $+{time_posted} // '';
Condition50%1/2
$+{"time_posted"} // ""
l!l
01
MC/DC0%0/1
$+{"time_posted"} // ""
$+{"time_posted"}
Truth table50%1/2
A
A= $+{"time_posted"}
Aresult
11
00
151 1 ▶ my $date_updated = $+{date_updated} // '';
Condition50%1/2
$+{"date_updated"} // ""
l!l
01
MC/DC0%0/1
$+{"date_updated"} // ""
$+{"date_updated"}
Truth table50%1/2
A
A= $+{"date_updated"}
Aresult
11
00
152 1 ▶ my $time_updated = $+{time_updated} // '';
Condition50%1/2
$+{"time_updated"} // ""
l!l
01
MC/DC0%0/1
$+{"time_updated"} // ""
$+{"time_updated"}
Truth table50%1/2
A
A= $+{"time_updated"}
Aresult
11
00
153  
154 1 ▶ Carp::croak "Entry date could not be found" unless $date_posted;
Branch: 1 branch
BranchTrueFalseTotal
unless $date_posted 0 1 1
155  
156 $e->{create_time} = $time_posted
157 ? $self->{strp_dt}->parse_datetime($date_posted.$time_posted)
158 1 ▶ : $self->{strp_d}->parse_datetime($date_posted);
Branch: 1 branch
BranchTrueFalseTotal
$time_posted ? : 0 1 1
159 $e->{update_time} = $time_updated
160 ? $self->{strp_dt}->parse_datetime(
161 1 ▶ ($date_updated || $date_posted).$time_updated)
Branch: 1 branch
BranchTrueFalseTotal
$time_updated ? : 0 1 1
Condition0%0/3
$date_updated || $date_posted
l!l&&r!l&&!r
000
MC/DC0%0/2
$date_updated || $date_posted
$date_updated $date_posted
Truth table0%0/3
A || B
A= $date_updatedB= $date_posted
ABresult
1X1
011
000
162 : '';
163  
164  
165 # author_name, author_url, author_id, fav_count
166 1 my $li_data_id_entry = $dom->at("li[data-id=$id]");
167 1 my $a_entry_author = $dom->at('a[class=entry-author]');
168 1 ▶ $e->{author_name} = $li_data_id_entry->{"data-author"}
Condition33%1/3
$li_data_id_entry->{"data-author"} // $a_entry_author->text
l!l&&r!l&&!r
100
MC/DC0%0/2
$li_data_id_entry->{"data-author"} // $a_entry_author->text
$li_data_id_entry->{"data-author"} $a_entry_author->text
Truth table33%1/3
A // B
A= $li_data_id_entry->{"data-author"}B= $a_entry_author->text
ABresult
1X1
011
000
169 // $a_entry_author->text;
170 1 $e->{author_url} = $self->{base}.$a_entry_author->{href};
171 1 ▶ $e->{author_id} = $li_data_id_entry->{"data-author-id"} // 0;
Condition50%1/2
$li_data_id_entry->{"data-author-id"} // 0
l!l
10
MC/DC0%0/1
$li_data_id_entry->{"data-author-id"} // 0
$li_data_id_entry->{"data-author-id"}
Truth table50%1/2
A
A= $li_data_id_entry->{"data-author-id"}
Aresult
11
00
172 1 ▶ $e->{fav_count} = $li_data_id_entry->{"data-favorite-count"} // 0;
Condition50%1/2
$li_data_id_entry->{"data-favorite-count"} // 0
l!l
10
MC/DC0%0/1
$li_data_id_entry->{"data-favorite-count"} // 0
$li_data_id_entry->{"data-favorite-count"}
Truth table50%1/2
A
A= $li_data_id_entry->{"data-favorite-count"}
Aresult
11
00
173  
174  
175 # topic_channels
176 1 ▶ my $channels_text = $dom->at('section[id=hidden-channels]')->text // 0;
Condition50%1/2
$dom->at("section[id=hidden-channels]")->text // 0
l!l
10
MC/DC0%0/1
$dom->at("section[id=hidden-channels]")->text // 0
$dom->at("section[id=hidden-channels]")->text
Truth table50%1/2
A
A= $dom->at("section[id=hidden-channels]")->text
Aresult
11
00
177 1 $channels_text =~s/^\s*//;
178 1 $channels_text =~s/\s*$//;
179 1 my @channels = split ',',$channels_text;
180 1 $e->{topic_channels} = \@channels;
181  
182  
183 # topic_title, topic_url
184 1 my $h1_id_title = $dom->at('h1[id=title]');
185 1 $e->{topic_title} = $h1_id_title->{'data-title'};
186 1 $e->{topic_url} = $self->{base}.$h1_id_title->at('a')->{href};
187  
188 1 return $e;
189 }
190  
191 =head2 ghebe($politeness_delay)
192  
193 Returns an array of entries for top posts of last week.
194 Ordered from more popular to less popular.
195  
196 =cut
197  
198 0 ▶sub ghebe {
Subroutine ghebe: not called CC=4 SCAR=30.0
Pod documented
199 0 my ($self, $sleep_seconds) = @_;
200 0 ▶ $sleep_seconds //= 0;
Condition0%0/2
$sleep_seconds //= 0
l!l
00
MC/DC0%0/1
$sleep_seconds //= 0
$sleep_seconds
Truth table0%0/2
A
A= $sleep_seconds
Aresult
11
00
201 0 my $data = $self->_download($self->{ghebe});
202 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
203  
204 0 my $dom = Mojo::DOM->new($data);
205 0 my $links = $dom->at('ol[class~=stats]')->find('a');
206 0 ▶ my $ids = $links->map(sub{$_->{href}=~m/%23(\d+)$/})->to_array;
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 my @entries = ();
208  
209 0 foreach my $id (@$ids){
210 0 my $entry = $self->download_entry($id);
211 0 push @entries, $entry;
212 0 sleep $sleep_seconds
213 }
214  
215 0 return @entries;
216 }
217  
218 =head2 debe($politeness_delay)
219  
220 Returns an array of entries for top posts of yesterday.
221 Ordered from more popular to less popular.
222  
223 =cut
224  
225 0 ▶sub debe {
Subroutine debe: not called CC=4 SCAR=30.0
Pod documented
226 0 my ($self, $sleep_seconds) = @_;
227 0 ▶ $sleep_seconds //= 0;
Condition0%0/2
$sleep_seconds //= 0
l!l
00
MC/DC0%0/1
$sleep_seconds //= 0
$sleep_seconds
Truth table0%0/2
A
A= $sleep_seconds
Aresult
11
00
228 0 my $data = $self->_download($self->{debe});
229 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
230  
231 0 my $dom = Mojo::DOM->new($data);
232 0 my $links = $dom->find('ul[class~=partial]')->[1]->find('a');
233 0 ▶ my $ids = $links->map(sub{$_->{href}=~m/\/(\d+)$/})->to_array;
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 my @entries = ();
235  
236 0 foreach my $id (@$ids){
237 0 my $entry = $self->download_entry($id);
238 0 push @entries, $entry;
239 0 sleep $sleep_seconds
240 }
241  
242 0 return @entries;
243 }
244  
245 =head2 doludolu($politeness_delay)
246  
247 Returns an array of entries with alternative top posts of yesterday.
248 Ordered from more popular to less popular.
249  
250 =cut
251  
252 0 ▶sub doludolu {
Subroutine doludolu: not called CC=4 SCAR=30.0
Pod documented
253 0 my $self = shift;
254 0 ▶ my $sleep_sec = shift // 0;
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
255 0 my $data = $self->_download($self->{doludolu});
256 0 my @doludolu = ();
257 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
258  
259 0 my $dom = Mojo::DOM->new($data);
260 my $links = $dom
261 ->at('ul[class=topic-list]')
262 ->find('a')
263 0 ▶ ->map(sub{$_->{href}=~m/^(.*)\?/})
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 ->to_array;
265  
266 0 foreach my $link (@$links){
267 0 my $entry_html = $self->_download($self->{base}.$link.'?a=dailynice');
268 0 my $entry_hash = $self->_parse_entry($entry_html);
269 0 push @doludolu, $entry_hash;
270 0 sleep $sleep_sec;
271 }
272  
273 0 return @doludolu;
274 }
275  
276 0 ▶sub _download {
Subroutine _download: not called CC=7 SCAR=40.3
277 0 my ($self,$url) = @_;
278  
279 0 ▶ my $u = URI->new($url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
280 0 ▶ return 0 unless ($url && $u && (any {$u->scheme eq $_} qw/http https/));
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $url and $u and &any(sub { $u->scheme eq $_; } , "http", "https") 0 0 0
Condition0%0/6
$url and $u and &any(sub { $u->scheme eq $_; } , "http", "https")
!ll&&!rl&&r
000
$url and $u
!ll&&!rl&&r
000
MC/DC0%0/3
$url and $u and &any(sub { $u->scheme eq $_; } , "http", "https")
$url $u &any(sub { $u->scheme eq $_; } , "http", "https")
Truth table0%0/4
A and B and C
A= $urlB= $uC= &any(sub { $u->scheme eq $_; } , "http", "https")
ABCresult
0XX0
10X0
1100
1111
281  
282 0 my $response = Furl->new->get($u);
283  
284 0 ▶ return ($response && $response->is_success)
Branch: 1 branch
BranchTrueFalseTotal
$response && $response->is_success ? : 0 0 0
Condition0%0/3
$response and $response->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->is_success
$response $response->is_success
Truth table0%0/3
A and B
A= $responseB= $response->is_success
ABresult
0X0
100
111
285 ? $response->content
286 : 0;
287 }
288  
289 0 ▶sub _lengthen {
Subroutine _lengthen: not called CC=6 SCAR=37.4
290 0 my ($self, $url) = @_;
291  
292 0 ▶ my $u = URI->new($url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
293 0 ▶ return 0 unless ($url && $u && (any {$u->scheme eq $_} qw/http https/));
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $url and $u and &any(sub { $u->scheme eq $_; } , "http", "https") 0 0 0
Condition0%0/6
$url and $u and &any(sub { $u->scheme eq $_; } , "http", "https")
!ll&&!rl&&r
000
$url and $u
!ll&&!rl&&r
000
MC/DC0%0/3
$url and $u and &any(sub { $u->scheme eq $_; } , "http", "https")
$url $u &any(sub { $u->scheme eq $_; } , "http", "https")
Truth table0%0/4
A and B and C
A= $urlB= $uC= &any(sub { $u->scheme eq $_; } , "http", "https")
ABCresult
0XX0
10X0
1100
1111
294  
295 0 my $lengthener = WWW::Lenghten->new;
296  
297 0 ▶ return (any {$u->host eq $_} qw/is.gd goo.gl/)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { $u->host eq $_; } , 'is.gd', 'goo.gl') ? : 0 0 0
298 ? $lengthener->try($u)
299 : $u;
300 }
301  
302 1 ▶sub _process_entry {
Subroutine _process_entry: called CC=2 SCAR=7.0
303 1 my ($self,$e) = @_;
304 1 ▶ return unless $e;
Branch: 1 branch
BranchTrueFalseTotal
unless $e 0 1 1
305  
306 # Expand goo.gl and is.gd links
307 0 $e=~s/href="(https?:\/\/(goo\.gl|is\.gd)[^"]*)"/"href=\""._lengthen($1)."\""/ieg;
308  
309 # Make hidden references (akıllı bkz) visible
310 1 $e=~s/(<sup class="ab"><a data-query=")([^"]*)("[^<>]*>)\*/$1$2$3* ($2)/g;
311  
312 # Make local links global
313 1 $e=~s/href="\//target="_blank" href="https:\/\/eksisozluk.com\//g;
314  
315 # Force no decoration to disable underline in Gmail
316 1 $e=~s/href="/style="text-decoration:none;" href="/g;
317  
318 # Add JPG to imgur images with no extension
319 1 $e=~s/(href="https?:\/\/[^.]*\.?imgur.com\/\w{7})"/$1\.jpg"/g;
320  
321 # Make JPG/PNG images visible
322 1 $e=~s/(href="([^"]*\.(jpe?g|png)(:large)?)"[^<]*<\/a>)/$1<br><br><img src="$2"><br><br>/g;
323  
324 # Add NW arrow to external links
325 1 $e=~s/(https?:\/\/(?!eksisozluk.com)([^\/<]*\.[^\/<]*)[^<]*<\/a>)/$1 \($2 &#8599;\)/g;
326  
327 1 return $e;
328  
329 }
330  
331 0 ▶sub _entry_not_found {
Subroutine _entry_not_found: not called CC=1 SCAR=6.9
332  
333 return {
334 0 topic_title => '?',
335 topic_url => '?',
336 topic_channels => [],
337 author_name => '?',
338 author_id => 0,
339 body_raw => "<i>bu entry silinmi&#351;.</i>",
340 body_text => "bu entry silinmi&#351;.",
341 body_processed => "<i>bu entry silinmi&#351;.</i>",
342 fav_count => '?',
343 create_time => 0,
344 update_time => 0,
345 };
346 }
347  
348 2 1;
349  
350 __END__
↑ Summary Eksisozluk.pm »