-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinValueMon.pl
More file actions
executable file
·52 lines (43 loc) · 1.47 KB
/
coinValueMon.pl
File metadata and controls
executable file
·52 lines (43 loc) · 1.47 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use DBI;
MAIN: {
my $req = HTTP::Request->new(GET => 'http://www.coinchoose.com/api.php?base=BTC');
my $ua = new LWP::UserAgent();
$ua->agent("CoinValueMon/0.1 ");
my $res = $ua->request($req);
# Check the outcome of the response
if (!$res->is_success) {
print "response failed\n";
print $res->status_line, "\n";
exit 1;
}
my $jsonString = $res->content;
my $jsonObj = decode_json($jsonString);
my $dbh = DBI->connect('dbi:mysql:CryptoGods','root','') or die 'could not connect to db: ' . DBI->errstr;
my $coinSelectStmt = $dbh->prepare("select * from Coin where symbol = ?");
my $coinUpdateStmt = $dbh->prepare("update Coin set profitability = ? where symbol = ?");
my $coinInsertStmt = $dbh->prepare("insert into Coin set symbol = ?, name = ?, profitability = ?");
my $c = 0;
foreach my $coin (@$jsonObj) {
my $sym = lc($coin->{symbol});
$coinSelectStmt->execute($sym);
if ($coinSelectStmt->rows == 0) {
$coinInsertStmt->execute(
$coin->{symbol},
$coin->{name},
$coin->{adjustedratio}
) or print "could not insert coin($sym):" . $coinInsertStmt->errstr;
print 'inserted ', $coin->{symbol}, ' to ', $coin->{adjustedratio},"\n";
} else {
$coinUpdateStmt->execute(
$coin->{adjustedratio},
$coin->{symbol}
) or print "could not update coin($sym):" . $coinUpdateStmt->errstr;
print 'updated ', $coin->{symbol}, ' to ', $coin->{adjustedratio},"\n";
}
}
}