#!/usr/bin/perl # Andreas Wacker 091118 use strict; $| = 1; # this will loop and sync up new content to INTERDUBS # # If you run this once with the upload commented out, then only new content will be uploaded. # #config things to change for an install, # #make sure that interdubs_api.py is in the same directory than upload_file_into_idx.py my $python_cmd = "/root/sync2interdubs/upload_file_into_idx.py"; #everything from below this path will be copied into INTERDUBS #needs to exist, otherwise the script will freak out my $cfg_sourcefolder = "/var/www/html/WiretapCentral/export"; #this is where tiny flags are keep to keep a handle on what is new #it will be created if it does not exist my $cfg_flagdirectory = "/root/sync2interdubs/flags"; #INTERDUBS Key, get it from the INTERDUBS admin interface under Utiliies -> API my $cfg_idxkey = ""; #INTERDUBS base folder, it will be created if it does not exist, my $cfg_idxfolder = "/automaticupload"; #wait this long (in seconds) for files not to be touched any longer before an upload starts my $cfg_fileminage = 120; # # end of config section # #the actual program , no need to change anything below here chdir ($cfg_sourcefolder) or die "unable to find $cfg_sourcefolder, please check your configuration"; print "launching $0 syncing from $cfg_sourcefolder to $cfg_idxfolder PID: $$\n"; my $level; my $work ; while (1){ $work = 0; dorec (""); if ($work < 1){ sleep (10); } } sub dorec { my $curpath = shift @_ ; $level ++; opendir(DIR, "$cfg_sourcefolder/$curpath") || die "opendir $cfg_sourcefolder/$curpath: $!"; my @dots = readdir(DIR); closedir DIR; foreach my $e ( @dots ){ if (!($e =~ /^\./) && !($e =~ /^_idx_/)){ my $work = "$curpath/$e"; if (-d "$cfg_sourcefolder/$work"){ dorec ("$work"); } else{ my $filemt =( stat "$cfg_sourcefolder/$work")[9]; if ($filemt < time() - $cfg_fileminage){ if ( getflagage($work) < $filemt && $work =~ /\.mov$/){ setflag ($work); print "uploading $work in $cfg_sourcefolder\n"; my $opath = $work; $opath =~ s/\/[^\/]+$//; system ("$python_cmd $cfg_idxkey \"$cfg_sourcefolder/$work\" \"$cfg_idxfolder/$opath\""); $work ++; } } } } } $level --; } sub getflagage{ my $work = shift @_; my $flagname = "$cfg_flagdirectory/$work"; if (-f "$flagname"){ return ((stat "$flagname")[9]); } else{ return (0); } } sub setflag{ my $work = shift @_; my $flagname = "$cfg_flagdirectory/$work"; my $flagpath = $flagname; $flagpath =~ s/\/[^\/]+$//; if (! -d "$flagpath"){ system ("mkdir -p \"$flagpath\""); if (! -d "$flagpath"){ die "unable to create flag directory $flagpath\n"; } } open (OUT, ">$flagname") or die "unable to open flag file $flagname"; print OUT "$flagname"; close (OUT); }