用python自动推送文章到kindle

需求说明

公众号文章,还有付费订阅的一些专栏课程只能在手机或者电脑上阅读, 对于一直喜欢用kindle来阅读的我来说有些痛苦,就想着有没有办法每天自动把当天要阅读的新文章自动推送到kindle上,这样能极大提升自己的阅读体验。 作为程序员,作为python程序员,一定是要想办法自动化的,在网上搜了一圈,发现这个新发布的python自动化开发工具clicknium不错, 就开始动手尝试.

环境准备

本人开发环境:windows 10, visual studio code

  • 参见 getting started

  • 根据扩展的提示说明,安装Clicknium Python module

本人使用chrome浏览器,所以需要安装clicknium chrome extension。

创建项目

直接新建一个folder

方案思路

这里以知识星球举例

  • 进入一个专栏

  • 然后通过python 自动化去抓取文章列表以及时间信息,只抓取当天的新文章

  • 然后打开每个文章,利用浏览器打印功能,选择“Microsoft Print PDF”打印机,将文章保存到到一个指定目录下

  • 通过发送邮件的方式将pdf文件发送给kindle,这里需要使用python的两个库:email(用来构造邮件)和smtplib(用来发送邮件)

购买的 Kindle 每个设备都会绑定一个邮箱作为收件地址,格式类似于:xxxx_xx@kindle.cn。可以在 Kindle 中 全部设置->我的账户 的最下面查看该设备的邮件地址,也可以进行配置,这个可以自行查找kindle的相关文档说明.

首先录制专栏文章的‘...’ 和时间,都是div

编辑切换为居中

添加图片注释,不超过 140 字(可选)

然后编辑选择器,加上参数{{index}}

然后代码里循环遍历每个文章,根据时间来判断是否是今天的新文章

i = 1
while True:
    v = {"index": i}
    if cc.is_exist(locator.WebPageToKindle.zsxq.div_index, v, 3):
        date = cc.find_element(locator.WebPageToKindle.zsxq.div_date, v).get_text()
        if '昨天' in date:
            break
        cc.find_element(locator.WebPageToKindle.zsxq.div_index, v).click(by="mouse-emulation")
        sleep(1)
        cc.find_element(locator.WebPageToKindle.zsxq.div_复制链接, v).click()
        sleep(2)
        url = pyperclip.paste()
        pdffile = save_pdf(url, datetime.now().strftime('%Y%m%d%H%M%S'))
        send_email(pdffile)
        i = i+1
    else:
        break

说明:循环遍历,如果存在,并且时间是今天的文章,则点击复制链接

然后使用lib `pyperclip ` 来获取剪切板内容,再save_pdf,如下

 
def save_pdf(url,path):
        file_name = str(path) + '.pdf'
        if os.path.exists(file_name):
            pass
        else:
            print(file_name)
        file_path = os.path.join(os.getcwd(),file_name)
        driver = cc.chrome.open(url)
        ui(locator.WebPageToKindle.zsxq.span_详情).send_hotkey('{CTRL}{p}')
        sleep(3)
        cc.send_hotkey('{ENTER}')
        sleep(3)
        cc.wait_appear(locator.WebPageToKindle.zsxq.saveas_edit)
        ui(locator.WebPageToKindle.zsxq.saveas_edit).set_text(file_path, InputTextBy='set-text')
        ui(locator.WebPageToKindle.zsxq.saveas_saveBtn).click()
        sleep(3)
        driver.close()
        return file_path

说明:会打开复制的链接,然后在打开的页面上发送快捷键Ctrl+p, 弹出打印窗口,这里提前设置好打印机为

然后发送快捷键‘Enter’,等待另存为窗口弹出,

输入保存路径,点击‘保存’按钮

保存完pdf,接着发送邮件,会自动推送到kindle

通过以上的方式,可以做到自动化推送订阅的新文章到kindle上了,推送其他订阅公众号文章,课程等都可以是类似方式。

附:完整代码, 录制的locator可以自行使用clicknium提供的录制器来录制,操作教程可以看clicknium vscode的说明,非常详细,赞一个。

 
from datetime import datetime
from time import sleep
from clicknium import clicknium as cc, locator, ui
import os
import pyperclip
import os.path
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders

_user = "*@hotmail.com"
_pwd  = ''
_to   = "*@kindle.cn"

def send_email(pdf_file):
    # set up the SMTP server
    s = smtplib.SMTP(host='smtp.office365.com', port=587)
    s.starttls()
    s.login(_user, _pwd)
    print('connection good')

    msg = MIMEMultipart()
    msg['Subject'] = 'convert'  #邮件标题
    msg['From'] = _user #显示发件人
    msg['To'] = _to #接收邮箱
    attfile = pdf_file
    basename = os.path.basename(pdf_file) 
    print(basename)
    fp = open(attfile,'rb')
    att = MIMEText(fp.read(),'base64','gbk')
    att['Content-Type'] = 'application/octer-stream'
    att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', basename))
    encoders.encode_base64(att)
    msg.attach(att)
    
    s = smtplib.SMTP("smtp.office365.com",587)
    s.ehlo() # Hostname to send for this command defaults to the fully qualified domain name of the local host.
    s.starttls() #Puts connection to SMTP server in TLS mode
    s.ehlo()
    s.login(_user, _pwd)#登陆服务器

    s.sendmail(_user, _to, msg.as_string())#发送邮件
    s.close()

def save_pdf(url,path):
        file_name = str(path) + '.pdf'
        if os.path.exists(file_name):
            pass
        else:
            print(file_name)
        file_path = os.path.join(os.getcwd(),file_name)
        driver = cc.chrome.open(url)
        ui(locator.WebPageToKindle.zsxq.span_详情).send_hotkey('{CTRL}{p}')
        sleep(3)
        cc.send_hotkey('{ENTER}')
        sleep(3)
        cc.wait_appear(locator.WebPageToKindle.zsxq.saveas_edit)
        ui(locator.WebPageToKindle.zsxq.saveas_edit).set_text(file_path, InputTextBy='set-text')
        ui(locator.WebPageToKindle.zsxq.saveas_saveBtn).click()
        sleep(3)
        driver.close()
        return file_path
      
i = 1
while True:
    v = {"index": i}
    if cc.is_exist(locator.WebPageToKindle.zsxq.div_index, v, 3):
        date = cc.find_element(locator.WebPageToKindle.zsxq.div_date, v).get_text()
        if '昨天' in date:
            break
        cc.find_element(locator.WebPageToKindle.zsxq.div_index, v).click(by="mouse-emulation")
        sleep(1)
        cc.find_element(locator.WebPageToKindle.zsxq.div_复制链接, v).click()
        sleep(2)
        url = pyperclip.paste()
        pdffile = save_pdf(url, datetime.now().strftime('%Y%m%d%H%M%S'))
        send_email(pdffile)
        i = i+1
    else:
        break

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值