homes/graphic/.config/variety/plugins/json-fortune_variety_quotes.py

77 lines
1.9 KiB
Python
Raw Normal View History

2018-10-12 16:17:17 +02:00
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
"""
Get a fortune from a JSON file.
File is under EUPL1.2
Author: kujiu
"""
import os
import json
import random
from locale import gettext as _
from variety.plugins.IQuoteSource import IQuoteSource
SRCFILE = "~/.fortunes.json"
class JsonFortuneSource(IQuoteSource):
""" Get quote from JSON fortune file """
quotes = {}
@classmethod
def get_info(cls):
return {
"name": "JSON Fortunes",
"description": _("Displays quotes using a JSON file"),
"author": "kujiu",
"version": "0.1"
}
def supports_search(self):
return False
def load_srcfile(self):
"""
Load quote from file
"""
if self.quotes:
return
srcfile = os.path.expanduser(
os.path.expandvars(SRCFILE)
)
with open(srcfile) as fin:
self.quotes = json.load(fin)
def get_random(self):
self.load_srcfile()
quote = ""
count = 0
while not quote and count < 50:
count += 1
lang = random.choice([lang for lang in self.quotes.keys()])
category = random.choice([cat for cat in self.quotes[lang].keys()])
if not self.quotes[lang][category]:
continue
author = random.choice(
[author for author in self.quotes[lang][category].keys()])
if not self.quotes[lang][category][author]:
continue
quote = random.choice(self.quotes[lang][category][author])
return [{
"quote": quote,
"author": author,
"sourceName": "WikiQuote",
"link": "https://%s.wikiquote.org/wiki/%s" % (lang, author)
}]
def get_for_author(self, author):
return []
def get_for_keyword(self, keyword):
return []