homes/common/.local/bin/update-quotes

117 lines
3.6 KiB
Text
Raw Normal View History

2018-10-12 16:17:17 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Create a JSON file containing quotes.
File is under EUPL1.2
Author: kujiu
"""
import json
import argparse
import logging
import pathlib
2018-10-12 16:17:17 +02:00
import sys
import os.path
import wikiquote
logger = logging.getLogger(__name__)
ch = logging.StreamHandler(sys.stderr)
2018-10-12 20:19:23 +02:00
ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
2018-10-12 16:17:17 +02:00
ch.setLevel(logging.WARNING)
logger.addHandler(ch)
def generate_quotes(srcfile):
"""
Generate a dict with all quotes
"""
quotes = {}
with open(srcfile) as fin:
sources_dict = json.load(fin)
for lang in sources_dict:
quotes.setdefault(lang, {})
for category in sources_dict[lang]:
quotes[lang].setdefault(category, {})
for author in sources_dict[lang][category]:
print(f"\n***** Retrieving {author} from {lang}/{category} *****")
2018-10-12 16:17:17 +02:00
try:
quotes[lang][category][author] = wikiquote.quotes(
author, lang=lang
)
except Exception:
logger.exception(
"Error when retrieving %s from %s/%s",
author, lang, category)
continue
if not quotes[lang][category][author]:
logger.warning(
"Source %s from %s/%s has no quote.",
author, lang, category
)
else:
logger.debug(
"Source %s from %s/%s: %d quotes.",
author, lang, category,
len(quotes[lang][category][author])
)
return quotes
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="""
Generate a JSON file containing quotes from wikiquote.\n
\n
Source file must be a JSON in this format:\n
{\n
'en': {'fantasy': ['Discworld', 'Terry Pratchett']},\n
'fr': {'fantasy': ['Kaamelott/Arthur'], 'category2': ['Name']}\n
}\n
""")
parser.add_argument(
'--source', default='~/.config/json-fortune/wikiquote-sources.json',
2018-10-12 16:17:17 +02:00
help='JSON file with sources')
parser.add_argument(
'--dest', default='~/.local/share/json-fortune/fortunes.json',
2018-10-12 16:17:17 +02:00
help='Destination file')
parser.add_argument(
'-d', '--debug', action='store_true', help="Debug mode")
parser.add_argument(
'-v', '--verbose', help="Verbose mode", action='store_true')
options = parser.parse_args()
2018-10-12 20:19:23 +02:00
class VerboseFilter(logging.Filter):
def filter(self, rec):
return rec.levelno in (logging.DEBUG, logging.INFO)
2018-10-12 16:17:17 +02:00
if hasattr(options, 'debug') and options.debug:
ch = logging.StreamHandler(sys.stdout)
2018-10-12 20:19:23 +02:00
ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
2018-10-12 16:17:17 +02:00
ch.setLevel(logging.DEBUG)
2018-10-12 20:19:23 +02:00
ch.addFilter(VerboseFilter())
2018-10-12 16:17:17 +02:00
logger.addHandler(ch)
2018-10-12 20:19:23 +02:00
logger.setLevel(logging.DEBUG)
2018-10-12 16:17:17 +02:00
2018-10-12 20:19:23 +02:00
elif hasattr(options, 'verbose') and options.verbose:
2018-10-12 16:17:17 +02:00
ch = logging.StreamHandler(sys.stdout)
2018-10-12 20:19:23 +02:00
ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
2018-10-12 16:17:17 +02:00
ch.setLevel(logging.INFO)
2018-10-12 20:19:23 +02:00
ch.addFilter(VerboseFilter())
2018-10-12 16:17:17 +02:00
logger.addHandler(ch)
2018-10-12 20:19:23 +02:00
logger.setLevel(logging.INFO)
2018-10-12 16:17:17 +02:00
source = os.path.expanduser(
os.path.expandvars(options.source)
)
dest = os.path.expanduser(
os.path.expandvars(options.dest)
)
pathlib.Path(dest).parent.mkdir(parents=True, exist_ok=True)
2018-10-12 16:17:17 +02:00
with open(dest, 'w') as fout:
json.dump(generate_quotes(source), fout, indent=4)