|
| 1 | +--[[ |
| 2 | + This file is part of darktable, |
| 3 | + Copyright 2019 by Tobias Jakobs. |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation; either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +]] |
| 18 | +--[[ |
| 19 | +darktable Wordpress export script |
| 20 | +
|
| 21 | +ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT |
| 22 | +* curl |
| 23 | +
|
| 24 | +USAGE |
| 25 | +* require this script from your main Lua file |
| 26 | +
|
| 27 | +]] |
| 28 | + |
| 29 | +local dt = require "darktable" |
| 30 | +local du = require "lib/dtutils" |
| 31 | +local df = require "lib/dtutils.file" |
| 32 | +local ds = require "lib/dtutils.string" |
| 33 | +local dsys = require "lib/dtutils.system" |
| 34 | + |
| 35 | +local gettext = dt.gettext |
| 36 | + |
| 37 | +local PS = dt.configuration.running_os == "windows" and "\\" or "/" |
| 38 | + |
| 39 | +du.check_min_api_version("5.0.0", wordpress_export) |
| 40 | + |
| 41 | +-- Tell gettext where to find the .mo file translating messages for a particular domain |
| 42 | +gettext.bindtextdomain("wordpress_export",dt.configuration.config_dir.."/lua/locale/") |
| 43 | + |
| 44 | +local function _(msgid) |
| 45 | + return gettext.dgettext("wordpress_export", msgid) |
| 46 | +end |
| 47 | + |
| 48 | +local function show_status(storage, image, format, filename, number, total, high_quality, extra_data) |
| 49 | + dt.print(string.format(_("Export Image %i/%i"), number, total)) |
| 50 | +end |
| 51 | + |
| 52 | +-- Add duplicate index to filename |
| 53 | +-- image.filename does not have index, exported_image has index |
| 54 | +function addDuplicateIndex( index, filename ) |
| 55 | + if index > 0 then |
| 56 | + filename = filename.."_" |
| 57 | + if index < 10 then |
| 58 | + filename = filename.."0" |
| 59 | + end |
| 60 | + filename = filename..index |
| 61 | + end |
| 62 | + |
| 63 | + return filename |
| 64 | +end |
| 65 | + |
| 66 | +local function export(storage, image_table, extra_data) |
| 67 | + |
| 68 | + local curlPath |
| 69 | + if dt.configuration.running_os == "linux" then |
| 70 | + curlPath = 'curl' |
| 71 | + else |
| 72 | + curlPath = dt.preferences.read("wordpress_export","curlPath","string") |
| 73 | + end |
| 74 | + |
| 75 | + if not df.check_if_bin_exists(curlPath) then |
| 76 | + dt.print_error(_("curl not found")) |
| 77 | + return |
| 78 | + end |
| 79 | + dt.print_log("Will try to export to WordPress") |
| 80 | + |
| 81 | + local imageFoldername |
| 82 | + exportDirectory = dt.preferences.read("wordpress_export","ExportDirectory","string") |
| 83 | + -- Creates dir if not exsists |
| 84 | + imageFoldername = "files"..PS |
| 85 | + df.mkdir(df.sanitize_filename(exportDirectory..PS..imageFoldername)) |
| 86 | + |
| 87 | + for image,exported_image in pairs(image_table) do |
| 88 | + -- Extract filename, e.g DSC9784.ARW -> DSC9784 |
| 89 | + filename = string.upper(string.gsub(image.filename,"%.%w*", "")) |
| 90 | + -- Handle duplicates |
| 91 | + filename = addDuplicateIndex( image.duplicate_index, filename ) |
| 92 | + -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG |
| 93 | + extension = string.match(exported_image,"%.%w*$") |
| 94 | + |
| 95 | + local image_title, image_description, image_longitude, image_latitude, image_exif_datetime_taken, image_creator |
| 96 | + if (image.title and image.title ~= "") then |
| 97 | + image_title = ds.escape_xml_characters(image.title) |
| 98 | + else |
| 99 | + image_title = filename..extension |
| 100 | + end |
| 101 | + |
| 102 | + image_description = ds.escape_xml_characters(image.description) |
| 103 | + image_longitude = string.gsub(tostring(image.longitude),",", ".") |
| 104 | + image_latitude = string.gsub(tostring(image.latitude),",", ".") |
| 105 | + image_exif_datetime_taken = string.gsub(image.exif_datetime_taken," ", "T") |
| 106 | + image_creator = ds.escape_xml_characters(image.creator) |
| 107 | + end |
| 108 | + |
| 109 | + local sendToWordPressCommand = "curl DO SOMETHING" --ToDo |
| 110 | + dsys.external_command(sendToWordPressCommand) |
| 111 | + |
| 112 | +end |
| 113 | + |
| 114 | +-- Preferences |
| 115 | + |
| 116 | +local defaultDir = '' |
| 117 | +if dt.configuration.running_os == "windows" then |
| 118 | + defaultDir = os.getenv("USERPROFILE") |
| 119 | +elseif dt.configuration.running_os == "macos" then |
| 120 | + defaultDir = os.getenv("home") |
| 121 | +else |
| 122 | + local handle = io.popen("xdg-user-dir DESKTOP") |
| 123 | + defaultDir = handle:read() |
| 124 | + handle:close() |
| 125 | +end |
| 126 | + |
| 127 | +dt.preferences.register("wordpress_export", |
| 128 | + "ExportDirectory", |
| 129 | + "directory", |
| 130 | + _("WordPress export: Export directory"), |
| 131 | + _("A directory that will be used to export the files"), |
| 132 | + defaultDir ) |
| 133 | + |
| 134 | +if dt.configuration.running_os ~= "linux" then |
| 135 | + dt.preferences.register("wordpress_export", |
| 136 | + "curlPath", -- name |
| 137 | + "file", -- type |
| 138 | + _("WordPress export: curl binary Location"), -- label |
| 139 | + _("Install location of curl[.exe]. Requires restart to take effect."), -- tooltip |
| 140 | + "curl") -- default |
| 141 | +end |
| 142 | + |
| 143 | + |
| 144 | +--https://www.darktable.org/lua-api/ar01s02s54.html.php |
| 145 | +local post_titel = dt.new_widget("entry") |
| 146 | +{ |
| 147 | + text = _("Titel"), |
| 148 | + placeholder = "placeholder", |
| 149 | + editable = true, |
| 150 | + tooltip = _("Tooltip Text"), |
| 151 | + reset_callback = function(self) self.text = "text" end |
| 152 | +} |
| 153 | + |
| 154 | +local post_category = dt.new_widget("entry") |
| 155 | +{ |
| 156 | + text = _("Category"), |
| 157 | + placeholder = "placeholder", |
| 158 | + editable = true, |
| 159 | + tooltip = _("Tooltip Text"), |
| 160 | + reset_callback = function(self) self.text = "text" end |
| 161 | +} |
| 162 | + |
| 163 | +local post_tags = dt.new_widget("entry") |
| 164 | +{ |
| 165 | + text = _("Tags"), |
| 166 | + placeholder = "placeholder", |
| 167 | + editable = true, |
| 168 | + tooltip = _("Tooltip Text"), |
| 169 | + reset_callback = function(self) self.text = "text" end |
| 170 | +} |
| 171 | + |
| 172 | +local widgets_list = {} |
| 173 | + |
| 174 | +--if not df.check_if_bin_exists("curl") then |
| 175 | +-- table.insert(widgets_list, df.executable_path_widget({"curl"})) |
| 176 | +--end |
| 177 | +table.insert(widgets_list, post_titel) |
| 178 | +table.insert(widgets_list, post_category) |
| 179 | +table.insert(widgets_list, post_tags) |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +local module_widget = dt.new_widget("box") { |
| 184 | + orientation = "vertical", |
| 185 | + table.unpack(widgets_list) |
| 186 | +} |
| 187 | + |
| 188 | +-- Register |
| 189 | +dt.register_storage("wordpress_export", |
| 190 | + _("WordPress Export"), |
| 191 | + nil, |
| 192 | + export, |
| 193 | + nil, |
| 194 | + nil, |
| 195 | + module_widget) |
| 196 | + |
| 197 | +-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua |
| 198 | +-- kate: hl Lua; |
0 commit comments