-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpnb.py
86 lines (75 loc) · 2.88 KB
/
jpnb.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
import re
import json
import uuid
import bot
import asyncio
import requests
import datetime
import traceback
from websocket import create_connection
import json
with open('api.json', 'r') as f:
data = json.load(f)
cookies = data['jpnb']
base = data['jpnburl']
notebook = data['jpnbnotebook']
def send_execute_request(code):
msg_type = 'execute_request';
content = { 'code' : code, 'silent':False }
hdr = { 'msg_id' : uuid.uuid1().hex,
'username': 'test',
'session': uuid.uuid1().hex,
'data': datetime.datetime.now().isoformat(),
'msg_type': msg_type,
'version' : '5.0' }
msg = { 'header': hdr, 'parent_header': hdr,
'metadata': {},
'content': content }
return msg
async def get_jpnb_res(context, code):
base = 'https://'+base
notebook_path = notebook
url = base + '/api/sessions'
cookies = {'Cookie': api}
r = requests.get(url, cookies=cookies)
session = json.loads(r.text)[0]
kernel = session["kernel"]
cookies = cookies
ws = create_connection("wss://"+base"/api/kernels/"+kernel["id"]+"/channels?session_id"+session["id"], cookie=cookies)
code = re.split(r'[;]+', code)
for c in code:
ws.send(json.dumps(send_execute_request(c.strip())))
for i in range(0, len(code)):
try:
msg_type = ''
while True:
rsp = json.loads(ws.recv())
msg_type = rsp["msg_type"]
if msg_type == "stream":
print(code[i])
print(rsp["content"]["text"])
await bot.bot.send(context, rsp["content"]["text"].strip())
elif msg_type == "execute_result":
if "image/png" in (rsp["content"]["data"].keys()):
print(code[i])
print(rsp["content"]["data"]["image/png"])
await bot.bot.send(context, '图片现在还发送不了')
else:
print(code[i])
print(rsp["content"]["data"]["text/plain"])
await bot.bot.send(context, rsp["content"]["data"]["text/plain"].strip())
elif msg_type == "display_data":
print(code[i])
print(rsp["content"]["data"]["image/png"])
await bot.bot.send(context, '图片现在还发送不了')
elif msg_type == "error":
print(code[i])
errorrsp = [re.sub(r'\[[01]+(;\d{2})*m', '', i) for i in rsp["content"]["traceback"]]
print(errorrsp)
await bot.bot.send(context, '\n'.join(errorrsp).strip())
elif msg_type == "status" and rsp["content"]["execution_state"] == "idle":
break
except:
traceback.print_exc()
ws.close()
ws.close()