-
Notifications
You must be signed in to change notification settings - Fork 1
/
LetsHelperd.pl
60 lines (51 loc) · 1.24 KB
/
LetsHelperd.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
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
use File::Basename;
use File::Copy;
use DBI;
use Switch;
# Variables
our $Sleeptime="10";
our $Watchdir="/tmp/LetsHelper/XMLIN";
our $Donedir="/tmp/LetsHelper/XMLDONE";
our $DBconnection = DBI->connect("dbi:Pg:dbname=letshelper") or die "Cannot establish database connection";
while() {
# Searching for new files in watchdir
my @Workfiles=<$Watchdir/*.xml>;
my $File;
foreach $File (@Workfiles) {
# Reading all xml files
print "Reading $File\n";
my $xml = new XML::Simple;
my $data = $xml->XMLin("$File");
print Dumper($data);
switch ($data->{type}) {
case "project" {
print "Project\n";
finish_xml($File);
}
case "episode" {
print "Episode\n";
}
else {
print "Not a valid xml type";
}
}
}
print "Sleeping $Sleeptime Seconds\n";
sleep($Sleeptime);
}
## Functions
sub finish_xml {
# Moving processed xml to donedir
my $File=$_[0];
my $Filename=basename($File);
print "Done parsing $Filename, moving to done folder\n";
my $Donetime = time;
my $Donefile="$Donedir/$Donetime\_$Filename";
move($File,$Donefile) or die "The move operation failed: $!";
# Sleep 1 second to avoid moving another file over the current processed
sleep(1);
}