-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
219 lines (186 loc) · 7.59 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import imaplib
import email
from email.header import decode_header
import requests
import os
import base64
import json
from dotenv import load_dotenv
import logging
import mimetypes
import html2text
# Initialize logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
# Load environment variables from .env file
load_dotenv()
# Joplin and email account configurations
joplin_token = os.getenv("JOPLIN_TOKEN")
joplin_port = os.getenv("JOPLIN_PORT", "41184")
joplin_notes_url = f"http://localhost:{joplin_port}/notes?token={joplin_token}"
joplin_resources_url = f"http://localhost:{joplin_port}/resources?token={joplin_token}"
username = os.getenv("EMAIL_USERNAME")
password = os.getenv("EMAIL_PASSWORD")
imap_url = os.getenv("IMAP_URL")
# Folder to receive notes (Buffer) Joplin ID
buffer_folder_id = "386c2f4fa2d441bd96e9596c8bbff7e5"
def is_api_available(url):
try:
response = requests.get(url)
return response.status_code == 200
except requests.RequestException as e:
logging.exception("API check failed:")
return False
def create_resource_in_joplin(file_path):
try:
with open(file_path, "rb") as f:
file_content = f.read()
file_name = os.path.basename(file_path)
mime_type, _ = mimetypes.guess_type(file_path)
if mime_type is None:
logging.warning(
f"Could not guess the MIME type for {file_name}. Defaulting to 'application/octet-stream'."
)
mime_type = "application/octet-stream"
# Create the payload for the POST request
files = {
"data": (file_name, file_content, mime_type),
"props": (
None,
json.dumps({"title": file_name, "filename": file_name}),
"application/json",
),
}
# Make the POST request to the Joplin API to create the resource
response = requests.post(joplin_resources_url, files=files)
logging.info(
f"Joplin API response for creating resource: {response.text}"
) # Log the response text
if response.status_code == 200:
resource_id = response.json().get("id")
if resource_id:
if mime_type.startswith("image/"):
# If the file is an image, return Markdown for displaying the image
return f"![{file_name}](:/{resource_id})"
else:
# If the file is not an image, return Markdown for a downloadable link
return f"[{file_name}](:/{resource_id})"
else:
logging.error(
"Received a 200 response but no resource ID was found in the response."
)
return ""
else:
logging.error(
f"Failed to create resource in Joplin: {response.status_code} {response.text}"
)
return ""
except Exception as e:
logging.exception("Exception occurred while creating resource in Joplin:")
return ""
def save_attachment(part, filename):
attachments_dir = "attachments"
if not os.path.isdir(attachments_dir):
os.mkdir(attachments_dir)
filepath = os.path.join(attachments_dir, filename)
with open(filepath, "wb") as fp:
fp.write(part.get_payload(decode=True))
return filepath
def process_email(msg):
cid_to_resource_link = {}
body = ""
h = html2text.HTML2Text()
h.ignore_links = False
for part in msg.walk():
content_type = part.get_content_type()
content_disposition = str(part.get("Content-Disposition"))
# Handling inline images
if "inline" in content_disposition and part.get("Content-ID"):
content_id = part.get("Content-ID").strip("<>")
filename = part.get_filename()
filepath = save_attachment(part, filename)
resource_link = create_resource_in_joplin(filepath)
if resource_link:
cid_to_resource_link[content_id] = (
resource_link # Store Markdown link directly
)
elif "attachment" in content_disposition:
filename = part.get_filename()
if filename:
filepath = save_attachment(part, filename)
attachment_link = create_resource_in_joplin(filepath)
body += f"\n{attachment_link}" # Append link to body
elif (
content_type in ["text/plain", "text/html"]
and "attachment" not in content_disposition
):
charset = part.get_content_charset()
part_payload = part.get_payload(decode=True)
body_content = (
part_payload.decode(charset)
if charset
else part_payload.decode(errors="replace")
)
if "html" in content_type:
body += h.handle(body_content) # Convert HTML to Markdown
else:
body += body_content # Append plain text directly
# Replace 'cid' references in the email body
for cid, link in cid_to_resource_link.items():
body = body.replace(f"cid:{cid}", link)
return body
def create_note_in_joplin(subject, body, folder_id):
note = {"title": subject, "body": body, "parent_id": folder_id}
try:
response = requests.post(joplin_notes_url, json=note)
if response.status_code != 200:
logging.error("Failed to create note in Joplin: %s", response.text)
except Exception as e:
logging.exception("Error creating note in Joplin: %s", e)
def check_emails():
if is_api_available(joplin_notes_url):
try:
mail = imaplib.IMAP4_SSL(imap_url)
mail.login(username, password)
mail.select("inbox")
status, messages = mail.search(None, "UNSEEN")
if status == "OK":
for num in messages[0].split():
status, data = mail.fetch(num, "(RFC822)")
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
subject = decode_header(msg["subject"])[0][0]
if isinstance(subject, bytes):
subject = subject.decode()
body = process_email(msg)
create_note_in_joplin(subject, body, buffer_folder_id)
mail.logout()
except Exception as e:
logging.exception("Failed to check emails:")
else:
logging.error("API not available, skipping email check")
def find_folder_id(folder_name):
folders_url = f"http://localhost:{joplin_port}/folders?token={joplin_token}"
try:
response = requests.get(folders_url)
if response.status_code == 200:
folders = response.json()
print(f"Folders JSON: {folders}") # Diagnostic print statement
for folder in folders:
if folder["title"] == folder_name:
return folder["id"]
logging.error(f'Folder "{folder_name}" not found.')
return None
else:
logging.error(f"Failed to fetch folders: {response.text}")
return None
except Exception as e:
logging.exception("Error fetching folders: %s", e)
return None
# to find folder id uncomment 2 lines below and comment the if
# buffer_folder_id = find_folder_id('Buffer')
# print(buffer_folder_id)
if __name__ == "__main__":
check_emails()