line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Lingr::Archives; |
2
|
4
|
|
|
4
|
|
265806
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
122
|
|
3
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
91
|
|
4
|
4
|
|
|
4
|
|
41
|
use 5.10.0; |
|
4
|
|
|
|
|
23
|
|
|
4
|
|
|
|
|
164
|
|
5
|
4
|
|
|
4
|
|
18
|
use Carp; |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
223
|
|
6
|
4
|
|
|
4
|
|
34097
|
use LWP::UserAgent; |
|
4
|
|
|
|
|
154597
|
|
|
4
|
|
|
|
|
129
|
|
7
|
4
|
|
|
4
|
|
44
|
use URI; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
195
|
|
8
|
4
|
|
|
4
|
|
1248
|
use JSON qw(decode_json); |
|
4
|
|
|
|
|
13666
|
|
|
4
|
|
|
|
|
36
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
7
|
|
|
7
|
1
|
12936
|
my ($class, %args) = @_; |
14
|
7
|
50
|
|
|
|
35
|
if(!defined($args{user})) { |
15
|
0
|
|
|
|
|
0
|
croak "user parameter is mandatory"; |
16
|
|
|
|
|
|
|
} |
17
|
7
|
50
|
|
|
|
25
|
if(!defined($args{password})) { |
18
|
0
|
|
|
|
|
0
|
croak "password parameter is mandatory"; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
my $self = bless { |
21
|
7
|
|
33
|
|
|
125
|
user_agent => $args{user_agent} // do { |
|
|
|
100
|
|
|
|
|
22
|
0
|
|
|
|
|
0
|
my $ua = LWP::UserAgent->new; |
23
|
0
|
|
|
|
|
0
|
$ua->env_proxy(); |
24
|
0
|
|
|
|
|
0
|
$ua; |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
user => $args{user}, |
27
|
|
|
|
|
|
|
password => $args{password}, |
28
|
|
|
|
|
|
|
app_key => $args{app_key}, |
29
|
|
|
|
|
|
|
api_base => $args{api_base} // 'http://lingr.com/api', |
30
|
|
|
|
|
|
|
session_id => undef, |
31
|
|
|
|
|
|
|
}, $class; |
32
|
7
|
|
|
|
|
49
|
$self->{api_base} =~ s{/+$}{}; |
33
|
7
|
|
|
|
|
33
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get_archives { |
37
|
8
|
|
|
8
|
1
|
1235
|
my ($self, $room, $options) = @_; |
38
|
8
|
|
100
|
|
|
30
|
$options //= {}; |
39
|
8
|
50
|
|
|
|
25
|
if(!defined($room)) { |
40
|
0
|
|
|
|
|
0
|
croak "room parameter is mandatory"; |
41
|
|
|
|
|
|
|
} |
42
|
8
|
|
|
|
|
15
|
my $retry_allowed = 1; |
43
|
8
|
100
|
|
|
|
26
|
if(!defined($self->{session_id})) { |
44
|
7
|
|
|
|
|
21
|
$self->_create_session(); |
45
|
6
|
|
|
|
|
12
|
$retry_allowed = 0; |
46
|
|
|
|
|
|
|
} |
47
|
7
|
100
|
100
|
|
|
78
|
my $result = $self->_get_request('/room/get_archives', [ |
48
|
|
|
|
|
|
|
session => $self->{session_id}, room => $room, |
49
|
|
|
|
|
|
|
before => $options->{before} // 99999999, |
50
|
|
|
|
|
|
|
(defined($options->{limit}) ? (limit => $options->{limit}) : ()) |
51
|
|
|
|
|
|
|
]); |
52
|
7
|
100
|
66
|
|
|
606
|
if(!defined($result->{status}) || lc($result->{status}) ne "ok") { |
53
|
1
|
50
|
|
|
|
5
|
if($retry_allowed) { |
54
|
1
|
|
|
|
|
2
|
$self->{session_id} = undef; |
55
|
1
|
|
|
|
|
6
|
return $self->get_archives($room, $options); |
56
|
|
|
|
|
|
|
} |
57
|
0
|
|
|
|
|
0
|
croak "Lingr API Error: code = $result->{code}, detail = $result->{detail}"; |
58
|
|
|
|
|
|
|
} |
59
|
6
|
|
|
|
|
9
|
return @{$result->{messages}}; |
|
6
|
|
|
|
|
50
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _create_session { |
63
|
7
|
|
|
7
|
|
12
|
my ($self) = @_; |
64
|
7
|
100
|
|
|
|
51
|
my $result = $self->_get_request('/session/create', [user => $self->{user}, |
65
|
|
|
|
|
|
|
password => $self->{password}, |
66
|
|
|
|
|
|
|
(defined($self->{app_key}) ? (app_key => $self->{app_key}) : ())]); |
67
|
7
|
100
|
66
|
|
|
218
|
if(!defined($result->{status}) || lc($result->{status}) ne "ok") { |
68
|
1
|
|
|
|
|
36
|
croak "Cannot create session with user $self->{user}: $result->{code}, $result->{detail}"; |
69
|
|
|
|
|
|
|
} |
70
|
6
|
|
|
|
|
30
|
$self->{session_id} = $result->{session}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _get_request { |
74
|
14
|
|
|
14
|
|
31
|
my ($self, $endpoint, $params) = @_; |
75
|
14
|
|
|
|
|
87
|
my $url = URI->new($self->{api_base} . $endpoint); |
76
|
14
|
|
|
|
|
21382
|
$url->query_form($params); |
77
|
14
|
|
|
|
|
1737
|
my $res = $self->{user_agent}->get($url); |
78
|
14
|
50
|
|
|
|
8948
|
if(!$res->is_success) { |
79
|
0
|
|
|
|
|
0
|
croak "Network Error: " . $res->status_line; |
80
|
|
|
|
|
|
|
} |
81
|
14
|
|
|
|
|
179
|
return decode_json($res->content); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |