Merge pull request 'added first images' (#18) from first_image_uploads into main
Reviewed-on: http://192.168.1.2:3000/brian/visual_critical_discourse_analysis/pulls/18
This commit was merged in pull request #18.
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from pymongo.errors import DuplicateKeyError
|
||||||
|
|
||||||
|
from src.database import connect
|
||||||
|
from src.database import VisualCommunication
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# get list of image paths
|
||||||
|
ext_img_dir = Path('/Volumes/BW-PSSD/Mixed Methods/')
|
||||||
|
assert ext_img_dir.exists()
|
||||||
|
img_path_list = [
|
||||||
|
path for path in ext_img_dir.glob(
|
||||||
|
'*.jpg',
|
||||||
|
) if path.is_file()
|
||||||
|
]
|
||||||
|
print(f"found {len(img_path_list)} images")
|
||||||
|
# create visual communication objects
|
||||||
|
vis_com_list = [
|
||||||
|
VisualCommunication.from_file(path)
|
||||||
|
for path
|
||||||
|
in img_path_list
|
||||||
|
]
|
||||||
|
print(f"created {len(vis_com_list)} visual communication objects")
|
||||||
|
# prepare env vars
|
||||||
|
env_path = Path(__file__).parent.parent / 'server.env'
|
||||||
|
assert env_path.exists()
|
||||||
|
load_dotenv(env_path)
|
||||||
|
# connect to database
|
||||||
|
collection, db, client = connect()
|
||||||
|
assert client.server_info() is not None
|
||||||
|
print('connected to database')
|
||||||
|
# upload images
|
||||||
|
for vis_com in vis_com_list:
|
||||||
|
try:
|
||||||
|
result = collection.insert_one(vis_com.model_dump())
|
||||||
|
except DuplicateKeyError as exc:
|
||||||
|
print('ignoring:\n', exc)
|
||||||
|
else:
|
||||||
|
print(f"inserted document: {result}")
|
||||||
Reference in New Issue
Block a user