-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.php
More file actions
129 lines (107 loc) · 3.21 KB
/
sync.php
File metadata and controls
129 lines (107 loc) · 3.21 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
require_once 'STFU.php';
class STFU_Check {
private $stfu;
private $folder;
private $root;
private $albums = array();
private $orphans = array();
public function __construct($folder) {
$this->stfu = new SimpleTerminalFlickrUtility('Folders/Albums Sync');
$this->folder = $folder;
$this->root = $this->stfu->root;
}
public function getAlbums() {
Color::text("List all albums...\t");
$sets = $this->stfu->api->photosets_getList();
Color::ok();
foreach($sets['photoset'] as $set) {
$album = new Album($set);
$this->albums[$album->name] = $album;
}
return $this->albums;
}
public function getOrphans() {
$list = $this->stfu->api->photos_getNotInSet();
$this->orphans = $list['photos']['photo'];
}
private static function photoExists($title, $set) {
$found = false;
if (!is_array($set)) {
Color::text("\nSomething weird happened... Set should be an array!\n", Color::red);
var_dump($set);
exit;
}
foreach ($set as $key => $photo) {
if ($photo['title'] == $title) {
$found = $key;
break;
}
}
return $found;
}
private function checkFolder($currentAlbum) {
if (array_key_exists($currentAlbum, $this->albums)) {
return true;
}
else {
return false;
}
}
public function check() {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->folder, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
$notFound = null;
foreach ($iterator as $info) {
$fullPath = $info->getRealpath();
$path = $info->getPath();
$currentAlbumName = SimpleTerminalFlickrUtility::folderToAlbum($path, $this->root);
$currentAlbumName = utf8_encode($currentAlbumName);
if (!$info->isDir()) {
// If album doesn't exist
if (!$this->checkFolder($currentAlbumName)) {
// If it's the first photo from a missing album, display a warning, else just skip it
if ($notFound != $currentAlbumName) Color::text("$currentAlbumName not found!\n", Color::red);
$notFound = $currentAlbumName;
continue;
}
else {
$notFound = null;
}
$album = $this->albums[$currentAlbumName];
$filename = substr($info->getFilename(), 0, -(strlen($info->getExtension())+1));
if (self::photoExists($filename, $album->getPhotos($this->stfu)) === false) {
Color::text("$filename not found!\t", Color::red);
// check if photo is orphaned
$orphaned = self::photoExists($filename, $this->orphans);
if ($orphaned !== false) {
echo "$filename exists on flickr, but not in an album, adding it to $currentAlbumName ...\t";
$this->stfu->api->photosets_addPhoto($album->id, $this->orphans[$orphaned]['id']);
Color::ok();
}
else {
// Not an orphan, only thing left to do is upload it
$photoID = $this->stfu->simpleUpload($fullPath);
echo "Add to album ...\t";
$this->stfu->api->photosets_addPhoto($album->id, $photoID);
Color::ok();
}
}
}
}
}
public function exec() {
$this->getAlbums();
$this->getOrphans();
$this->check();
}
}
if (count($argv) < 2) {
Color::text("Usage: php ".$argv[0]." <folder>\n", Color::red);
exit;
}
$stfu = new STFU_Check($argv[1]);
$stfu->exec();
Color::text("\n\nYATA!!\n", Color::blue);