-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnevisa.py
executable file
·115 lines (104 loc) · 3.55 KB
/
nevisa.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 8 16:36:27 2021
@author: mahsa
"""
import asyncio
import json
import re
import requests as req
import websockets
async def send_file_nevisa(file,ip,size,name,comment):
headers = {"subject": "SOF",
"file-size": size,
"file-name": name,
"source-ip": ip,
"settings": {"autostart": "true", "comment": comment}
}
endmsg = {
"subject": "EOF"
}
streamBlockSize = 43967
try:
async with websockets.connect('ws://80.210.37.8:8087/queueFile') as ws:
try:
await ws.send(json.dumps(headers))
except:
print("Error in sending SOF message")
try:
re = await ws.recv()
re = json.loads(re)
if re['result'] == 0:
try:
chunks = [file[i:i + streamBlockSize] for i in range(0, len(file), streamBlockSize)]
for chunk in chunks:
await ws.send(chunk)
await ws.send(json.dumps(endmsg))
re = await ws.recv()
re = json.loads(re)
if re['result'] == 0:
await ws.close()
return 1
else:
print("unsuccessfully converted file")
return 0
except:
print("Error in sending File")
return 0
else:
print("Error in connecting to Nevisa,Result is not 0")
return 0
except:
print("Error in Receiving Response of SOF")
return 0
except:
print("Error in connecting to Nevisa")
return 0
def send_req_nevisa(type,uid):
if type == "GetList":
try:
r = req.get("http://80.210.37.8:8087/FileAsrServlet?subject="+type,auth=req.auth.HTTPBasicAuth('ali', '123'))
except:
print("Request Error")
return 0
else:
try:
r = req.get("http://80.210.37.8:8087/FileAsrServlet?subject=" + type + "&uid=" + uid,auth=req.auth.HTTPBasicAuth('ali', '123'))
except:
print("Request Error")
return 0
return json.loads(r.text)
def get_uid(JFile,name,comment,ip):
file_list = JFile['list']
for i in file_list.keys():
if file_list[i]['comment'] == comment and file_list[i]['fname'] == name and file_list[i]['source-ip'] == ip :
return i
def remove_signs(text):
while text.find(")") != -1:
text = re.sub(r'\(([^()]*)\)','',text)
return text
def nevisa(file,comment,ip="172.0.0.0"):
size = str(file.__sizeof__())
file_1 = file.read()
name=file.name
asyncio.set_event_loop(asyncio.new_event_loop())
asyncio.get_event_loop().run_until_complete(send_file_nevisa(file_1,ip,size,name,comment))
asyncio.get_event_loop().run_until_complete(send_file_nevisa(file_1,ip,size,name,comment))
JFile = send_req_nevisa("GetList","")
uid = get_uid(JFile,name,comment,ip)
try:
text = send_req_nevisa("GetText",uid)['text']
text = remove_signs(text)
send_req_nevisa("DeleteFile", uid)
return text
except Exception as e:
print(e)
return 0
return 0
"""
START
"""
# file=open("{path/to/file/file.wav}",mode='rb')
# comment="{your national code}"
# text = nevisa(file,comment)