-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
137 lines (121 loc) · 3.99 KB
/
utils.py
File metadata and controls
137 lines (121 loc) · 3.99 KB
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
from minio import Minio
from minio.error import S3Error
import requests
import uuid
import os
S3_URL = os.getenv("S3_URL")
S3_USERNAME = os.getenv("S3_USERNAME")
S3_PASSWORD = os.getenv("S3_PASSWORD")
S3_BUCKET = os.getenv("S3_BUCKET")
minio_client = Minio(
S3_URL,
access_key=S3_USERNAME,
secret_key=S3_PASSWORD,
secure=True
)
def generate_html(posts):
# Only for testing : Generates HTML page with the currently generated posts and stores them as 'index.html' file
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Social Media Posts</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 20px;
}
.post {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 8px;
margin-bottom: 20px;
padding: 15px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.title {
font-size: 24px;
color: #333;
font-weight: bold;
}
.caption {
font-size: 16px;
color: #666;
margin-top: 10px;
}
img {
width: 100%;
max-width: 600px;
height: auto;
display: block;
margin: 0 auto 10px;
border-radius: 4px;
}
</style>
</head>
<body>
"""
# Loop through each post using the provided iteration style
for key, val in posts.items():
html_content += f"""
<div class="post">
<div class="title">{key}</div>
<div class="caption">{val['content']}</div>
<img src="{val['image_url']}" alt="Image for {key}">
</div>
"""
html_content += """
</body>
</html>
"""
with open('index.html', 'w', encoding='utf-8') as file:
file.write(html_content)
def upload_to_minio(image_url,
bucket=S3_BUCKET,
):
try:
object_name = str(uuid.uuid4())
# Get image from Dalle generated URL
response = requests.get(image_url, stream=True)
response.raise_for_status()
if not minio_client.bucket_exists(bucket):
minio_client.make_bucket(bucket)
# Upload the file
minio_client.put_object(
bucket,
object_name,
data=response.raw,
length=int(response.headers.get('content-length')),
content_type=response.headers.get('content-type')
)
print(f"Image is successfully uploaded with id='{object_name}' to bucket '{bucket}'.")
return object_name
except S3Error as e:
print(f"File upload failed: {e}")
return '' # Returns an empty string in case the upload was unsuccessful
def download_image_from_url(url):
try:
# Download the image from url
response = requests.get(url, stream=True)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx and 5xx)
return response.raw
except requests.RequestException as e:
print(f"Error downloading the image: {e}")
except Exception as e:
print(e)
def list_objects_in_bucket(bucket_name=S3_BUCKET):
"""
List all objects in a MinIO bucket
:param bucket_name: Name of the bucket to list objects from
"""
try:
# List objects in the bucket
objects = minio_client.list_objects(bucket_name, recursive=True)
for obj in objects:
print(f"Object: {obj.object_name} - Size: {obj.size} bytes")
except S3Error as e:
print(f"Error occurred: {e}")