-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizeTVShows.php
executable file
·68 lines (62 loc) · 2.04 KB
/
organizeTVShows.php
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/php
<?php
// TODO: prevent the year of a show from being tacked on to the directory name
require_once("TVShow.class.php");
require_once("Logger.class.php");
runProcess();
function runProcess()
{
$file_dir = "/torrents/data/done";
$new_file_dir = "/mine/TVShows";
$logger = new Logger("/mine/logs/TVShows_".date("Y-m-d").".log");
$files = glob(quotemeta($file_dir)."/*");
if(count($files)) {
foreach($files as $file) {
list($show, $file_actual) = processFile($file);
if(is_object($show) && get_class($show) == "TVShow") {
$shows[$file_actual] = $show;
}
}
}
if(count($shows)) {
foreach($shows as $file=>$show) {
if($show->isValid()) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
$file_path = $new_file_dir."/".$show->getShowFolder()."/Season ".$show->getSeasonNumber();
if(!is_dir($file_path)) {
mkdir($file_path, 0777, true);
}
$file_name = $show->getShowString()." - ".$show->getEpisodeString().".".$ext;
$new_file = $file_path."/".$file_name;
$cmd = 'mv "'.$file.'" "'.$new_file.'"';
$logger->addToLog($cmd);
exec($cmd);
} else {
$logger->addToLog($show->getInvalidReason());
}
}
}
}
function processFile($file) {
if(!is_dir($file)) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if(!in_array($ext, array("avi", "mp4", "m4v", "!qb", "mkv"))) {
return array(NULL, NULL);
} else {
$showstring = pathinfo($file, PATHINFO_FILENAME);
$show = new TVShow($showstring);
return array($show, $file);
}
} else {
if(!stristr($file, "sample")) {
$files = glob(quotemeta($file)."/*");
foreach($files as $this_file) {
$ret_val = processFile($this_file);
if($ret_val[0] !== NULL) {
return $ret_val;
}
}
}
}
}
?>