Use cloudconvert to convert pages files to pdf or any other

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Use Hazel and script to implement a routine for convert pages files to pdf or any other supported file by cloudconvert.
The routine is triggered by ad or updating files in specific folder. On https://cloudconvert.com you find you api key. PDF file are saved in same folder with same name. This solution is much better than using pages because the program is constantly being updated and changed.

Code: Select all
set CLOUDCONVERT_APIKEY to "your api key"

-- Get POSIX file path
set inputFilePath to POSIX path of theFile
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .pdf extension.
set outputFilePath to first text item of inputFilePath & ".pdf"
set AppleScript's text item delimiters to prevTIDs

set theDate to short date string of (get current date)
set theTime to time string of (get current date)
set theMessage to theDate & " " & theTime & " converting " & inputFilePath & " into " & outputFilePath
do shell script "echo \"" & theMessage & "\" >> ~/Library/Logs/Hazel-AppleScript.log"

set theCurl to do shell script "curl -L \"https://api.cloudconvert.com/convert\" -F file=@\"" & inputFilePath & "\" -F \"apikey=" & CLOUDCONVERT_APIKEY & "\" -F \"inputformat=pages\" -F \"outputformat=pdf\" -F \"input=upload\" -F \"wait=true\" -F \"download=inline\" > \"" & outputFilePath & "\""


[img]
www.eli.systems/Bilder/2017-01-29_11-45-00.png
www.eli.systems/Bilder/2017-01-29_11-45-00.png
[/img]
David Ortner
 
Posts: 1
Joined: Sun Jan 29, 2017 6:36 am

Hello David

Would you be available to help me get this going?
CloudConvert api is now version 2
https://cloudconvert.com/api/v2#overview

I can write Hazel Rules, running an embedded shell script (that effectively calls a python3 script passing parameters, if the script is already written). But I do not know how to write the shell script. Do you have a python script that works, that I can copy and use?

i wish to convert epub to azw3 files
The file is either on my dropbox (connected to cloudconvert, now available)
or I grab the file from my computer

I would rather create a watch folder on my computer, and hazel execute every time I put a file in the "watch folder"

Thankyou for any assistance
NoodleNewbie
 
Posts: 34
Joined: Fri Jul 13, 2018 5:17 pm

Hi NoodleNewbie,
Here's a Python script that you can use to convert an EPUB file to an AZW3 file using the CloudConvert API:
Code: Select all
python
import requests
import json

# Set your API key here
api_key = "<your API key>"

# Set the input and output formats
input_format = "epub"
output_format = "azw3"

# Set the input and output options
input_options = {}
output_options = {}

# Set the input and output URLs
input_url = "<your input file URL>"
output_url = "<your output file URL>"

# Set the CloudConvert API endpoint
api_endpoint = "https://api.cloudconvert.com/v2/convert"

# Set the conversion parameters
params = {
    "apikey": api_key,
    "inputformat": input_format,
    "outputformat": output_format,
    "input": "download",
    "output": "download",
    "inputoptions": input_options,
    "outputoptions": output_options,
    "inputurl": input_url,
    "outputurl": output_url
}

# Send the conversion request
response = requests.post(api_endpoint, data=json.dumps(params))

# Get the job ID from the response
job_id = response.json()["data"]["id"]

# Poll the job status until it's completed
while True:
    response = requests.get(api_endpoint + "/" + job_id, params={"apikey": api_key})
    status = response.json()["data"]["status"]["code"]
    if status == "completed":
        break

# Download the output file
response = requests.get(output_url)

# Save the output file to disk
with open("<your output file path>", "wb") as f:
    f.write(response.content)

To use this script, you'll need to replace <your API key>, <your input file URL>, <your output file URL>, and <your output file path> with your own values. You can get your API key from the CloudConvert website.

Once you have the Python script working, you can create a shell script that calls it with the appropriate parameters. Here's an example:

Code: Select all
bash
#!/bin/bash

# Set the input and output file paths
input_file="/path/to/input.epub"
output_file="/path/to/output.azw3"

# Call the Python script to convert the file
python /path/to/convert.py "$input_file" "$output_file"

You can save this shell script to a file and make it executable with chmod +x <script name>. Then, you can set up a Hazel rule to watch a folder and execute this script whenever a file is added to it.territorial io
chrisjones
 
Posts: 2
Joined: Wed Jul 19, 2023 10:54 pm


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron