This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoeboxExporter.php
87 lines (71 loc) · 1.88 KB
/
ShoeboxExporter.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
if(count($argv) < 3)
{
echo "Usage: php ShoeboxExporter.php <export file> <directory to save to>\n";
return;
}
else
{
$exportData = $argv[1];
$outputDirectory = $argv[2];
$exporter = new ShoeboxExporter();
$exporter->export($exportData, $outputDirectory);
}
class ShoeboxExporter
{
public function export($exportFile, $outputDirectory)
{
$directoryPrefix = "ShoeboxExport";
$exportData = file($exportFile);
$count = count($exportData);
for($i=0; $i<$count; $i++)
{
$lineItem = $exportData[$i];
$directory = $outputDirectory . '/' . $directoryPrefix . '/';
$directory .= $this->directoryFor($lineItem) . '/';
$fileName = $this->fileNameFor($lineItem);
$fileSize = $this->fileSizeFor($lineItem);
$url = $this->urlFor($lineItem);
$this->download($fileName, $fileSize, $directory, $url, $i, $count);
}
}
private function download($fileName, $fileSize, $directory, $url, $index, $total)
{
$fullPath = $directory . $fileName;
echo '(' . ($index+1) . ' of ' . $total . ') ' . $fullPath . ' ';
if(!file_exists($directory)) mkdir($directory, 0755, true);
if(file_exists($fullPath))
{
echo "Skipping\n";
}
else
{
$data = file_get_contents($url);
file_put_contents($fullPath, $data);
echo "Done\n";
}
}
private function directoryFor($lineItem)
{
$lineParts = explode(',', $lineItem);
$fileParts = explode('/', $lineParts[0]);
return $fileParts[0];
}
private function fileNameFor($lineItem)
{
$lineParts = explode(',', $lineItem);
$fileParts = explode('/', $lineParts[0]);
return $fileParts[1];
}
private function urlFor($lineItem)
{
$lineParts = explode(',', $lineItem);
return $lineParts[2];
}
private function fileSizeFor($lineItem)
{
$lineParts = explode(',', $lineItem);
return $lineParts[1];
}
}
?>