-
Notifications
You must be signed in to change notification settings - Fork 0
/
postcards_service.py
181 lines (122 loc) · 4.19 KB
/
postcards_service.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
import xmlrpc
import xmlrpc.client
from PIL import Image, ImageDraw
import random_picture
from io import BytesIO
import configparser
import db
import os
import shutil
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.read(f"{SCRIPT_PATH}/config.ini")
HOST_IP = config.get("main", "host")
HOST_PORT = config.get("main", "port")
DB_HOST = config.get("tarantool", "db_host")
DB_PORT = config.get("tarantool", "db_port")
GEN_DELAY = config.get("generation", "delay")
POSTCARDS_PER_NOUN = config.get("generation", "postcards_per_noun")
VANILLA_POSTCARDS_COUNT = config.get("generation", "vanilla_postcards_count")
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ("/RPC2",)
class RPCServer(SimpleXMLRPCServer):
def serve_forever(self):
self.handle_next = True
while self.handle_next:
self.handle_request()
def stop(self):
self.handle_next = False
# Create server
server = RPCServer(
(HOST_IP, int(HOST_PORT)), requestHandler=RequestHandler, allow_none=True
)
server.register_introspection_functions()
db.connect(DB_HOST, DB_PORT)
db_nouns_ids_dict = db.start_init(
random_picture.nouns, int(POSTCARDS_PER_NOUN), int(VANILLA_POSTCARDS_COUNT)
)
db_ids_nouns_dict = {}
for key, value in db_nouns_ids_dict.items():
db_ids_nouns_dict[value] = key
# fiesta_bot
def get_random_postcard(user_id):
db_result = db.get_random_postcard(user_id)
image_file = open(
os.path.join(SCRIPT_PATH, "postcards", f"{db_result[0]}/{db_result[1]}_2.jpg"),
"rb",
)
file_data = image_file.read()
image_file.close()
return xmlrpc.client.Binary(file_data)
server.register_function(get_random_postcard)
# fiesta_bot
def get_random_noun_postcard(user_id, noun_id):
db_result = db.get_random_noun_postcard(user_id, noun_id)
image_file = open(
os.path.join(SCRIPT_PATH, "postcards", f"{noun_id}/{db_result[0]}_1.jpg"), "rb"
)
file_data = image_file.read()
image_file.close()
return xmlrpc.client.Binary(file_data)
server.register_function(get_random_noun_postcard)
def get_nouns_ids_dict():
global db_nouns_ids_dict
return db_nouns_ids_dict
server.register_function(get_nouns_ids_dict)
def get_actual():
result = db.get_generate_priority().data
return result
server.register_function(get_actual)
def write_image(data_array, noun_id, postcard_index):
counter = 1
for image_data in data_array:
image_file = open(
os.path.join(
SCRIPT_PATH,
"postcards",
str(noun_id),
f"{postcard_index}_{counter}.jpg",
),
"wb",
)
image_file.write(image_data.data)
image_file.close()
counter += 1
db.postcard_showings_dec(noun_id, postcard_index)
server.register_function(write_image)
# ------------------------Vanila postcards--------------------------------
def get_actual_vanilla():
result = db.get_vanilla_generate_priority().data
return result
server.register_function(get_actual_vanilla)
def get_random_vanilla_postcard(user_id, watermark_index):
db_result = db.get_random_vanilla_postcard(user_id)
image_path = os.path.join(
SCRIPT_PATH, "vanilla_postcards", f"{db_result[0]}_{watermark_index}.jpg"
)
image_file = open(image_path, "rb")
file_data = image_file.read()
image_file.close()
return xmlrpc.client.Binary(file_data)
server.register_function(get_random_vanilla_postcard)
def vanilla_write_image(data_array, index):
counter = 1
for image_data in data_array:
image_file = open(
os.path.join(SCRIPT_PATH, "vanilla_postcards", f"{index}_{counter}.jpg"),
"wb",
)
image_file.write(image_data.data)
image_file.close()
counter += 1
db.vanilla_postcard_showings_dec(index)
server.register_function(vanilla_write_image)
def stop_service():
print("exit")
server.stop()
server.register_function(stop_service)
# Run the server's main loop
server.serve_forever()