Skip to content

Instantly share code, notes, and snippets.

@goodevilgenius
Last active October 28, 2023 05:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save goodevilgenius/3878ce0f3e232e3daf5c to your computer and use it in GitHub Desktop.
Save goodevilgenius/3878ce0f3e232e3daf5c to your computer and use it in GitHub Desktop.
[Variety fortune plugin] Plugin for Variety background changer that uses the UNIX fortune program as a quotes source
#!/usr/bin/python
from variety.plugins.IQuoteSource import IQuoteSource
import subprocess, re
from locale import gettext as _
class FortuneSource(IQuoteSource):
@classmethod
def get_info(cls):
return {
"name": "Fortunes",
"description": _("Displays quotes using the UNIX fortune program"),
"author": "Dan Jones",
"version": "0.1"
}
def supports_search(self):
return False
def get_random(self):
fortune = subprocess.check_output(['fortune'])
q = fortune.split('--')
quote = q[0].strip()
if len(q) > 1:
s = q[1].strip()
m = re.search('(.+), +"?(.+)"?',s)
if m:
author = m.group(1)
sourceName = m.group(2)
else:
author = s
sourceName = None
else:
author = None
sourceName = None
r = {
"quote": quote,
"author": author,
"sourceName": sourceName,
"link": None
}
return [r]
def get_for_author(self, author):
return []
def get_for_keyword(self, keyword):
return []
### Read Me
#
# First be sure that fortune is installed and in your PATH
# Copy this file to ~/.config/variety/plugins/quotes
# Restart variety
#
###
#
# Gist: https://gist.github.com/goodevilgenius/3878ce0f3e232e3daf5c
#
### BEGIN LICENSE
# Copyright (c) 2014 Dan Jones
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
### END LICENSE
@youthlin
Copy link

for Chinese users:
We can use the fortune-zh program to get Tang or Song poetry. However its output is colorful with string like \033[32m, so we should remove it:

    quote = quote.replace('\033[32m','')
    quote = quote.replace('\033[33m','')
    quote = quote.replace('\033[m','')

中文用户:
可以使用 fortune-zh 来随机生成唐诗宋词,不过这个输出是带颜色的,终端的颜色使用类似 \033[32m 的字符串来控制的,因此我们可以使用上面的代码段来去除这些额外的字符。

@jimydirektsaft
Copy link

tried this plugin but it unfortunately failes with:

File "/home/xxx/.config/variety/plugins/fortune_variety_quotes.py", line 22, in get_random
q = fortune.split('--')
TypeError: a bytes-like object is required, not 'str'

How can I fix this?

@peterlevi
Copy link

Hey, @goodevilgenius, would you mind if I include this as a built-in quotes plugin in Variety?
Please check varietywalls/variety#263

@goodevilgenius
Copy link
Author

@peterlevi No problem at all. Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment