-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTFU.php
170 lines (136 loc) · 4.26 KB
/
STFU.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
require_once('phpflickr/phpFlickr.php');
class SimpleTerminalFlickrUtility {
public $root = null;
public $autoSyncAlbumName = "Auto Sync";
public $userID = null;
public $api = null;
public $count = 0;
public $totalCount = 0;
public $bytesToUpload = 0;
public $bytesUploaded = 0;
private $startTime;
public function __construct($moduleName=null) {
Color::text("STFU $moduleName\n\n", Color::blue);
echo "Setting up flickr connection...\t";
$cfg = parse_ini_file('config.ini', true);
$this->root = $cfg['stfu']['root'];
$this->userID = $cfg['api']['userid'];
$this->autoSyncAlbumName = $cfg['stfu']['autosync'];
$this->api = new phpFlickr($cfg['api']['key'], $cfg['api']['secret']);
$this->api->setToken($cfg['api']['token']);
Color::ok();
$this->startTime = microtime(true);
}
public function simpleUpload($file) {
$start = microtime(true);
echo "Uploading $file ...\t";
$photoId = $this->api->sync_upload($file);
$end = microtime(true);
$fileSize = filesize($file);
$speed = round($fileSize/1024 / ($end - $start));
Color::ok(false);
echo "($speed Kb/s)\t";
return $photoId;
}
public function asyncUpload($file) {
$start = microtime(true);
echo "Uploading $file ...\t";
$photoId = $this->api->async_upload($file);
$end = microtime(true);
$fileSize = filesize($file);
$this->count--;
$this->bytesUploaded += $fileSize;
$speed = round($fileSize/1024 / ($end - $start));
$avgSpeed = round($this->bytesUploaded/1024 / ($end - $this->startTime));
$seconds = ($this->bytesToUpload - $this->bytesUploaded)/1024 / $avgSpeed;
// we add the number of files to the seconds, estimating that 1 query to add to an album = 3s
$seconds += $this->count*3;
$eta = gmdate("H:i:s", $seconds);
Color::ok(false);
echo "[Speed: $speed Kb/s\tAverage: $avgSpeed Kb/s\t$this->count files to upload\tETA: $eta]\n";
return $photoId;
}
// Transforms a folder (eg. C:\Photos\2014\11) to an album name (eg. 2014::11), taking a root into account
public static function folderToAlbum($path, $root) {
$newPath = substr($path, stripos($path, $root) + strlen($root));
return str_replace(DIRECTORY_SEPARATOR, '::', $newPath);
}
public function pathToAlbum($path) {
$newPath = substr($path, stripos($path, $this->root) + strlen($this->root));
return str_replace(DIRECTORY_SEPARATOR, '::', $newPath);
}
// Retrieves the AutoSync album, using a conf for the name which can vary
public function getAutoSyncAlbum() {
Color::text("Looking for Auto Sync album...\t");
$sets = $this->api->photosets_getList();
$found = false;
foreach($sets['photoset'] as $set) {
$album = new Album($set);
$this->albums[$album->name] = $album;
if ($album->name == $this->autoSyncAlbumName) {
Color::ok();
$found = $album;
}
}
if (!$found) {
Color::text("FAIL!\n", Color::red);
exit;
}
return $found;
}
}
class Color {
const blue = 34;
const green = 32;
const red = 31;
const none = 39;
public static function text($msg, $color=Color::none) {
echo "\e[".$color."m";
echo $msg;
echo "\e[0m";
}
public static function ok($newline=true) {
$nl = ($newline) ? "\n" : "\t";
Color::text("OK!$nl", Color::green);
}
}
class PostUploadAction {
public $name;
public $albumName;
public $mustCreateAlbum;
public function __construct($name, $albumName, $mustCreateAlbum=false) {
$this->name = $name;
$this->albumName = $albumName;
$this->mustCreateAlbum = $mustCreateAlbum;
}
}
class Album {
public $id;
public $name;
public $nbItems;
private $photos;
public function __construct($set) {
$title = utf8_encode(utf8_decode($set['title']['_content']));
$this->name = $title;
$this->id = $set['id'];
$this->nbItems = intval($set['photos']) + intval($set['videos']);
}
public function getPhotos($stfu) {
//load only if needed
if (!$this->photos) {
echo "Loading metadata for album $this->name ...\t";
$set = $stfu->api->photosets_getPhotos($this->id, "url_o,original_format,date_taken,media");
$this->photos = $set['photoset']['photo'];
Color::ok();
}
return $this->photos;
}
// gets instance for newly created albums
public static function initNew($album, $title) {
$album['title']['_content'] = $title;
$album['photos'] = 0;
$album['videos'] = 0;
return new Album($album);
}
}