Memobird咕咕机是一个热敏纸打印机。官网

官方提供了开发的web API接口: 开放平台API文档

通过web API可以做许多奇怪的事了。

不过官方并没有提供示例程序,于是我按照接口说明写了一下Python3的代码实现。

发到这里希望能帮到你。

bind_new(): 绑定新的设备。
print_str(i,s): 对第i个设备打印字符串s。

Code:

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
#! python3

import pycurl,io,json,time,base64


ak = ''

bind = []
# [ [memobirdID,useridentifying,showapi_userid]]

with open('ak.txt','r') as f:
ak = f.readline().strip()

with open('bind.txt','r') as f:
bind = json.loads(f.read())


print('ak:',ak)
print('bind:',bind)

# Basic API

def get_url(url):
print(url)
c = pycurl.Curl()
b = io.BytesIO()
c.setopt(c.URL,url)
c.setopt(c.WRITEDATA, b)
c.perform()
c.close()
s = b.getvalue().decode('utf-8')
a = json.loads(s)
return a

def get_timestamp():
return time.strftime("%Y-%m-%d%%20%H:%M:%S", time.localtime())

def bind_new(memoID):
b_url = 'http://open.memobird.cn/home/setuserbind'
b_url += '?ak=' + ak
b_url += '&timestamp=' + get_timestamp()
b_url += '&memobirdID=' + memoID
userID = len(bind)
b_url += '&useridentifying=' + str(userID)

res = get_url(b_url)

if res['showapi_res_code'] == 1:
bind.append([memoID,userID,res['showapi_userid']])
with open('bind.txt','w') as f:
f.write(json.dumps(bind))
print('Success',bind[userID])
else:
print(res)

def print_str(userID,content):
b_url = 'http://open.memobird.cn/home/printpaper'
b_url += '?ak=' + ak
b_url += '&timestamp=' + get_timestamp()
b_url += '&memobirdID=' + str(bind[userID][0])
b_url += '&userID=' + str(bind[userID][2])
content = base64.b64encode(content.encode('gbk')).decode('utf-8')
b_url += '&printcontent=T:' + content

res = get_url(b_url)

print(res)

# bind_new('233')
# print_str(0,'test')