]> ruin.nu Git - ndwebbie.git/blob - scripts/smscredits.pl
Use new race infrastructure for members stats
[ndwebbie.git] / scripts / smscredits.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use feature ':5.10';
6
7 no if $] >= 5.018, warnings => "experimental::smartmatch";
8
9 use local::lib;
10
11 use Encode;
12
13 use LWP::UserAgent;
14 use URI::Escape qw(uri_escape);
15 use HTTP::Request::Common;
16
17 use DBI;
18 use DBD::Pg qw(:pg_types);
19
20 use FindBin;
21 use lib "$FindBin::Bin/../lib";
22
23 use ND::DB;
24 my $dbh = ND::DB::DB();
25
26 my $ua = LWP::UserAgent->new(agent => 'NDWebbie' );
27
28 my $click = $dbh->selectrow_hashref(q{
29 SELECT api_id,username,password FROM clickatell LIMIT 1
30 });
31
32 my %tags = (
33         api_id => $click->{api_id},
34         user => $click->{username},
35         password => $click->{password},
36         callback => 3,
37 );
38
39 my $update = $dbh->prepare(q{
40 UPDATE clickatell SET credits = $1
41 });
42
43 my $res = $ua->request(
44         POST 'http://api.clickatell.com/http/getbalance',
45         Content_Type  => 'application/x-www-form-urlencoded',
46         Content       => [ %tags ]
47 );
48
49 given ($res->content){
50         when(/^Credit: (\d+\.\d\d)/){
51                 $update->execute($1);
52         }
53         default {
54                 die $_;
55         }
56 }