forked from 500px/500pxPublisher.lrplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path500pxPublishSupport.lua
executable file
·376 lines (321 loc) · 13.4 KB
/
500pxPublishSupport.lua
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
-- Lightroom SDK
local LrApplication = import "LrApplication"
local LrDate = import "LrDate"
local LrErrors = import "LrErrors"
local LrTasks = import "LrTasks"
local LrView = import "LrView"
-- Common Shortcuts
local bind = LrView.bind
-- Logging
local logger = import "LrLogger"("500pxPublisher")
logger:enable("logfile")
-- 500px Plugin
require "500pxAPI"
local publishServiceProvider = {}
publishServiceProvider.small_icon = "500px_icon.png"
-- Description entry in the Publish Manager dialog, if the user does not provide one.
publishServiceProvider.publish_fallbackNameBinding = "username"
publishServiceProvider.titleForGoToPublishedCollection = "Show in 500px"
publishServiceProvider.titleForGoToPublishedPhoto = "Show in 500px"
publishServiceProvider.titleForPublishedCollection = "Collection"
publishServiceProvider.titleForPublishedCollection_standalone = "Collection"
publishServiceProvider.titleForPublishedSmartCollection = "Smart Collection"
publishServiceProvider.titleForPublishedSmartCollection_standalone = "Smart Collection"
function publishServiceProvider.getCollectionBehaviorInfo( publishSettings )
return {
defaultCollectionName = "Library",
defaultCollectionCanBeDeleted = false,
canAddCollection = true,
maxCollectionSetDepth = 0, -- collection sets not supported
}
end
--[[ Called when the user creates a new publish service. Will create a published collection for each collection in the user's portfolio. ]]--
function publishServiceProvider.didCreateNewPublishService( publishSettings, info )
publishSettings.LR_publishService = info.publishService
if publishSettings.syncNow then
if publishSettings.syncCollectionsOnly then
PxUser.syncCollections( publishSettings )
else
PxUser.sync( publishSettings )
end
else
info.publishService.catalog:withWriteAccessDo( "", function()
for _, collection in ipairs( info.publishService:getChildCollections() ) do
collection:setCollectionSettings( { toCommunity = false } )
collection:setRemoteUrl( "http://500px.com/organizer" )
end
local collection = info.publishService:createPublishedCollection( "Public Profile", nil, true )
collection:setCollectionSettings( { toCommunity = true } )
collection:setRemoteUrl( "http://500px.com/" .. publishSettings.username )
end)
end
end
-- called when user attempts to delete published photos, use to customize dialog
function publishServiceProvider.shouldDeletePhotosFromServiceOnDeleteFromCatalog( publishSettings, nPhotos )
-- don't delete anything from 500px
return nil
end
function publishServiceProvider.deletePhotosFromPublishedCollection( publishSettings, arrayOfPhotoIds, deletedCallback )
local success, obj = PxAPI.getCollections( publishSettings )
if obj == "banned" then
LrErrors.throwUserError( "Sorry, this user is inactive or banned. If you think this is a mistake — please contact us by email: [email protected]." )
end
local collections = {}
for _, collection in ipairs( obj.collections ) do
local photos = ","
for _, photo in ipairs( collection.photos ) do
photos = string.format( "%s%i,", photos, photo.id )
end
collections[ tostring( collection.id ) ] = photos
end
for i, remoteId in ipairs( arrayOfPhotoIds ) do
local photo_id, collection_id = string.match( arrayOfPhotoIds[i], "([^-]+)-([^-]+)" )
local success, obj
if collection_id == "profile" then
success, obj = PxAPI.postPhoto( publishSettings, { photo_id = tonumber( photo_id ), privacy = 1 } )
elseif collection_id ~= "nil" then
local photos = collections[ collection_id ]
photos = string.gsub( photos, "," .. photo_id .. ",", "," )
collections[ collection_id ] = photos
success, obj = PxAPI.postCollection( publishSettings, { collection_id = collection_id, photo_ids = string.sub( photos, 2, string.len( photos ) ) } )
else
success, obj = PxAPI.deletePhoto( publishSettings, { photo_id = tonumber( photo_id ) } )
local LrApplication = import "LrApplication"
local catalog = LrApplication:activeCatalog()
local publishServices = catalog:getPublishServices( "com.500px.publisher" )
local publishService
for _, ps in ipairs( publishServices ) do
if ps:getPublishSettings().username == publishSettings.username then
publishService = ps
break
end
end
if PluginInit then PluginInit.lock() end
LrApplication:activeCatalog():withWriteAccessDo( "delete", function()
for _, publishedCollection in ipairs( publishService:getChildCollections() ) do
for _, photo in ipairs( publishedCollection:getPhotos() ) do
if photo:getPropertyForPlugin( _PLUGIN, "photoId" ) == photo_id then
publishedCollection:removePhotos( { photo } )
-- may want to unset all metadata too
photo:setPropertyForPlugin( _PLUGIN, "photoId", nil )
photo:setPropertyForPlugin( _PLUGIN, "views", nil )
photo:setPropertyForPlugin( _PLUGIN, "favorites", nil )
photo:setPropertyForPlugin( _PLUGIN, "votes", nil )
photo:setPropertyForPlugin( _PLUGIN, "category", nil )
photo:setPropertyForPlugin( _PLUGIN, "publishedUUID", nil )
photo:setPropertyForPlugin( _PLUGIN, "previous_tags", nil )
photo:setPropertyForPlugin( _PLUGIN, "privacy", nil )
photo:setPropertyForPlugin( _PLUGIN, "nsfw", nil )
photo:setPropertyForPlugin( _PLUGIN, "license_type", nil )
end
end
end
end)
if PluginInit then PluginInit.unlock() end
end
deletedCallback( remoteId )
end
end
function publishServiceProvider.metadataThatTriggersRepublish( publishSettings )
return {
default = false,
title = true,
caption = true,
keywords = true,
["com.500px.publisher.nsfw"] = true,
["com.500px.publisher.license_type"] = true,
["com.500px.publisher.category"] = true,
}
end
local settings
function publishServiceProvider.validatePublishedCollectionName( newName )
settings.newPath = PxAPI.collectionNameToPath( newName )
return true
end
function publishServiceProvider.endDialogForCollectionSettings( publishSettings, info )
local collectionSettings = info.collectionSettings or {}
collectionSettings.path = collectionSettings.newPath
end
function publishServiceProvider.viewForCollectionSettings( f, publishSettings, info )
settings = info.collectionSettings or {}
-- don't let them edit the default collection
if info.isDefaultCollection or info.name == "Public Profile" or info.name == "Profile" or info.name == "Library" or info.name == "Organizer" then
info.collectionSettings.LR_canSaveCollection = false
return
end
if not info.publishedCollection then
info.collectionSettings.toCommunity = publishSettings.toCommunity
end
settings.newPath = settings.path
return f:group_box {
title = "500px",
fill = 1,
spacing = f:control_spacing(),
bind_to_object = info.collectionSettings,
f:row {
f:static_text {
title = "URL:",
alignment = "right",
},
f:edit_field {
value = bind "newPath",
}
}
}
end
function publishServiceProvider.shouldReverseSequenceForPublishedCollection( publishSettings, collectionInfo )
return false
end
-- not for now
publishServiceProvider.supportsCustomSortOrder = false
function publishServiceProvider.updateCollectionSettings( publishSettings, info )
logger:trace("updateCollectionSettings")
local remoteId = info.publishedCollection:getRemoteId()
if remoteId then
if not info.collectionSettings.newPath then
info.collectionSettings.newPath = PxAPI.collectionNameToPath( info.name )
end
local args = {
collection_id = remoteId,
title = info.name,
path = info.collectionSettings.newPath
}
local success, obj = PxAPI.postCollection( publishSettings, args )
if obj == "other" then
LrErrors.throwUserError( "Sorry, you have to upgrade to create sets." )
end
if not success and obj.status == 404 then
args.collection_id = nil
success, obj = PxAPI.postCollection( publishSettings, args )
end
if success then
info.collectionSettings.path = info.collectionSettings.newPath
info.collectionSettings.newPath = nil
info.publishedCollection.catalog:withPrivateWriteAccessDo( function()
info.publishedCollection:setCollectionSettings( info.collectionSettings )
info.publishedCollection:setRemoteUrl( PxAPI.makeCollectionUrl( publishSettings.username, info.collectionSettings.path ) )
end )
else
LrErrors.throwUserError( "Could not update the collection." )
end
end
end
function publishServiceProvider.shouldDeletePublishedCollection( publishSettings, info )
for i, collection in ipairs( info.collections ) do
if collection:getName() == "Public Profile" then
return "cancel"
end
end
return nil
end
function publishServiceProvider.deletePublishedCollection( publishSettings, info )
if info.name == "Public Profile" then
LrErrors.throwUserError( "You can not delete your public profile." )
elseif info.remoteId then
local success, obj = PxAPI.deleteCollection( publishSettings, { collection_id = info.remoteId } )
if not success and obj.status ~= 404 then
LrErrors.throwUserError( "Could not delete collection from your portfolio. Try again later." )
end
else
LrErrors.throwUserError( "You can not delete this collection" )
end
end
function publishServiceProvider.willDeletePublishService( publishSettings, info )
local publishService = info.publishService
local publishedPhotos = publishService.catalog:findPhotosWithProperty( "com.500px.publisher", "photoId" )
for _, photo in ipairs( publishedPhotos ) do
photo:setPropertyForPlugin( _PLUGIN, "views", nil )
photo:setPropertyForPlugin( _PLUGIN, "favorites", nil )
photo:setPropertyForPlugin( _PLUGIN, "votes", nil )
photo:setPropertyForPlugin( _PLUGIN, "previous_tags", nil )
photo:setPropertyForPlugin( _PLUGIN, "category", nil )
photo:setPropertyForPlugin( _PLUGIN, "nsfw", nil )
photo:setPropertyForPlugin( _PLUGIN, "publishedUUID", nil )
photo:setPropertyForPlugin( _PLUGIN, "photoId", nil )
photo:setPropertyForPlugin( _PLUGIN, "privacy", nil )
photo:setPropertyForPlugin( _PLUGIN, "license_type", nil )
end
end
local function formatDate( datetime )
local year, month, day, hour, minute, second = string.match( datetime, "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+).*" )
return LrDate.timeFromComponents( year, month, day, hour, minute, second, -18000 )
end
function publishServiceProvider.getCommentsFromPublishedCollection( publishSettings, arrayOfPhotoInfo, commentCallback )
for i, photoInfo in ipairs( arrayOfPhotoInfo ) do
local photoId
LrApplication:activeCatalog():withReadAccessDo( function()
photoId = photoInfo.photo:getPropertyForPlugin( _PLUGIN, "photoId" )
end )
local commentList = {}
if photoId then
local page = 1
-- while true do
local success, obj = PxAPI.getComments( publishSettings, { photo_id = photoId, page = page } )
local comments = success and obj.comments
if comments and #comments > 0 then
for _, comment in ipairs( comments) do
table.insert( commentList, {
commentId = comment.id,
commentText = comment.body,
dateCreated = formatDate( comment.created_at ),
username = comment.user.username,
realname = comment.user.fullname,
} )
end
end
-- if success and obj.total_pages == obj.current_page then break end
-- page = page + 1
-- end
end
commentCallback { publishedPhoto = photoInfo, comments = commentList }
end
end
publishServiceProvider.titleForPhotoRating = "Rating"
function publishServiceProvider.getRatingsFromPublishedCollection( publishSettings, arrayOfPhotoInfo, ratingCallback )
logger:trace("getRatingsFromPublishedCollection")
for i, photoInfo in ipairs( arrayOfPhotoInfo ) do
local photoId
LrApplication:activeCatalog():withReadAccessDo( function()
photoId = photoInfo.photo:getPropertyForPlugin( _PLUGIN, "photoId" )
end )
local success, obj = PxAPI.getPhoto( publishSettings, { photo_id = photoId } )
if success then
local rating = obj.photo.rating
-- update photos view, favorites etc...
if PluginInit then PluginInit.lock() end
photoInfo.photo.catalog:withPrivateWriteAccessDo( function()
photoInfo.photo:setPropertyForPlugin( _PLUGIN, "views", tostring( obj.photo.times_viewed ) )
photoInfo.photo:setPropertyForPlugin( _PLUGIN, "favorites", tostring( obj.photo.favorites_count ) )
photoInfo.photo:setPropertyForPlugin( _PLUGIN, "votes", tostring( obj.photo.votes_count ) )
end )
if PluginInit then PluginInit.unlock() end
ratingCallback { publishedPhoto = photoInfo, rating = rating }
else
photoInfo.photo:setPropertyForPlugin( _PLUGIN, "photoId", nil )
local publishedCollections = photoInfo.photo:getContainedPublishedCollections() or {}
for _, publishedCollection in ipairs( publishedCollections ) do
if publishedCollection:getService():getPluginId() == "com.500px.publisher" then
if PluginInit then PluginInit.lock() end
publishedCollection.catalog:withWriteAccessDo( "Delete Photo", function()
publishedCollection:removePhotos( { photo } )
end )
if PluginInit then PluginInit.unlock() end
end
end
ratingCallback { publishedPhoto = photoInfo, rating = 0 }
end
end
end
function publishServiceProvider.cansToService( publishService )
--test if we can contact 500px....
return true
end
function publishServiceProvider.addCommentToPublishedPhoto( publishSettings, remotePhotoId, commentText )
local photoId, collectionId = string.match( remotePhotoId, "([^-]+)-([^-]+)" )
local success, _ = PxAPI.postComment( publishSettings, {
photo_id = photoId,
body = commentText,
} )
return success
end
publishSupport = publishServiceProvider