-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathocr_functions.py
299 lines (276 loc) · 10.1 KB
/
ocr_functions.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import subprocess
import telegram
import os
import logging
from glob import glob
path = os.path.abspath("")
path_ocr = path + "/webScraper/automation/ocr"
path_automation = path + "/webScraper/automation"
def send_log_to_user(bot, chat_id, logname):
with open(logname) as f:
log_output = f.read()
if len(log_output) > 4095:
bot.send_document(chat_id=chat_id, document=open(logname, "rb"))
else:
bot.send_message(chat_id=chat_id, text=log_output)
def ocr1(bot, chat_id, photo, state_name, dist_name, is_translation_req=False):
ocr_log_file = open("/tmp/ocr.log", "w+")
file_id = photo.file_id
newFile = bot.get_file(file_id)
newFile.download("/tmp/file.jpg")
bot.send_chat_action(
chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO,
)
# ./ocr.sh /tmp/file.jpg Bihar Araria True
subprocess.call(
[
"bash",
"ocr.sh",
"/tmp/file.jpg",
state_name,
dist_name,
str(is_translation_req),
],
cwd=path_ocr,
stdout=ocr_log_file,
stderr=ocr_log_file,
)
try:
bot.send_photo(
chat_id=chat_id, photo=open(path_ocr + "/image.png", "rb"),
)
except:
pass
try:
with open(path_ocr + "/output.txt") as f:
content = f.read()
if len(content) > 4095:
bot.send_document(chat_id=chat_id, document=open("/tmp/ocr.log", "rb"))
else:
bot.send_message(
chat_id=chat_id,
text=content,
# reply_markup=reply_markup,
)
os.remove(path_ocr + "/output.txt")
os.remove(path_ocr + "/image.png")
except:
logging.info("File do not exist")
bot.send_message(
chat_id=chat_id,
text="Picked the wrong state? :/",
# reply_markup=reply_markup,
)
ocr_log_file.close()
send_log_to_user(bot, chat_id, logname="/tmp/ocr.log")
os.remove("/tmp/file.jpg")
def ocr2(bot, chat_id, text, state_name):
with open("/tmp/ocr.log", "w+") as ocr_log_file:
output1 = text
try:
with open(path_ocr + "/output.txt", "w+") as f:
f.write(output1)
except Exception as e:
logging.error(e)
pass
# ./ocr.sh ../../../b2.jpg Rajasthan AJMER False ocr,table
try:
subprocess.run(
["bash", "ocr.sh", "", state_name, "", "", "ocr,table"],
cwd=path_ocr,
stdout=ocr_log_file,
stderr=ocr_log_file,
timeout=20,
)
except subprocess.TimeoutExpired:
e = "Request timed out"
logging.error(e)
bot.send_message(chat_id=chat_id, text=e)
return
try:
with open(path_automation + "/output2.txt") as f:
output2 = f.read()
if len(output2) > 4095:
bot.send_document(
chat_id=chat_id,
document=open(path_automation + "/output2.txt", "rb"),
)
else:
bot.send_message(chat_id=chat_id, text=output2)
os.remove(path_automation + "/output2.txt")
except:
pass
send_log_to_user(bot, chat_id, logname="/tmp/ocr.log")
def pdf(bot, chat_id, state_name, url, page_num):
"""
Run the pdf automation when pdf links are passed
"""
pdf_log_file = "/tmp/pdf_output.txt"
pdf_err_file = "/tmp/pdf_err.txt"
# python3 automation.py Haryana full pdf=url
with open(pdf_log_file, "w") as log_file:
with open(pdf_err_file, "w") as err_file:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
logging.info(f"pdf={url}={page_num}")
p = subprocess.Popen(
[
"python3",
"automation.py",
state_name,
"full",
f"pdf={url}={page_num}",
],
cwd=path_automation,
stdout=log_file,
stderr=err_file,
stdin=subprocess.PIPE,
encoding="utf8",
)
p.communicate(input=str(page_num))
with open(pdf_log_file, "rb") as log_file:
with open(pdf_err_file, "rb") as err_file:
out = log_file.read()
err = err_file.read()
try:
# Send the errata
if err is not None:
if len(err) > 4095:
bot.send_document(chat_id=chat_id, document=err_file)
else:
bot.send_message(chat_id=chat_id, text=err.decode("utf-8"))
os.remove(pdf_err_file)
except Exception as e:
logging.error(e)
pass
try:
# Send the results
if out is not None:
if len(out) > 4095:
log_file.seek(0)
bot.send_document(chat_id=chat_id, document=log_file)
else:
bot.send_message(chat_id=chat_id, text=out.decode("utf-8"))
os.remove(pdf_log_file)
except Exception as e:
logging.error(e)
pass
def dashboard(bot, chat_id, state_name):
"""
Run the pdf automation when pdf links are passed
"""
dash_log_file = "/tmp/dash_output.txt"
dash_err_file = "/tmp/dash_err.txt"
# python3 automation.py Tripura full
logging.info(f"Dashboard fetch for {state_name}")
try:
with open(dash_log_file, "w") as log_file:
with open(dash_err_file, "w") as err_file:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
p = subprocess.run(
["python3", "automation.py", state_name, "full"],
cwd=path_automation,
stdout=log_file,
stderr=err_file,
encoding="utf8",
timeout=20,
)
except subprocess.TimeoutExpired:
e = "Request timed out"
logging.error(e)
bot.send_message(chat_id=chat_id, text=e)
return
with open(dash_log_file, "rb") as log_file:
with open(dash_err_file, "rb") as err_file:
out = log_file.read()
err = err_file.read()
try:
# Send the errata
if err is not None:
if len(err) > 4095:
bot.send_document(chat_id=chat_id, document=err_file)
else:
bot.send_message(chat_id=chat_id, text=err.decode("utf-8"))
os.remove(dash_err_file)
except Exception as e:
logging.error(e)
pass
try:
# Send the results
if out is not None:
if len(out) > 4095:
log_file.seek(0)
bot.send_document(chat_id=chat_id, document=log_file)
else:
bot.send_message(chat_id=chat_id, text=out.decode("utf-8"))
os.remove(dash_log_file)
except Exception as e:
logging.error(e)
pass
def ka_detail(bot, chat_id, pdf_file, category, start_page, end_page):
"""
Run KA automation
"""
# Save TG file
pdf_file.download(path_automation + '/.tmp/KA.pdf')
pdf_log_file = "/tmp/pdf_output.txt"
pdf_err_file = "/tmp/pdf_err.txt"
# python3 kaautomation.py deceased 80 85
with open(pdf_log_file, "w") as log_file:
with open(pdf_err_file, "w") as err_file:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
logging.info(f"KA Detail : {category} {start_page} {end_page}")
p = subprocess.Popen(
[
"python3",
"kaautomation.py",
category[0].lower(), # To handle even if the user inputs full word
start_page,
end_page
],
cwd=path_automation,
stdout=log_file,
stderr=err_file,
stdin=subprocess.PIPE,
encoding="utf8",
)
p.communicate()
with open(pdf_log_file, "rb") as log_file:
with open(pdf_err_file, "rb") as err_file:
out = log_file.read()
err = err_file.read()
try:
# Send the errata
if err is not None:
if len(err) > 4095:
bot.send_document(chat_id=chat_id, document=err_file)
else:
bot.send_message(chat_id=chat_id, text=err.decode("utf-8"))
os.remove(pdf_err_file)
except Exception as e:
logging.error(e)
pass
try:
# Send the results
if out is not None:
if len(out) > 4095:
log_file.seek(0)
bot.send_document(chat_id=chat_id, document=log_file)
else:
bot.send_message(chat_id=chat_id, text=out.decode("utf-8"))
os.remove(pdf_log_file)
except Exception as e:
logging.error(e)
pass
try:
# Send the output file
with open(path_automation + "/kaconfirmed.csv",'rb') as f:
bot.send_document(chat_id=chat_id, document=f)
# Remove the output to avoid resending in future
os.remove(path_automation + "/kaconfirmed.csv")
for f in glob(path_automation + "/.tmp/*.csv"):
os.remove(f)
os.remove(path_automation + "/.tmp/KA.pdf")
except Exception as e:
logging.error(e)
bot.send_message(chat_id=chat_id, text="Something went wrong while sending output")
return