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

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

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

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

发到这里希望能帮到你。

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

Code:

#! 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')