-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcryptobot
executable file
·201 lines (147 loc) · 4.41 KB
/
cryptobot
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
#!/usr/bin/env perl
use common::sense;
use AnyEvent;
use AnyEvent::HTTP;
use AnyEvent::IRC::Client;
use JSON::XS;
my $cmd = shift || usage("please provide command");
my $api_key = $ENV{CMC_API_KEY} || usage("need CMC_API_KEY env var");
if ($cmd eq 'test') {
my $sender = shift;
my $msg = shift;
my $cv = AE::cv;
process($sender, $msg, undef, sub {
my $response = shift;
for my $msg (@$response) {
print "OUTPUT: $msg\n";
}
$cv->send;
});
$cv->wait;
} elsif ($cmd eq 'serve') {
my $server = shift // usage("need irc server");
my $chan = shift // usage("need irc channel");
my $nick = shift // usage("need nick");
irc_client($server, $chan, $nick);
} else {
usage("unknown command: $cmd");
}
sub usage {
my $reason = shift;
print <<'END';
Usage: cryptbot <cmd> [args]
cryptobot serve <irc server> <channel> <nick>
cryptobot test <nick> <message>
Examples:
cryptobot serve irc.choopa.net '#mychan' cbot123
cryptobot test jimmy '!q btc'
END
print "\n** $reason **\n" if $reason;
exit 1;
}
sub irc_client {
my ($server, $chan_list, $nick) = @_;
my $cv = AE::cv;
my $con = new AnyEvent::IRC::Client;
$con->reg_cb(publicmsg => sub {
my ($con, $channel, $ircmsg) = @_;
my $msg = $ircmsg->{params}->[1];
my $sender = $ircmsg->{prefix};
$sender =~ s/!.*$//;
process($sender, $msg, $con, sub {
my $response = shift;
return if !defined $response || @$response == 0;
for my $msg (@$response) {
print " -> $msg\n";
$con->send_chan($channel, PRIVMSG => ($channel, $msg));
}
});
});
$con->connect($server, 6667, { nick => $nick } );
foreach my $chan (split /\s*,\s*/, $chan_list) {
$con->send_srv(JOIN => $chan);
}
$cv->recv;
$con->disconnect;
}
sub process {
my ($sender, $msg, $con, $cb) = @_;
return $cb->() unless $msg =~ m{^!};
print "$sender: $msg\n";
if ($msg =~ m{^!q (.+)}) {
my $sym_list = lc($1);
my $syms = { map { $_ => 1, } split(/\s*,\s*/, $sym_list) };
get_coinmarketcap(sub {
my ($prices) = @_;
return $cb->() unless $prices;
my $output = [];
foreach my $quote (@$prices) {
if ($syms->{lc($quote->{symbol})}) {
push @$output, render_quote($quote);
}
}
push @$output, "no symbol found" if !@$output;
$cb->($output);
});
} elsif ($msg =~ m{^!movers (\S+)}) {
my $period = lc($1);
if ($period ne '1h' && $period ne '24h' && $period ne '7d') {
return $cb->(['invalid period (choose: 1h, 24h, 7d)']);
}
my $field = "percent_change_$period";
get_coinmarketcap(sub {
my ($prices) = @_;
$prices = [ sort { $b->{$field} <=> $a->{$field} } @$prices ];
my $output = [];
push @$output, render_quote($prices->[0]);
push @$output, render_quote($prices->[1]);
push @$output, render_quote($prices->[2]);
push @$output, render_quote($prices->[3]);
push @$output, '---';
push @$output, render_quote($prices->[-1]);
push @$output, render_quote($prices->[-2]);
push @$output, render_quote($prices->[-3]);
push @$output, render_quote($prices->[-4]);
$cb->($output);
});
} elsif ($msg =~ m{^!join (#[\w_.]+)}) {
$con->send_srv(JOIN => $1);
}
return;
}
sub render_quote {
my $info = shift;
my $quote = $info->{quote}->{USD};
my $pc1h = normalize_percent_change($quote->{percent_change_1h});
my $pc24h = normalize_percent_change($quote->{percent_change_24h});
my $pc7d = normalize_percent_change($quote->{percent_change_7d});
return "\x02$info->{name}\x0F | 1 $info->{symbol} = $quote->{price} USD | 1h: $pc1h 24h: $pc24h 7d: $pc7d";
}
my $coinmarketcap_cache;
my $coinmarketcap_last_time = 0;
sub get_coinmarketcap {
my ($cb) = @_;
if (time() - $coinmarketcap_last_time < 60) {
return $cb->($coinmarketcap_cache);
}
http_get "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=${api_key}", sub {
my ($data, $headers) = @_;
return $cb->() unless $headers->{Status} =~ /^2/;
my $prices = decode_json($data);
$prices = $prices->{'data'};
$coinmarketcap_cache = $prices;
$coinmarketcap_last_time = time();
$cb->($prices);
};
return;
}
sub normalize_percent_change {
my $pc = shift;
$pc = "+$pc" unless $pc =~ /^-/;
if ($pc =~ /^\+/) {
$pc = "\x0309$pc\x0F";
} else {
$pc = "\x0304$pc\x0F";
}
return $pc . '%';
}