(水文)编写一个variety一言插件

简介

Variety是一个具有许多功能,适用于Linux的开源自动墙纸更换器。 它使用本地或自动下载的图像定期更改桌面墙纸。 各种版本可以从Flickr,Wallbase.cc,Wallpapers.net,NASA天文学每日图片,Desktoppr.co,Picasa和deviantART的媒体RSS源中获取壁纸。

Arch Linux,Manjaro 用户可以从存储库安装。

sudo pacman -S variety

创建 Hitokoto.py 放到 /usr/lib/python3.9/site-packages/variety/plugins/builtin/quotes/ ~/.config/variety/plugins/,然后重启软件即可。

代码

# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (c) 2021, Xmengnet
# Copyright (c) 2021, Xmengnet <liyp@xmengnet.cn>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
# move this file to /usr/lib/python3.9/site-packages/variety/plugins/builtin/quotes/ or ~/.config/variety/plugins/
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

from locale import gettext as _
import random
import requests

from variety.plugins.IQuoteSource import IQuoteSource


class Hitokoto(IQuoteSource):
    @classmethod
    def get_info(cls):
        return {
            "name": "Hitokoto(一言)",
            "description": _("Displays definitions from Hitokoto"),
            "author": "Xmengnet",
            "version": "0.1",
        }

    def get_random(self):
        url = 'https://v1.hitokoto.cn/?encode=json&'
        cmdlst = {'动画': 'a', '漫画': 'b', '游戏': 'c', '文学': 'd', '原创': 'e', '来自网络': 'f',
                  '其他': 'g', '影视': 'h', '诗词': 'i', '网易云': 'j', '哲学': 'k', '抖机灵': 'l'}
        dict_dict = requests.get(url + random.choice(list(cmdlst.keys()))).json()
        print(dict_dict)

        def _clean(s):
            return s.strip().replace("[", "").replace("]", "")

        word = dict_dict["hitokoto"]
        word_from = dict_dict["from"]
        from_who = dict_dict["from_who"]
        if from_who is None:
            from_who = ''
        result = [{
            "quote": word,
            "author": '《' + word_from + '》' + from_who,
            "sourceName": "Hitokoto(一言)",
            "link": 'https://v1.hitokoto.cn/?encode=json',
        }]

        return result

效果

Variety.png

水完了。。🥊