-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_downloader.pl
68 lines (46 loc) · 1.59 KB
/
motion_downloader.pl
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
#!/usr/bin/perl
# download image and video files from a remote server which is created by motion
# http://zetcode.com/db/sqliteperltutorial/err/
use warnings;
use strict;
use DBI;
use File::Fetch;
#
# required libfile-nfslock-perl - perl module to do NFS (or not) locking package
use Fcntl qw(LOCK_EX LOCK_NB);
use File::NFSLock;
# Try to get an exclusive lock on myself.
my $lock = File::NFSLock->new($0, LOCK_EX|LOCK_NB);
die "$0 is already running!\n" unless $lock;
#
#use this for remote host
#my $dsn = "dbi:mysql:dbname=motion;host=10.0.0.1";
#in localhost:
my $dsn = "dbi:mysql:dbname=motion";
my $user = "motion";
my $password = "secret";
my $dbh = DBI->connect($dsn, $user, $password)
or die "Can't connect to database: $DBI::errstr";
my $sth = $dbh->prepare(
q{ SELECT filename from security2 where downloaded='0' }
)
or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute()
or die "Can't execute statement: $DBI::errstr";
while (my($filename) = $sth->fetchrow()) {
# print "$filename\n";
# print "1\n";
my $f ="$filename";
my $fullpath = substr $f, 18;
# print "$fullpath\n";
my $url = "http://10.10.10.20/cam/${fullpath}";
# print "$url\n";
my $ff = File::Fetch->new(uri => $url);
my $file = $ff->fetch(to => '/home/www/sitedir/cam/') or die $ff->error;
my $statement = "update security2 set downloaded='1' where filename = ?";
my $rv = $dbh->do($statement, undef, $filename) or die "Can't do statement: $DBI::errstr";
}
# check for problems which may have terminated the fetch early
warn $DBI::errstr if $DBI::err;
$sth->finish();
$dbh->disconnect();