骚操作,妈妈再也不用担心我看英文文献了 - 3.翻译接口+打包执行

接前两篇文章:
骚操作,妈妈再也不用担心我看英文文献了 - 1.功能介绍
骚操作,妈妈再也不用担心我看英文文献了 - 2.逻辑+代码

翻译比较简单,有现成的接口,可以看 百度翻译开放平台,注册一个账号,拿到appid和key,代码如下:
(我把账号密码隐藏了)

# -*- coding: utf-8 -*-
#python3
import http.client
import hashlib
import urllib
import random
import json

def _get_password(path="E:\\Program_Files\\英译中朗读女\\password.txt"):
    with open(path) as f:
        lines = f.readlines()
    appid = lines[0].strip()
    secretKey = lines[1].strip()
    return appid,secretKey

def translateToChinese(q):
    appid, secretKey = _get_password()

    httpClient = None
    myurl = '/api/trans/vip/translate'

    fromLang = 'auto'  # 原文语种
    toLang = 'zh'  # 译文语种
    salt = random.randint(32768, 65536)
    # q= "What's the simplest way of detecting keyboard input in python from the terminal?"
    sign = appid + q + str(salt) + secretKey
    sign = hashlib.md5(sign.encode()).hexdigest()
    myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(
        q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
        salt) + '&sign=' + sign

    try:
        httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
        httpClient.request('GET', myurl)

        # response是HTTPResponse对象
        response = httpClient.getresponse()
        result_all = response.read().decode("utf-8")
        result = json.loads(result_all)

        out = result['trans_result'][0]['dst']
        return out

    except Exception as e:
        print(e)
    finally:
        if httpClient:
            httpClient.close()

最后,再把它打包弄成一个bat文件,不然还得打开ide太麻烦。

@echo off
REM 声明采用UTF-8编码
chcp 65001

echo note
echo 选中中文,按下f1键直接朗读。
echo note
echo 选中英文,按下f2键先翻译成中文,再进行朗读。

python E:\Program_Files\英译中朗读女\auto_read_by_chinese.py
@cmd.exe

酱紫,就完成了。是不是很简单,但很有用呀。你想想,看着英文,按一个键,就可以朗读对应的中文了,这还不爽吗?

看英文系列【全】:
骚操作,妈妈再也不用担心我看英文文献了 - 1.功能介绍
骚操作,妈妈再也不用担心我看英文文献了 - 2.逻辑+代码
骚操作,妈妈再也不用担心我看英文文献了 - 3.翻译接口+打包执行
源码连接:
https://github.com/JizhiXiang/bestEngToChiSpeaker
【感觉有意思,点个赞呗,算对我分享东西的肯定,thx】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Enter the Swift future of iOS and OS X programming Beginning Swift Programming is your ideal starting point for creating Mac, iPhone, and iPad apps using Apple’s new Swift programming language. Written by an experienced Apple developer and trainer, this comprehensive guide explains everything you need to know to jumpstart the creation of your app idea. Coverage includes data types, strings and characters, operators and functions, arrays and dictionaries, control flow, and looping, with expert guidance on classes, objects, class inheritance, closures, protocols, and generics. This succinct — yet complete — overview provides a detailed introduction to the core features of Swift. Apple developed Swift to address the limitations of Objective-C, and add features found in more complex languages like Python. The results is simpler, cleaner, more expressive code with automatic memory management, functional programming patterns, and more, including built-in features that make Swift apps faster, scalable, and more secure. This book explains it all, helping developers master Apple’s new language. Become fluent with syntax that’s easier to read and maintain Understand inferred types for cleaner, less mistake-prone code Learn the key features that make Swift more expressive than Objective-C Learn the new optional types in Swift that make your code more resilient Understand the key design patterns in iOS and Mac OS programming using protocols and delegates Learn how to use generics to create highly reusable code Learn the new access controls mechanism in Swift Get up to speed quickly to remain relevant and ahead of the curve.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值