4

Alfred workflow查询历史上的今天 | CHEGVA

 2 years ago
source link: https://chegva.com/3675.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Mac高效办公第六弹——Alfred workflow查询历史上的今天

2020年1月31日 by anzhihe·2条评论 · 1,907 人阅读 · 隐藏边栏 · 最后更新: 2020/3/22

◎软件环境:

◎功能描述:

对于喜欢历史的同学来说,生活中的每一天都在创造历史,而对于人类历史长河来说,个人也只是沧海一粟,历史让人短暂有限的人生延长了。通过查看历史上的今天,回顾过去,吸取前人的教训,争取美好的未来,每天了解一下过去这一天发生了什么,日积月累,不仅知识增长了,也可以了解历史的进程,给生活增加点乐趣。

◎实现思路:

通过requests去调用历史上的今天网站的查询接口,bs4去抓取返回页面元素获取每一天的历史事件,回车直接跳转到内容页面,按shift键可以预览网页内容,输入ls默认取当天的历史纪录,ls 日期(x/x格式)可以查看指定日期的历史事件。第一次加载由于要下载图片比较慢,之后就好了。

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import sys, os
import requests
from bs4 import BeautifulSoup
from workflow import Workflow3

reload(sys)
sys.setdefaultencoding('utf-8')

def save_img(url):
    """下载图片"""
    file_name = url.split('/')[-1]
    try:
        r = requests.get(url)
    except:
        print "远程连接错误"
    try:
        filepath = "picture/" + file_name
        with open(filepath, 'wb') as f:
            f.write(r.content)
    except:
        print "文件保存错误"
        
        
def main(wf):
    """输入日期"""
    base_url = "https://www.lssdjt.com"
    if not os.path.exists("picture"):
         os.makedirs("picture")
    if not wf.args:
        url = base_url
    else:
        try:
            today = str(wf.args[0])
            month = today.split('/')[0]
            day = today.split('/')[1]
            url = "https://www.lssdjt.com/{}/{}/".format(month, day)
        except:
           print "输入参数错误"
        
    resp = requests.request("GET", url)
    resp.raise_for_status()
    soup = BeautifulSoup(resp.content, "html.parser")
    today_in_history = soup.find_all("li", attrs={"class": "gong"})
    """加载资源"""
    try:
        threads = []
        for li in today_in_history:
            h_url = base_url + li.a.attrs['href']
            if li.a.has_attr('rel'):
                pic_url = li.a.attrs['rel'][0]
                pic_name = pic_url.split('/')[-1]
                h_jpg = "picture/" + pic_name
                if not os.path.exists(h_jpg):
                    t = threading.Thread(target=save_img,args={pic_url})
                    threads.append(t)
            else:
                h_jpg = 'default.png'
            h_date = li.a.em.get_text("", strip=True)
            h_title = li.a.i.get_text("", strip=True)
            wf.add_item(title=h_title, subtitle=h_date, arg=h_url, valid=True, icon=h_jpg)
        for i in threads:
            i.start()
        for i in threads:
            i.join()
    except:
         print "资源加载失败"
         
    wf.send_feedback()
    
if __name__ == '__main__':
    wf = Workflow3()
    sys.exit(wf.run(main))
Python

◎效果展示:

◎下载地址:https://github.com/anzhihe/Efficient-office/tree/master/today-in-history

anzhihe安志合个人博客,版权所有丨 如未注明,均为原创 丨转载请注明转自:https://chegva.com/3675.html | ☆★★每天进步一点点,加油!★★☆

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK