whyliam / whyliam.workflows.youdao

使用有道翻译你想知道的单词和语句
http://blog.naaln.com/2015/05/repeat-create-the-wheel-alfred-plugin-youdao/
MIT License
1.09k stars 132 forks source link

查询截取逻辑有问题 #67

Closed kevin-lucifer closed 5 years ago

kevin-lucifer commented 5 years ago

翻译下面中文: 在保存失败的时候单词将保存在离线单词本中

报错: UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 37: invalid continuation byte

这段生截query造成的,为什么要这样做?

def truncate(q): if q is None: return None size = len(q) return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]

kevin-lucifer commented 5 years ago

我直接改成return q正常了,是加了什么功能要在这里做截断吗?

whyliam commented 5 years ago

https://ai.youdao.com/docs/doc-trans-api.s#p04

其中,input的计算方式为:input=q前10个字符 + q长度 + q后十个字符(当q长度大于20)或 input=q字符串(当q长度小于等于20)。

kevin-lucifer commented 5 years ago

改成这样会报202签名错误,python不是太熟,你有空帮看看 @liszd

return q if size <= 20 else q.decode('utf8')[0:10].encode('utf8') + str(size) + q.decode('utf8')[size - 10:size].encode('utf8')

但生截10会报utf8encode错误.

ifyour commented 5 years ago

Thank you for developing this software. It seems that this problem still exists?

Environmenta

Error screenshot

image

rookiezq commented 5 years ago

Thank you for developing this software. It seems that this problem still exists?

Environmenta

  • Alfred: v4.0.1
  • Youdao Workflow: v2.2.2

Error screenshot

image

我也遇到了这个问题,好像是只要句子一长就出现,请问该怎么解决呢

whyliam commented 5 years ago

@kevin-lucifer @ifyour @rookiezq 可以帮忙测一下这个包吗?

whyliam.workflows.youdao.zip

ifyour commented 5 years ago

whyliam.workflows.youdao.zip

@whyliam 同样的问题,长句子出现错误: image

whyliam commented 5 years ago

@ifyour 好的,谢谢。这个包主要是记录一些错误信息,方便我调试。我晚上在统一处理一下。

whyliam commented 5 years ago

@kevin-lucifer @ifyour @rookiezq 找到问题了,已经上传新版本了

kevin-lucifer commented 5 years ago

@kevin-lucifer @ifyour @rookiezq 找到问题了,已经上传新版本了

感谢修复,顺便请教个问题,为什么要str()两次查询串才能正常?

query = str(query)
curtime = str(int(time.time()))
salt = str(uuid.uuid1())
signStr = zhiyun_id + truncate(query) + salt + curtime + zhiyun_key
sign = encrypt(signStr)

url = 'https://openapi.youdao.com/api' + \
    '?appKey=' + str(zhiyun_id) + \
    '&salt=' + str(salt) + \
    '&sign=' + str(sign) + \
    '&q=' + urllib.quote(query) + \
    '&from=auto' + \
    '&to=auto' + \
    '&signType=v3' + \
    '&curtime=' + curtime
return url
whyliam commented 5 years ago

这里还是只有一个str 其实主要是unicode编码问题

kevin-lucifer commented 5 years ago

这里还是只有一个str 其实主要是unicode编码问题

懂了,刚看漏了签名生成那截. 不过我还是不理解有道这样改api图啥,不同平台很容易出问题.