20250128.1738075081
This commit is contained in:
parent
5a0ffd895a
commit
64532eb1b6
1 changed files with 84 additions and 9 deletions
|
@ -21,6 +21,8 @@ from pathlib import Path
|
|||
DB_NAME = Path("metadata.db")
|
||||
TZ = 1
|
||||
TOOLS = ("jrnl", "sqlite3")
|
||||
FILES = (".jpeg", ".jpg", ".png", ".gif")
|
||||
FILES_PATH = Path("attachments")
|
||||
|
||||
for t in TOOLS:
|
||||
if not shutil.which(t):
|
||||
|
@ -101,6 +103,29 @@ def convert_diary_date(date_str):
|
|||
return None
|
||||
|
||||
|
||||
def download_file(url, filename, diary_path, headers):
|
||||
ext = os.path.splitext(url)[-1]
|
||||
filename = f"{int(time.time())}_{filename}"
|
||||
if not Path(diary_path / FILES_PATH).exists():
|
||||
Path(diary_path / FILES_PATH).mkdir(parents=True, exist_ok=True)
|
||||
filepath = Path(diary_path / FILES_PATH / filename)
|
||||
if ext not in FILES:
|
||||
print(f"File {url} is not supporting")
|
||||
if filepath.exists():
|
||||
print(f"File {url} exists")
|
||||
try:
|
||||
request = urllib.request.Request(url, headers=headers)
|
||||
with (
|
||||
urllib.request.urlopen(request) as response,
|
||||
open(filepath, "wb") as out_file,
|
||||
):
|
||||
out_file.write(response.read())
|
||||
except Exception as e:
|
||||
sys.exit(str(e))
|
||||
|
||||
return Path(FILES_PATH / filename)
|
||||
|
||||
|
||||
def fetch_data(url, headers={}, data=None, rjson=True, log=True):
|
||||
logit = make_logger()
|
||||
method = "POST" if data else "GET"
|
||||
|
@ -166,6 +191,12 @@ def initialize_db(conn: sqlite3.Connection):
|
|||
metadata_id INTEGER NOT NULL,
|
||||
FOREIGN KEY (metadata_id) REFERENCES metadata (id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
filepath TEXT NOT NULL,
|
||||
metadata_id INTEGER NOT NULL,
|
||||
FOREIGN KEY (metadata_id) REFERENCES metadata (id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS location (
|
||||
id INTEGER PRIMARY KEY,
|
||||
city TEXT NOT NULL,
|
||||
|
@ -222,6 +253,27 @@ def insert_weather(weather: dict, conn: sqlite3.Connection, metadata_id: int):
|
|||
conn.commit()
|
||||
|
||||
|
||||
def insert_attach(filepath: str, conn: sqlite3.Connection, metadata_id: int):
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO attachments(filepath, metadata_id)
|
||||
VALUES(?, ?)
|
||||
""",
|
||||
[
|
||||
filepath,
|
||||
metadata_id,
|
||||
],
|
||||
)
|
||||
except Exception as e:
|
||||
remove_metadata(conn, metadata_id)
|
||||
conn.rollback()
|
||||
print(e)
|
||||
raise
|
||||
conn.commit()
|
||||
|
||||
|
||||
def insert_location(location: dict, conn: sqlite3.Connection, metadata_id: int):
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
|
@ -320,13 +372,19 @@ def doctor():
|
|||
location = cursor.execute(
|
||||
"SELECT * FROM location WHERE metadata_id = ?", (m[0],)
|
||||
).fetchall()
|
||||
attachment = cursor.execute(
|
||||
"SELECT * FROM attachments WHERE metadata_id = ?", (m[0],)
|
||||
).fetchall()
|
||||
|
||||
if not weather:
|
||||
print(f"There is no weather info about {m[0]} - {m[1]}")
|
||||
if not location:
|
||||
print(f"There is no location info about {m[0]} - {m[1]}")
|
||||
# I can't have attachments in every entity...
|
||||
# if not attachment:
|
||||
# print(f"There is no attachment info about {m[0]} - {m[1]}")
|
||||
|
||||
if not weather and not location:
|
||||
if not weather and not location and not attachment:
|
||||
# delete metadata entry if any of metadata type is not exists
|
||||
print("An empty metadata was deleted")
|
||||
remove_metadata(conn, m[0])
|
||||
|
@ -409,18 +467,35 @@ def export():
|
|||
sys.exit("Export canceled.")
|
||||
|
||||
for memo in memos:
|
||||
# attachments part
|
||||
# memo_info = fetch_data(f"{Config.memo_url}/api/v1/{memo['name']}", headers)
|
||||
# # check if there are resources
|
||||
# if "resources" in memo_info:
|
||||
# for resource in memo_info["resources"]:
|
||||
# memo_resources = fetch_data(
|
||||
# f"{Config.memo_url}/api/v1/resources:by-uid/{resource['uid']}"
|
||||
# )
|
||||
create_time = memo["createTime"]
|
||||
content = shlex.quote(memo["content"].replace(f"#{tag}", "").strip())
|
||||
|
||||
metadata_id = insert_metadata(conn, make_tz_unixtime(create_time))
|
||||
|
||||
# attachments part
|
||||
memo_info = fetch_data(f"{Config.memo_url}/api/v1/{memo['name']}", headers)
|
||||
# check if there are resources
|
||||
if "resources" in memo_info:
|
||||
for resource in memo_info["resources"]:
|
||||
# download files
|
||||
url = f"{Config.memo_url}file/{resource['name']}/{resource['filename']}"
|
||||
try:
|
||||
filepath = download_file(
|
||||
url=url,
|
||||
diary_path=diary_path,
|
||||
filename=resource["filename"],
|
||||
headers=headers,
|
||||
)
|
||||
except Exception:
|
||||
remove_metadata(conn, metadata_id)
|
||||
raise
|
||||
|
||||
insert_attach(
|
||||
filepath=str(filepath),
|
||||
conn=conn,
|
||||
metadata_id=metadata_id,
|
||||
)
|
||||
|
||||
closest_entry = fetch_geo(metadata_id, make_tz_unixtime(create_time), conn)
|
||||
fetch_weather(
|
||||
metadata_id, closest_entry, make_tz_unixtime(create_time), conn
|
||||
|
|
Loading…
Add table
Reference in a new issue