Last active
March 12, 2025 01:26
-
-
Save matiaskorhonen/40d46b122f061420787dde414b238260 to your computer and use it in GitHub Desktop.
A rough Crouton (https://crouton.app) to Mela (https://mela.recipes) recipe converter. The imported recipes may require some clean-up…
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 | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "tty-logger", "~> 0.6" | |
gem "pry" | |
gem "dotiw" | |
end | |
require "json" | |
require "uri" | |
include DOTIW::Methods | |
logger = TTY::Logger.new | |
crumbs = Dir.glob("./crouton/*.crumb") | |
crumbs.each do |file| | |
logger.info "Processing #{file}" | |
crumb = JSON.parse(File.read(file)) | |
uri = URI.parse(crumb["webLink"]) | |
totalTime = crumb["duration"].to_i + crumb["cookingDuration"].to_i | |
mela = { | |
id: "#{uri.host}#{uri.path.gsub(/\/\z/, "")}", | |
title: crumb["name"], | |
categories: [], | |
nutrition: "", | |
favorite: false, | |
link: crumb["webLink"], | |
prepTime: crumb["duration"].to_i > 0 ? distance_of_time(crumb["duration"]*60) : "", | |
totalTime: totalTime > 0 ? distance_of_time(totalTime*60) : "", | |
date: Time.now.to_i - 978307200, | |
images: crumb["images"], | |
wantToCook: false, | |
ingredients: crumb["ingredients"].map { |ingredient| | |
amount = ingredient.dig("quantity", "amount") | |
unit = case ingredient.dig("quantity", "quantityType")&.downcase | |
when "grams" | |
"g" | |
when "kgs" | |
"kg" | |
when "litres" | |
"l" | |
when "mills" | |
"ml" | |
when "teaspoon" | |
"tsp" | |
when "tablespoon" | |
"tbsp" | |
when "cup" | |
"cups" | |
when "pound" | |
"lb" | |
when "ounce" | |
"oz" | |
when "pinches" | |
"pinch" | |
when "section" | |
"#" | |
when nil, "item" | |
nil | |
else | |
raise "Unknown unit: #{ingredient.dig("quantity", "quantityType").inspect}" | |
end | |
name = ingredient.dig("ingredient", "name").downcase | |
amount_and_unit = if %w(g kg ml l lb oz).include?(unit) | |
[amount, unit].compact.join | |
elsif amount.nil? && unit.nil? | |
nil | |
else | |
[amount, unit].compact.join(" ") | |
end | |
[amount_and_unit, name].compact.join(" ") | |
}.join("\n"), | |
text: "", | |
instructions: crumb["steps"].map { |step| step["step"] }.join("\n"), | |
notes: crumb["notes"] | |
} | |
File.write("./mela/#{File.basename(file, ".*")}.melarecipe", JSON.pretty_generate(mela)) | |
end | |
# Mela format: | |
# | |
# { | |
# "id": "rasamalaysia.com/asian-cucumber-salad", | |
# "categories": [], | |
# "nutrition": "**Serving size** 2 people\n**Calories** 95 calories\n**Fat** 3.9 grams fat\n**Saturated fat** 0.6 grams saturated fat\n**Sodium** 889 grams sodium\n**Carbohydrates** 16.2 grams carbohydrates\n**Fiber** 1.3 grams fiber\n**Sugar** 11.6 grams sugar\n**Protein** 1.3 grams protein", | |
# "favorite": false, | |
# "link": "https://rasamalaysia.com/asian-cucumber-salad/", | |
# "totalTime": "20min", | |
# "title": "Asian Cucumber Salad", | |
# "notes": "", | |
# "date": 651490594.572202, | |
# "ingredients": "1 cucumber (about 225 g)\n3/4 teaspoon salt or to taste\n2 cloves garlic, minced\n1 teaspoon apple cider vinegar\n1 1/2 tablespoons sugar\n1 teaspoon Korean chili powder or regular chili powder\n1 teaspoon chili oil\n1/2 teaspoon sesame oil\nsesame seeds for garnishing", | |
# "text": "Asian Cucumber Salad – healthy cucumber salad with Asian spices. So refreshing and easy. A perfect appetizer for any meals!", | |
# "prepTime": "5min", | |
# "images": ["base64 images""], | |
# "wantToCook": false, | |
# "instructions": "Rinse the cucumber thoroughly and slice it into pieces. Add the salt to the cucumber, stir gently to combine well. Chill in the refrigerator for 15 minutes.\nCombine the garlic, vinegar, sugar, chili powder, chili oil, and sesame oil together, stir to mix well. Make sure that the sugar is completely dissolved.\nDrain the salt water from the cucumber. Add the dressing to the cucumber, toss well. Top with sesame seeds and serve immediately." | |
# } | |
# Crouton format: | |
# | |
# { | |
# "serves": 2, | |
# "folderIDs": [], | |
# "ingredients": [ | |
# { | |
# "quantity": { | |
# "amount": 225, | |
# "quantityType": "GRAMS" | |
# }, | |
# "ingredient": { | |
# "name": "Cucumber", | |
# "uuid": "9B4B7DF5-64E6-4171-88E0-1C29A938D253" | |
# }, | |
# "order": 0, | |
# "uuid": "A3914182-15E4-4D09-AE37-A2DAA10E534B" | |
# }, | |
# { | |
# "quantity": { | |
# "amount": 1, | |
# "quantityType": "TEASPOON" | |
# }, | |
# "ingredient": { | |
# "name": "Chili oil", | |
# "uuid": "0C216EAD-A361-40F8-BC3C-4D6B21ED2265" | |
# }, | |
# "order": 1, | |
# "uuid": "CC6EE54E-2DB0-4A03-819A-B2BE815B430C" | |
# }, | |
# { | |
# "quantity": { | |
# "amount": 2, | |
# "quantityType": "ITEM" | |
# }, | |
# "ingredient": { | |
# "name": "Cloves garlic, minced", | |
# "uuid": "2748943A-7E97-4538-988E-80D77388D146" | |
# }, | |
# "order": 2, | |
# "uuid": "74755E17-DA10-4CCC-A3DB-53186063540F" | |
# } | |
# ], | |
# "webLink": "https://rasamalaysia.com/asian-cucumber-salad/", | |
# "steps": [ | |
# { | |
# "step": "Rinse the cucumber thoroughly and slice it into pieces. Add the salt to the cucumber, stir gently to combine well. Chill in the refrigerator for 15 minutes.", | |
# "order": 0, | |
# "uuid": "22923C03-9CEB-415E-BF1B-C101A384623A" | |
# }, | |
# { | |
# "step": "Combine the garlic, vinegar, sugar, chili powder, chili oil, and sesame oil together, stir to mix well. Make sure that the sugar is completely dissolved.", | |
# "order": 1, | |
# "uuid": "BE6CE7E5-DD74-4852-9CB8-293D55CE9400" | |
# }, | |
# { | |
# "step": "Drain the salt water from the cucumber. Add the dressing to the cucumber, toss well. Top with sesame seeds and serve immediately.", | |
# "order": 2, | |
# "uuid": "FE7BEC18-761B-425F-9622-4392DD7A674B" | |
# } | |
# ], | |
# "images": [ | |
# "base64 images" | |
# ], | |
# "uuid": "F5A15C33-A664-4E5C-BD24-93803AD37EE7", | |
# "duration": 5, | |
# "cookingDuration": 15, | |
# "name": "Asian Cucumber Salad" | |
# } |
Does Mela require that the IDs be unique?
I don't remember the details, it's been a long time since I switched to Mela and I only worked on the script up to the point where it worked for me 😅
@matiaskorhonen Thanks for sharing. If you don't mind sharing, why did you end up switching?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I updated your gist to add a few more unit types, and to generate a hash of the name of the recipe if the webLink attribute is missing.
I'm not sure about the latter... Does Mela require that the IDs be unique? If so, two recipes with the same name would fail this requirement. I could generate a random string instead, but then multiple runs on the same recipe would generate duplicates.
Anyway, feel free to merge my changes back into your gist... I'd rather your code be the source of truth. I'll delete mine after you merge. https://gist.github.com/coppit/e00930ced11bb861e09503ab4e26c06b
Thanks for writing this!