-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
143 lines (115 loc) · 4.4 KB
/
main.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
import os
import pika
import json
import requests
from wand.image import Image
from dotenv import load_dotenv
load_dotenv()
HOST = os.getenv("HOST")
SENDER_QUEUE_NAME = os.getenv("SENDER_QUEUE_NAME")
CONSUMER_QUEUE_NAME = os.getenv("CONSUMER_QUEUE_NAME")
print(HOST, SENDER_QUEUE_NAME, CONSUMER_QUEUE_NAME)
mimeTypesExtension = {
"image/png": "png",
"image/jpeg": "jpg",
"image/gif": "gif",
"application/pdf": "pdf"
}
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST))
rmq_channel = connection.channel()
session = requests.Session()
rmq_channel.exchange_declare(exchange=SENDER_QUEUE_NAME, exchange_type='topic', durable=True)
def download_image(session: requests.Session, url: str, path: str) -> bool:
if os.path.exists(path):
os.remove(path)
r = session.get(url, stream=True)
if r.status_code == 200:
with open(path, "wb") as f:
f.write(r.content)
return True
return False
def upload_image(session: requests.Session, url: str, path: str, t=0) -> bool:
with open(path, 'rb') as upload:
r = session.put(url, data=upload, headers={"Content-Type": "image/png"}, stream=True)
if r.status_code == 200:
return True
else:
print(r.status_code)
print(url, r.request.headers)
if t < 5:
upload_image(session, url, path, t + 1)
return False
def send(message: str) -> None:
print(message)
rmq_channel.basic_publish(exchange=SENDER_QUEUE_NAME, routing_key=SENDER_QUEUE_NAME, body=message)
def on_message(channel, method_frame, header_frame, body) -> None:
print(body)
rmq_channel.basic_ack(method_frame.delivery_tag)
req = json.loads(body)
upload_id = req["sourceUploadID"]
upload_urls = req["uploadURLs"]
file_infos = req["fileInfos"]
os.makedirs(f"tmp/{upload_id}", exist_ok=True)
files = []
counter = 0
for idx, info in enumerate(file_infos):
url = info["url"]
mime_type = info["fileInfo"]["contentType"]
file = f"tmp/{upload_id}/{idx}.{mimeTypesExtension[mime_type]}"
if not download_image(session, url, file):
print(f'Failed to download {file}')
return None
conv_path = f'tmp/conv/{file}'
os.makedirs(conv_path, exist_ok=True)
if mime_type == "application/pdf" or mime_type == "image/gif":
ny = Image(filename=file, resolution=200)
ny_converted = ny.convert('png')
for page_number, img in enumerate(ny_converted.sequence):
page = Image(image=img)
page.format = 'png'
save_path = f'{conv_path}/{page_number}.png'
page.save(filename=save_path)
page.destroy()
print(f'Uploading {save_path}...')
if upload_image(session, upload_urls[counter]["url"], save_path):
files.append(upload_urls[counter]["fileName"])
print(f'Uploaded {save_path}')
else:
print(f'Something went wrong with uploading {save_path}')
counter += 1
os.remove(save_path)
ny_converted.destroy()
ny.destroy()
else:
ny = Image(filename=file)
save_path = f'{conv_path}/0.png'
ny.save(filename=save_path)
ny.destroy()
print(f'Uploading {save_path}...')
if upload_image(session, upload_urls[counter]["url"], save_path):
files.append(upload_urls[counter]["fileName"])
print(f'Uploaded {save_path}')
else:
print(f'Something went wrong with uploading {save_path}')
counter += 1
os.remove(save_path)
os.remove(file)
os.removedirs(conv_path)
res = {
"sourceUploadID": upload_id,
"data": {
"files": files
}
}
if counter == len(files):
send(json.dumps(res, separators=(',', ':'), ensure_ascii=False))
result = rmq_channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
rmq_channel.queue_bind(exchange=CONSUMER_QUEUE_NAME, queue=queue_name, routing_key=CONSUMER_QUEUE_NAME)
rmq_channel.basic_consume(
queue=queue_name, on_message_callback=on_message, auto_ack=False)
try:
rmq_channel.start_consuming()
except KeyboardInterrupt:
rmq_channel.close()
session.close()