Created
August 21, 2022 18:44
-
-
Save a2ikm/587ccc4b3755eaaa1b52bdb1e916be2b to your computer and use it in GitHub Desktop.
Convert CSV exported from Raindrop to Delcious XML format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# c.f. https://github.com/domainersuitedev/delicious-api/blob/master/api/posts.md#v1postsall | |
# | |
# <posts tag="" user="user"> | |
# <post href="http://www.weather.com/" description="weather.com" | |
# hash="6cfedbe75f413c56b6ce79e6fa102aba" tag="weather reference" | |
# time="2005-11-29T20:30:47Z" /> | |
# ... | |
# <post href="http://www.nytimes.com/" | |
# description="The New York Times - Breaking News, World News & Multimedia" | |
# extended="requires login" hash="ca1e6357399774951eed4628d69eb84b" | |
# tag="news media" time="2005-11-29T20:30:05Z" /> | |
# </posts> | |
require "csv" | |
begin | |
require "rexml" | |
rescue LoadError | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "rexml" | |
end | |
end | |
USAGE = <<~USAGE | |
#{File.basename($0)} <csv> | |
USAGE | |
path = ARGV[0] | |
if path.nil? | |
$stderr.puts USAGE | |
abort | |
end | |
if !File.exist?(path) | |
$stderr.puts "#{path} doesn't exist" | |
abort | |
end | |
items = [] | |
CSV.foreach(path, headers: true) do |row| | |
items << { | |
"description" => row["title"], | |
"extended" => "", | |
"href" => row["url"], | |
"tag" => (row["tags"] || "").split(", ").join(" "), | |
"time" => row["created"], | |
"hash" => "", | |
} | |
end | |
doc = REXML::Document.new | |
doc.context[:attribute_quote] = :quote | |
posts = REXML::Element.new("posts", doc, doc.context) | |
posts.add_attributes("tags" => "", "user" => "") | |
items.each do |item| | |
post = REXML::Element.new("post", posts, posts.context) | |
post.add_attributes(item) | |
end | |
doc.write($stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment