Blame

dcd756 Tebby Dog 2025-07-17 00:14:04
Initial
1
# Email-to-Obsidian-Sync
2
6784f3 Tebby Dog 2025-07-17 00:17:43
Added Purpose and what else is missing
3
## Purpose
4
This script paired with an IOS shortcut that sends a link to my email server when I find something lets me easily save content that I may find useful later. ETOS uses fabric to summarize and generates a markdown file that is then sent to the obsidian rest api.
5
6
Theres some crap missing here I still need to add
7
- Fabric Prompt
8
- Directions for REST API
9
- Obsidian Docker Container
10
- Directions for fetchmail setup
11
dcd756 Tebby Dog 2025-07-17 00:14:04
Initial
12
```bash
13
#!/bin/bash
14
# Define log levels and output functions
15
LOG_LEVEL=INFO
16
MAX_LOG_LINES=1000
17
log_debug() { [ "$LOG_LEVEL" = "DEBUG" ] && echo "$(date +"%Y%m%d_%H%M%S") - DEBUG: $@"; }
18
log_info() { echo "$(date +"%Y%m%d_%H%M%S") - INFO: $@"; }
19
log_warn() { echo "$(date +"%Y%m%d_%H%M%S") - WARN: $@" >&2; }
20
log_error() { echo "$(date +"%Y%m%d_%H%M%S") - ERROR: $@" >&2; }
21
22
# Configuration for Obsidian REST API
23
OBSIDIAN_API_URL="http://127.0.0.1:27123"
24
OBSIDIAN_API_KEY="APIKEY" # Replace with your actual API key
25
26
# Step 1: Add date and time stamp to the fetched email file name
27
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
28
FETCHED_EMAIL_FILE="/home/etos/fetched_email_${TIMESTAMP}.txt"
29
LOG_FILE="/var/log/etos_cron.log"
30
touch $FETCHED_EMAIL_FILE
31
32
# Add a visual separator
33
log_info "-------------------------- $(date) --------------------------"
34
35
# Log FETCH operation
36
log_info "Fetching emails..."
37
if [ "$LOG_LEVEL" = "DEBUG" ]; then
38
fetchmail -v --mda "cat > $FETCHED_EMAIL_FILE" --keep --fetchlimit 1 # add --fetchall to get emails that have been fetched before
39
else
40
fetchmail -s --mda "cat > $FETCHED_EMAIL_FILE" --keep --fetchlimit 1 # add --fetchall to get emails that have been fetched before
41
fi
42
43
# Step 2: Extract the first URL from the email body
44
EMAIL_URL=$(sed -n 's/.*\(http[s]:\/\/[^ ]*\).*/\1/p' "$FETCHED_EMAIL_FILE" | head -n 1)
45
if [ "$LOG_LEVEL" = "DEBUG" ]; then
46
log_debug "Extracted URL: $EMAIL_URL"
47
fi
48
49
# Step 3: Verify that the URL is not empty and is a valid URL format
50
if [[ "$EMAIL_URL" =~ ^http[s]?:\/\/[a-zA-Z0-9.-]+\/?.*$ ]]; then
51
# Log successful extraction
52
log_info "Extracted URL: $EMAIL_URL"
53
54
# Step 4: Use fabric to process the URL
55
FABRIC_OUTPUT=$(mktemp)
56
57
# Run the fabric command with the extracted URL
58
/usr/local/bin/fabric --pattern explain_docs --output="$FABRIC_OUTPUT" --scrape_url="$EMAIL_URL"
59
60
if [ $? -ne 0 ]; then
61
log_error "Fabric execution failed!"
62
rm "$FABRIC_OUTPUT"
63
exit 1
64
else
65
log_info "Fabric execution successful!"
66
fi
67
68
# Step 5: Extract the title for naming the note - only get the first line matching title pattern
69
TITLE=$(head -n 10 "$FABRIC_OUTPUT" | grep -E '^(# |Title: )' | head -n 1 | sed -E 's/^# //;s/^Title: //')
70
71
if [ -z "$TITLE" ]; then
72
# If no title found, use a default name with timestamp
73
TITLE="New_${TIMESTAMP}"
74
log_debug "No title found in the first 10 lines of the file, using default: $TITLE"
75
else
76
# Clean up the title by removing unwanted characters
77
TITLE=$(echo "$TITLE" | sed -E 's/^(# |Title: )//; s/[ ]+/_/g; s/[^[:alnum:]_.-]//g; s/__+/_/g')
78
log_debug "Using extracted title: $TITLE"
79
fi
80
81
# Step 6: Prepare content for API
82
TEMP_CONTENT_FILE=$(mktemp)
83
echo -e "URL: $EMAIL_URL\n\n$(cat "$FABRIC_OUTPUT")" > "$TEMP_CONTENT_FILE"
84
85
# Test API connectivity before making the call
86
log_debug "Testing Obsidian API connectivity..."
87
test_response=$(curl -s -o /dev/null -w "%{http_code}" "${OBSIDIAN_API_URL}/")
88
if [ "$test_response" = "200" ]; then
89
log_debug "Obsidian API is accessible"
90
else
91
log_error "Obsidian API is not accessible, received code: $test_response"
92
fi
93
94
# URL encode the title for the API path
95
ENCODED_TITLE=$(echo "$TITLE" | sed 's/ /%20/g')
96
97
# Step 7: Send the content to Obsidian via REST API
98
log_info "Sending content to Obsidian via REST API..."
99
100
curl_output=$(curl -s -X PUT \
101
"${OBSIDIAN_API_URL}/vault/New/${ENCODED_TITLE}.md" \
102
-H "Authorization: Bearer ${OBSIDIAN_API_KEY}" \
103
-H "Content-Type: text/markdown" \
104
--data-binary @"$TEMP_CONTENT_FILE" \
105
--write-out "%{http_code}" \
106
-o /dev/null)
107
108
CURL_EXIT_CODE=$?
109
HTTP_CODE=$curl_output
110
111
if [ $CURL_EXIT_CODE -ne 0 ]; then
112
log_error "Failed to send content to Obsidian API. CURL exit code: $CURL_EXIT_CODE"
113
elif [ "$HTTP_CODE" -ge 400 ]; then
114
log_error "Obsidian API returned error code: $HTTP_CODE"
115
else
116
log_info "Successfully sent content to Obsidian API. Note created: $TITLE.md"
117
fi
118
119
# Clean up temporary files
120
rm "$TEMP_CONTENT_FILE"
121
rm "$FABRIC_OUTPUT"
122
else
123
# Log invalid URL
124
log_info "No valid URL found in the email: $EMAIL_URL or no new email."
125
fi
126
127
# Cleanup Home Directory
128
log_info "Cleaning Home Directory"
129
rm -rf /home/etos/fetched_email_*.txt
130
log_info "Finished"
131
```