-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_reader.py
112 lines (71 loc) · 2.35 KB
/
file_reader.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
import websocket
import json
def on_message(ws, message):
a = '''
mutation {
saveChatMessage(input: {
type: "normal"'''
b = '''
}) {
success
}
}'''
msg = json.loads(message)
if "data" in msg and "chat" in msg["data"]:
msgtype = msg["data"]["chat"]["type"]
msgtext = msg["data"]["chat"]["text"]
msgrole = msg["data"]["chat"]["role"]
if not msgtext == None:
print("Bot Message: " + msgtext)
if msgtype == "normal" and msgrole == "rep":
global count
count = count + 1
if count == 1:
f2.write(msgtext + "\n")
if count == 2:
if len(temp) == 0:
print("Done reading file")
ws.close()
line = temp.pop(0)
payload = a + "\npid: " + '"' + _pid + '"' + "\ntext: " + '"' + line + '"' + b
ws.send(payload)
count = 0
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("OPENED")
a = '''subscription {
chat(input: {
participationId: '''
b = '''
}) {
type
role
text
}
}'''
subscribe = a + '"' + _pid + '"' + b
ws.send(subscribe)
print("SUBSCRIBED")
def connect(wsurl, pid):
global f
global f2
global count
count = 0
filename = input("Enter input file's name': ")
with open(filename, encoding="utf-8") as f:
global temp
temp = f.read().splitlines()
filename2 = input("Enter output file's name': ")
f2 = open(filename2, "a", encoding="utf-8")
global _pid
_pid = pid
websocket.enableTrace(True)
ws = websocket.WebSocketApp(wsurl,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()