134 lines
3.4 KiB
Lua
134 lines
3.4 KiB
Lua
local dt = require "darktable"
|
|
local df = require "lib/dtutils.file"
|
|
local os = require "os"
|
|
|
|
-- Some fucking bullshit happening right here.
|
|
function os.capture(command, raw)
|
|
local f = assert(io.popen(command, 'r'))
|
|
local s = assert(f:read('*a'))
|
|
f:close()
|
|
if raw then return s end
|
|
s = string.gsub(s, '^%s+', '')
|
|
s = string.gsub(s, '%s+$', '')
|
|
s = string.gsub(s, '[\n\r]+', ' ')
|
|
return s
|
|
end
|
|
|
|
local publish_title = dt.new_widget("entry") {
|
|
placeholder = "Post Title",
|
|
tooltip = "enter title for the post"
|
|
}
|
|
|
|
local publish_slug = dt.new_widget("entry") {
|
|
placeholder = "post-slug",
|
|
tooltip = "enter slug for the post (URL-friendly)"
|
|
}
|
|
|
|
local publish_keywords = dt.new_widget("entry") {
|
|
placeholder = "keywords (space-separated)",
|
|
tooltip = "enter keywords (tags) for the post"
|
|
}
|
|
|
|
local strip_gps_checkbox = dt.new_widget("check_button") {
|
|
label = "Strip GPS data",
|
|
value = false,
|
|
tooltip = "remove GPS metadata from files before uploading"
|
|
}
|
|
|
|
local widget = dt.new_widget("box") {
|
|
orientation = "vertical",
|
|
publish_title,
|
|
publish_slug,
|
|
publish_keywords,
|
|
strip_gps_checkbox
|
|
}
|
|
|
|
local function initialize(_, _, images, _, extra_data)
|
|
extra_data.exported_files = {}
|
|
extra_data.cleanup_files = {}
|
|
|
|
if publish_title.text == "" then
|
|
extra_data.title = df.get_basename(images[1].filename)
|
|
else
|
|
extra_data.title = publish_title.text
|
|
end
|
|
|
|
if publish_slug.text == "" then
|
|
extra_data.slug = df.get_basename(images[1].filename)
|
|
else
|
|
extra_data.slug = publish_slug.text
|
|
end
|
|
|
|
extra_data.keywords = publish_keywords.text
|
|
extra_data.strip_gps = strip_gps_checkbox.value
|
|
|
|
return images
|
|
end
|
|
|
|
local function store(_, image, _, filename, _, _, _, extra_data)
|
|
if extra_data.strip_gps then
|
|
local command = string.format("exiftool -gps:all= -overwrite_original '%s'", filename)
|
|
os.execute(command)
|
|
end
|
|
|
|
if image.is_raw then
|
|
local original_path = image.path .. "/" .. image.filename
|
|
local raw_filename = original_path
|
|
|
|
if extra_data.strip_gps then
|
|
local tmpfile = os.tmpname()
|
|
local command = string.format("exiftool -gps:all= -o '%s' '%s'", tmpfile, original_path)
|
|
os.execute(command)
|
|
table.insert(extra_data.cleanup_files, tmpfile)
|
|
raw_filename = tmpfile
|
|
end
|
|
|
|
table.insert(extra_data.exported_files, filename .. ":" .. raw_filename)
|
|
else
|
|
table.insert(extra_data.exported_files, filename)
|
|
end
|
|
end
|
|
|
|
local function finalize(_, _, extra_data)
|
|
local files_arg = table.concat(extra_data.exported_files, " ")
|
|
|
|
local command = string.format(
|
|
"darktable-ghost-cms-publish --title '%s' --slug '%s' %s",
|
|
extra_data.title, extra_data.slug, files_arg
|
|
)
|
|
if extra_data.keywords ~= "" then
|
|
command = command .. string.format(" --keywords %s", extra_data.keywords)
|
|
end
|
|
|
|
-- Ignore that I use an external tool (written in JavaScript god forbid)
|
|
-- I am _not_ doing JSON generation and web requests in Lua
|
|
local result = os.capture(command)
|
|
|
|
if result and result:match("^http") then
|
|
dt.print("Post published: " .. result)
|
|
else
|
|
dt.print("Failed to publish post.")
|
|
end
|
|
|
|
local command = string.format("xdg-open %s", result)
|
|
os.execute(command)
|
|
|
|
for _, tmpfile in ipairs(extra_data.cleanup_files) do
|
|
os.remove(tmpfile)
|
|
end
|
|
end
|
|
|
|
local function supported(_, _)
|
|
return true
|
|
end
|
|
|
|
dt.register_storage(
|
|
"ghost_cms_publish",
|
|
"publish to Ghost CMS",
|
|
store,
|
|
finalize,
|
|
supported,
|
|
initialize,
|
|
widget
|
|
)
|