Skip to content

Instantly share code, notes, and snippets.

@supertobi
Last active December 5, 2019 18:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save supertobi/0d8cb3bd23df8021d935202e2cdb2224 to your computer and use it in GitHub Desktop.
--[[
This file is part of darktable,
Copyright 2019 by Tobias Jakobs.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
darktable Wordpress export script
ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT
* curl
USAGE
* require this script from your main Lua file
]]
local dt = require "darktable"
local du = require "lib/dtutils"
local df = require "lib/dtutils.file"
local ds = require "lib/dtutils.string"
local dsys = require "lib/dtutils.system"
local gettext = dt.gettext
local PS = dt.configuration.running_os == "windows" and "\\" or "/"
du.check_min_api_version("5.0.0", wordpress_export)
-- Tell gettext where to find the .mo file translating messages for a particular domain
gettext.bindtextdomain("wordpress_export",dt.configuration.config_dir.."/lua/locale/")
local function _(msgid)
return gettext.dgettext("wordpress_export", msgid)
end
local function show_status(storage, image, format, filename, number, total, high_quality, extra_data)
dt.print(string.format(_("Export Image %i/%i"), number, total))
end
-- Add duplicate index to filename
-- image.filename does not have index, exported_image has index
function addDuplicateIndex( index, filename )
if index > 0 then
filename = filename.."_"
if index < 10 then
filename = filename.."0"
end
filename = filename..index
end
return filename
end
local function export(storage, image_table, extra_data)
local curlPath
if dt.configuration.running_os == "linux" then
curlPath = 'curl'
else
curlPath = dt.preferences.read("wordpress_export","curlPath","string")
end
if not df.check_if_bin_exists(curlPath) then
dt.print_error(_("curl not found"))
return
end
dt.print_log("Will try to export to WordPress")
local imageFoldername
exportDirectory = dt.preferences.read("wordpress_export","ExportDirectory","string")
-- Creates dir if not exsists
imageFoldername = "files"..PS
df.mkdir(df.sanitize_filename(exportDirectory..PS..imageFoldername))
for image,exported_image in pairs(image_table) do
-- Extract filename, e.g DSC9784.ARW -> DSC9784
filename = string.upper(string.gsub(image.filename,"%.%w*", ""))
-- Handle duplicates
filename = addDuplicateIndex( image.duplicate_index, filename )
-- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG
extension = string.match(exported_image,"%.%w*$")
local image_title, image_description, image_longitude, image_latitude, image_exif_datetime_taken, image_creator
if (image.title and image.title ~= "") then
image_title = ds.escape_xml_characters(image.title)
else
image_title = filename..extension
end
image_description = ds.escape_xml_characters(image.description)
image_longitude = string.gsub(tostring(image.longitude),",", ".")
image_latitude = string.gsub(tostring(image.latitude),",", ".")
image_exif_datetime_taken = string.gsub(image.exif_datetime_taken," ", "T")
image_creator = ds.escape_xml_characters(image.creator)
end
local sendToWordPressCommand = "curl DO SOMETHING" --ToDo
dsys.external_command(sendToWordPressCommand)
end
-- Preferences
local defaultDir = ''
if dt.configuration.running_os == "windows" then
defaultDir = os.getenv("USERPROFILE")
elseif dt.configuration.running_os == "macos" then
defaultDir = os.getenv("home")
else
local handle = io.popen("xdg-user-dir DESKTOP")
defaultDir = handle:read()
handle:close()
end
dt.preferences.register("wordpress_export",
"ExportDirectory",
"directory",
_("WordPress export: Export directory"),
_("A directory that will be used to export the files"),
defaultDir )
if dt.configuration.running_os ~= "linux" then
dt.preferences.register("wordpress_export",
"curlPath", -- name
"file", -- type
_("WordPress export: curl binary Location"), -- label
_("Install location of curl[.exe]. Requires restart to take effect."), -- tooltip
"curl") -- default
end
--https://www.darktable.org/lua-api/ar01s02s54.html.php
local post_titel = dt.new_widget("entry")
{
text = _("Titel"),
placeholder = "placeholder",
editable = true,
tooltip = _("Tooltip Text"),
reset_callback = function(self) self.text = "text" end
}
local post_category = dt.new_widget("entry")
{
text = _("Category"),
placeholder = "placeholder",
editable = true,
tooltip = _("Tooltip Text"),
reset_callback = function(self) self.text = "text" end
}
local post_tags = dt.new_widget("entry")
{
text = _("Tags"),
placeholder = "placeholder",
editable = true,
tooltip = _("Tooltip Text"),
reset_callback = function(self) self.text = "text" end
}
local widgets_list = {}
--if not df.check_if_bin_exists("curl") then
-- table.insert(widgets_list, df.executable_path_widget({"curl"}))
--end
table.insert(widgets_list, post_titel)
table.insert(widgets_list, post_category)
table.insert(widgets_list, post_tags)
local module_widget = dt.new_widget("box") {
orientation = "vertical",
table.unpack(widgets_list)
}
-- Register
dt.register_storage("wordpress_export",
_("WordPress Export"),
nil,
export,
nil,
nil,
module_widget)
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua
-- kate: hl Lua;
@wpferguson
Copy link

A thought about the export directory. Do we want to save the images locally or just send them to wordpress? If we take control of the export, then darktable defaults to exporting the images to config.tmp_dir. We can just take them one by one (or maybe as a batch) and send them to wordpress, then delete them after we are done.

We'll need to get logon and password. Do we store them, or ask each time?

macOS and linux have curl in the path. Windows needs curl.exe. You could use df.get_executable_path_widget({curl}) and it will provide the file selector widget for windows and store the result in a preference that df.check_if_bin_exists() looks for.

This may be a script that needs a lib dir. I'm thinking about putting all the rest stuff in a library. Thoughts?

If you would like, go ahead and start a repository with this, and I'll fork it and do PR's.

@supertobi
Copy link
Author

@wpferguson

  1. I've created a branch here:
    https://github.com/darktable-org/lua-scripts/blob/wordpress_export.lua/contrib/wordpress_export.lua

Feel free to do what ever you like with it. ;)

  1. @aurelienpierre should know all the other answers, I have no wp and no experience with it. The script is just a blindly hacked skeleton

  2. At least Windows 10 has curl in his path too, I just checkt on a fresh installation:
    https://daniel.haxx.se/blog/2018/01/13/microsoft-curls-too/

@aurelienpierre
Copy link

Do we want to save the images locally or just send them to wordpress? If we take control of the export, then darktable defaults to exporting the images to config.tmp_dir. We can just take them one by one (or maybe as a batch) and send them to wordpress, then delete them after we are done.

I will go as far as comparing control sums on distant server and local client, so maybe using a temp dir and deleting it once upload is complete and verified sounds like the best way.

We'll need to get logon and password. Do we store them, or ask each time?

WP stores a cookie, when using Rest API. I need to check what its lifespan is. Bottom line is, as long as the cookie is active, you don't need to login again. Otherwise, we can store in using the same libsecret or kwallet or whatever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment