Explorar o código

integrated calling of thunderbird into the lua code. No helper script necessary any more

Markus Spring hai 8 meses
pai
achega
f5c713fb1e
Modificáronse 1 ficheiros con 103 adicións e 15 borrados
  1. 103 15
      mail2blog.lua

+ 103 - 15
mail2blog.lua

@@ -19,6 +19,7 @@ local dt    = require "darktable"
 local du    = require "lib/dtutils"
 local log   = require 'lib/dtutils.log'
 local dtsys = require "lib/dtutils.system"
+local cjson = require "cjson"
 
 local function quote(text)
   return '"' .. text .. '"'
@@ -146,7 +147,7 @@ end
 local function run_exiftool ( file, tmp_exported, flags )
    local exiftoolbinary = get_executable("exiftoolbinary", "exiftool")
    run_cmd = exiftoolbinary..' -TagsFromFile '..file..' '..flags..' '..tmp_exported
-   LogInfo(string.format("Running %s", run_cmd))
+   -- LogInfo(string.format("Running %s", run_cmd))
    local job = dt.gui.create_job(string.format("Running %s", run_cmd), true, stop_job)
    resp = dtsys.external_command(run_cmd)
    job.valid = false
@@ -167,17 +168,16 @@ local function remove_unneeded_exif_values ( jpeg_file )
   for _, tag in ipairs(tags_to_remove) do
     table.insert(args, "-" .. tag .. "=")
   end
-  table.insert(args, "-FileModifyDate<FileModifyDate")
-  table.insert(args, filename)
+  -- table.insert(args, "-FileModifyDate<FileModifyDate")
+  table.insert(args, jpeg_file)
   
-  --run_exiftool( jpeg_file .. " " .. table.concat(args, " ") )
   local exiftoolbinary = get_executable("exiftoolbinary", "exiftool")
-  run_cmd = exiftoolbinary..' '..jpeg_file..' '.. table.concat(args, " ")
-  LogInfo(string.format("Running %s", run_cmd))
+  run_cmd = exiftoolbinary..' '.. table.concat(args, " ")
+  -- dt.print_log(string.format("Running %s", run_cmd))
   local job = dt.gui.create_job(string.format("Running %s", run_cmd), true, stop_job)
   resp = dtsys.external_command(run_cmd)
   job.valid = false
- return nil  
+  return nil  
 end
 
 -- Function to replace German umlauts and sanitize the filename
@@ -215,6 +215,91 @@ local select_publication_target = dt.new_widget("combobox")
    email_1, email_2, email_3
 }
 
+local function process_image(image_path)
+    local exiftoolbinary = get_executable("exiftoolbinary", "exiftool")
+    -- Get metadata using exiftool
+    local metadata_cmd = exiftoolbinary .. " -j -n -XMP:HierarchicalSubject -Composite:GPSPosition -XMP:Title " .. image_path
+    dt.print_log(metadata_cmd)
+    local handle = io.popen(metadata_cmd)
+    local json_data = handle:read("*a")
+    handle:close()
+
+    print(json_data)
+    
+    -- Parse JSON data using cjson
+    local metadata = cjson.decode(json_data)[1]
+    local first_item = metadata
+    local h_subject = first_item.HierarchicalSubject or ""
+    local gps_position = first_item.GPSPosition or ""
+    local title = first_item.Title or ""
+
+    -- find ^where|
+    local function find_where_value(tbl)
+      for _, value in pairs(tbl) do
+        if type(value) == "string" and string.find(value, "^where|") then
+          return value
+        end
+      end
+      return nil  -- Not found
+    end
+    where = find_where_value(h_subject)  
+    -- print(where)
+    -- extract platz, ort
+    local platz = ""
+    local ort = ""
+    local platz_ort_table = {where:match(".*|(.-)|([^|]+)$")}
+    for i, v in ipairs(platz_ort_table) do
+      -- print(i .. ' ' .. v)
+      if i == 1 then
+        platz = v
+      elseif i == 2 then
+        ort = v
+      end
+    end
+
+    -- print(platz)
+    -- print(ort)
+    
+    -- Process HierarchicalSubject
+    local h_subject_list = {}
+    for _, v in pairs(h_subject) do
+      if not ( tostring(v):find("^darktable") or
+               tostring(v):find("^photography|published") or
+               tostring(v):find("^where|") or
+               tostring(v):find("^LOCKED") ) then
+          table.insert(h_subject_list, v)
+      end
+    end
+
+    -- Extract GPS coordinates
+    local lat, lon = gps_position:match("([%d%.]+) ([%d%.]+)")
+
+    -- Generate caption
+    local caption = string.format(
+        "#img1 %s, %s (<a target='_blank' href='https://www.openstreetmap.org/?mlat=%s&mlon=%s#map=18/%s/%s'>OSM</a>)#",
+        platz or "", ort or "", lat or "", lon or "", lat or "", lon or ""
+    )
+    -- print(caption)
+
+    -- Generate tags, extract the last part of each string and format it
+    local function subject_tags(t)
+      local result = {}
+      for _, str in ipairs(t) do
+          -- Find the last part of the string after the last '|'
+          local lastPart = str:match(".*|([^|]+)$")
+          -- Add it to the result table, wrapped in brackets
+          table.insert(result, "[" .. lastPart .. "]")
+      end
+      -- Concatenate all elements into a single string
+      return table.concat(result, " ")
+    end
+    tags = subject_tags(h_subject_list)
+    -- Generate title
+    title = tags .. " " .. title
+
+    return filtered_subject, title, caption
+end
+
 local publication_button = dt.new_widget("button")
 {
    label = "Publish!",
@@ -242,21 +327,24 @@ local publication_button = dt.new_widget("button")
             run_exiftool( df.sanitize_filename(i.path..PS..i.filename), tmp_exported, '-exif:all --subifd:all --Orientation -overwrite_original' )
             -- copy exif data from xmp file
             run_exiftool( df.sanitize_filename(i.sidecar) , tmp_exported, '-xmp:all -exif:all --subifd:all -overwrite_original' )
+            local h_subject, title, caption = process_image(tmp_exported)
+            print("Title:", title)
+            print("Caption:", caption)
             remove_unneeded_exif_values(tmp_exported)
-            -- run mailscript
-            local spring2lifescript = get_executable("spring2lifescript", "spring2life script")
-            run_cmd = spring2lifescript .. " " .. tmp_exported
-               .. ' ' .. select_publication_target.value
-               .. ' ' .. df.sanitize_filename(i.filename) .. ' &'
-            LogInfo(run_cmd)
+        
+            -- local spring2lifescript = get_executable("spring2lifescript", "spring2life script")
+            -- run_cmd = spring2lifescript .. " " .. tmp_exported
+            --    .. ' ' .. select_publication_target.value
+            --    .. ' ' .. df.sanitize_filename(i.filename) .. ' &'
+            run_cmd = "thunderbird -compose \"to=".. select_publication_target.value ..",subject='" .. title .."',body='" .. caption .. "',attachment='file://" .. tmp_exported .."'\""
+            print(run_cmd)
             job = false
             local job = dt.gui.create_job("Running " .. run_cmd, true, stop_job)
             os.execute(run_cmd)
             set_published_tag ( select_publication_target.value, i )
             job = false
-            -- set_metadata_note ( tmp_exported, i )
+            set_metadata_note ( tmp_exported, i )
             LogScreen("Done")
-            --- dt.styles.delete(tmpstyle)
          end
       end
    end