Last active
January 14, 2017 18:48
-
-
Save alexkadis/aea0cc8048b58baccb2b0b257fcc1282 to your computer and use it in GitHub Desktop.
Clean up downloaded movie torrents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use diagnostics; | |
use Cwd; | |
use File::Find; | |
use File::Copy; | |
use File::Basename; | |
use Cwd 'realpath'; | |
my $dir = &promptUser("Enter the movies directory ", "~/Movies"); | |
if ($dir !~ /^\./) { | |
$dir = glob($dir) | |
} | |
$dir = realpath($dir); | |
opendir (DIR, $dir) or die $!; | |
sub find_move_rename_movies { | |
my $file = $File::Find::name; | |
if ($file =~ /(mp4|mkv|avi|srt)$/ ) { | |
move $file, $dir; | |
my $basename = basename($file); | |
my $new_filename = $basename; | |
$new_filename =~ s/([a-z\d\_]+)[\.\_\-]([s]\d+[e]\d+)\.(.*)\.(mp4|mkv|avi|srt)/$1-$2.$4/gi; | |
$new_filename =~ s/\.(?=.*\.)/ /g; | |
$new_filename =~ s/^([a-z\_]+)\.(s\d+e\d+)\.(.*)\.(mp4|mkv|avi|srt)/$1--$2--$3$4/gi; | |
# if the file doesn't start with a '.' and has the right extension | |
if ($basename !~ /^\./ && $basename =~ /(mp4|mkv|avi|srt)$/ ) { | |
print $file . " > $new_filename" . "\n"; | |
rename $basename, $new_filename; | |
} | |
} | |
} | |
find({ wanted => \&find_move_rename_movies, no_chdir=>1}, $dir); | |
closedir (DIR); | |
exit 0; | |
#-------------------------------------------------------------------------# | |
# promptUser, a Perl subroutine to prompt a user for input. | |
# Copyright 2010 Alvin Alexander, http://www.devdaily.com | |
# This code is shared here under the | |
# Creative Commons Attribution-ShareAlike Unported 3.0 license. | |
# See http://creativecommons.org/licenses/by-sa/3.0/ for more information. | |
#-------------------------------------------------------------------------# | |
#------------------------------------------# | |
# "driver" program to test &promptUser() # | |
#------------------------------------------# | |
# $username = &promptUser("Enter the username "); | |
# $password = &promptUser("Enter the password "); | |
# $homeDir = &promptUser("Enter the home directory ", "/home/$username"); | |
# print "$username, $password, $homeDir\n"; | |
#----------------------------( promptUser )-----------------------------# | |
# # | |
# FUNCTION: promptUser # | |
# # | |
# PURPOSE: Prompt the user for some type of input, and return the # | |
# input back to the calling program. # | |
# # | |
# ARGS: $promptString - what you want to prompt the user with # | |
# $defaultValue - (optional) a default value for the prompt # | |
# # | |
#-------------------------------------------------------------------------# | |
sub promptUser { | |
#-------------------------------------------------------------------# | |
# two possible input arguments - $promptString, and $defaultValue # | |
# make the input arguments local variables. # | |
#-------------------------------------------------------------------# | |
our ($promptString,$defaultValue); | |
local($promptString,$defaultValue) = @_; | |
#-------------------------------------------------------------------# | |
# if there is a default value, use the first print statement; if # | |
# no default is provided, print the second string. # | |
#-------------------------------------------------------------------# | |
if ($defaultValue) { | |
print $promptString, "[", $defaultValue, "]: "; | |
} else { | |
print $promptString, ": "; | |
} | |
$| = 1; # force a flush after our print | |
$_ = <STDIN>; # get the input from STDIN (presumably the keyboard) | |
#------------------------------------------------------------------# | |
# remove the newline character from the end of the input the user # | |
# gave us. # | |
#------------------------------------------------------------------# | |
chomp; | |
#-----------------------------------------------------------------# | |
# if we had a $default value, and the user gave us input, then # | |
# return the input; if we had a default, and they gave us no # | |
# no input, return the $defaultValue. # | |
# # | |
# if we did not have a default value, then just return whatever # | |
# the user gave us. if they just hit the <enter> key, # | |
# then return default value. # | |
#-----------------------------------------------------------------# | |
if ("$defaultValue") { | |
return $_ ? $_ : $defaultValue; # return $_ if it has a value | |
} elsif ("$_" eq '') { | |
return $defaultValue; | |
} else { | |
return $_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment