This commit is contained in:
Nathan Rashleigh committed 2024-04-08 10:46:14 +10:00
1 parent d82a7fdf1e
commit 335eb8c4ac
16 files changed
+370 -5930

No files matched your search

+36 -61
View File
@@ -19,11 +19,11 @@ from dotenv import dotenv_values, load_dotenv
from google.cloud import vision as google_vision
from google.protobuf.json_format import MessageToDict, MessageToJson
from google.cloud.vision_v1 import types
from minio import Minio
import meilisearch
from urllib3.util import Url, url
import uploader
from sqlalchemy import create_engine
from db.migrate import migrate_db
# need to change the env for the google api calls
load_dotenv()
@@ -55,42 +55,16 @@ class MemeDocument:
text: str
tags: List[str]
@dataclass
class DiscordAttachmentMessage:
id: UUID
msg_id: int
url: str
upload_url: str
guild_name: str
channel_name: str
vision_json: str | None
message_json: Dict[str, Any]
guild_json: Dict[str, Any]
channel_json: Dict[str, Any]
def detect_text(url, vision):
request = {
'image': { 'source': { 'image_uri': url } },
'features': [types.Feature(type=types.Feature.Type.TEXT_DETECTION)],
}
def document_dir(self):
return f'{DATA_DIR}/guilds/{self.guild_name}/{self.channel_name}'
response: types.AnnotateImageResponse = vision.annotate_image(request=request)
vision_json = json.loads(types.AnnotateImageResponse.to_json(response))
def file_dir(self):
return f"{DATA_DIR}/imgs/"
def detect_text(self, vision):
request = {
'image': { 'source': { 'image_uri': self.url } },
'features': [types.Feature(type=types.Feature.Type.TEXT_DETECTION)],
}
response: types.AnnotateImageResponse = vision.annotate_image(request=request)
self.vision_json = json.loads(types.AnnotateImageResponse.to_json(response))
if len(response.text_annotations) > 0:
self.text = response.text_annotations[0].description
return request
def persist(self):
with open(f'{self.document_dir()}/{self.id}.json', 'w') as f:
json.dump(vars(self), f, indent=4)
return request
def filter_message_img_urls(messages):
url_lists = map(
@@ -203,38 +177,39 @@ async def check_bucket_exists(storage, bucket_name):
raise # Reraise other exceptions
async def main():
assert config['MINIO_URL']
assert config['STORAGE_URL']
assert config['POSTGRES_URL']
vision = google_vision.ImageAnnotatorClient()
# storage = Minio(config['MINIO_URL'],
# access_key=config['MINIO_ACCESS_KEY'],
# secret_key=config['MINIO_SECRET_KEY'],
# )
# db setup
db_url = config['POSTGRES_URL']
db_engine = create_engine(db_url)
migrate_db(db_engine, db_url)
aioboto = aiobotocore_get_session()
async with aioboto.create_client(
's3',
endpoint_url=config['STORAGE_URL'],
aws_access_key_id=config['STORAGE_ACCESS_KEY'],
aws_secret_access_key=config['STORAGE_SECRET_KEY'],
) as storage:
storage: S3Client
# aioboto = aiobotocore_get_session()
# async with aioboto.create_client(
# 's3',
# endpoint_url=config['STORAGE_URL'],
# aws_access_key_id=config['STORAGE_ACCESS_KEY'],
# aws_secret_access_key=config['STORAGE_SECRET_KEY'],
# ) as storage:
# storage: S3Client
storage_bucket = config['UPLOAD_BUCKET']
assert storage_bucket
# storage_bucket = config['UPLOAD_BUCKET']
# assert storage_bucket
# Make the bucket if it doesn't exist.
exists = await check_bucket_exists(storage, storage_bucket)
if not exists:
await storage.create_bucket(Bucket=storage_bucket)
print("Created bucket", storage_bucket)
else:
print("Bucket", storage_bucket, "already exists")
# # Make the bucket if it doesn't exist.
# exists = await check_bucket_exists(storage, storage_bucket)
# if not exists:
# await storage.create_bucket(Bucket=storage_bucket)
# print("Created bucket", storage_bucket)
# else:
# print("Bucket", storage_bucket, "already exists")
async with aiohttp.ClientSession() as session:
channels = [download_channel_attachments(session, storage, vision, channel_id) for channel_id in CHANNEL_IDS]
memes = await asyncio.gather(*channels)
# async with aiohttp.ClientSession() as session:
# channels = [download_channel_attachments(session, storage, vision, channel_id) for channel_id in CHANNEL_IDS]
# memes = await asyncio.gather(*channels)
asyncio.run(main())