-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
quote_config.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
import os | ||
import shutil | ||
import importlib | ||
import random | ||
import sys | ||
|
||
import requests | ||
import json | ||
|
||
from flask import render_template_string | ||
|
||
from happyMirror.render import BaseRenderer, RenderResult | ||
|
||
try: | ||
import widget_secrets | ||
import widget_config | ||
except ImportError: | ||
from . import widget_secrets | ||
from . import widget_config | ||
|
||
|
||
class Renderer(BaseRenderer): | ||
|
||
def __init__(self, config=None, secrets_config=None) -> None: | ||
if secrets_config is None: | ||
self.secrets = widget_secrets | ||
else: | ||
self.secrets = secrets_config | ||
def __init__(self, alt_config=None) -> None: | ||
self.config = alt_config | ||
if self.config is None: | ||
self.config = self.__check_config() | ||
|
||
if config is None: | ||
self.config = widget_config | ||
else: | ||
self.config = config | ||
def next(self, category=None): | ||
if len(self.config.api_key) is not 40: | ||
return [{ | ||
"quote": "NO API KEY SET IN CONFIG FILE", | ||
"author": "'Quote' widget", | ||
"category": category, | ||
}] | ||
|
||
def next(self): | ||
category = random.choice(self.config.categories) | ||
url = self.config.api_url | ||
if category is not None: | ||
url = url + '?category={}'.format(category) | ||
|
||
quote = requests.get( | ||
self.config.api_url + category, headers={'X-Api-Key': self.secrets.api_key}, timeout=10 | ||
url, headers={'X-Api-Key': self.config.api_key}, timeout=10 | ||
) | ||
|
||
return json.loads(quote.text) | ||
|
||
def render(self) -> RenderResult: | ||
quote = self.next() | ||
category = None | ||
|
||
if self.config.categories is not None: | ||
category = random.choice(self.config.categories) | ||
|
||
quote = self.next(category) | ||
return { | ||
'view': render_template_string('{{ quote[0]["quote"] }} - {{ quote[0]["author"] }}', quote=quote), | ||
'name': 'quote' | ||
} | ||
|
||
@staticmethod | ||
def __check_config(): | ||
config_file = f"{os.getcwd()}/widgets/quote/quote_config.py" | ||
config_example_file = f"{os.getcwd()}/widgets/quote/quote_config_example.py" | ||
|
||
if os.path.isfile(config_file) is False: | ||
print(f'No config file was found. A new one will be created as "{config_file}"') | ||
print('Open it and insert your api key, then restart the application.') | ||
|
||
shutil.copy(config_example_file, config_file) | ||
|
||
return importlib.import_module('quote_config') | ||
|
||
|
||
|
11 changes: 10 additions & 1 deletion
11
happyMirror/widgets/quote/widget_config.py → ...ror/widgets/quote/quote_config_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.