Blame

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