Skip to content

Commit

Permalink
introducing templates
Browse files Browse the repository at this point in the history
  • Loading branch information
git2samus committed Feb 29, 2020
1 parent 4e37b40 commit 0bb1520
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
praw.ini
# Mapping of keywords to response images
keyword_mapping.json
# Message templates
templates/*.md

# Created by https://www.gitignore.io/api/python,virtualenv
# Edit at https://www.gitignore.io/?templates=python,virtualenv
Expand Down
28 changes: 19 additions & 9 deletions keyword_mapping.json.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"<keyword-1>": [
"<img-url1>",
"<img-url2>"
],
"<keyword-2>": [
"<img-url1>",
"<img-url2>",
"<img-url3>"
]
"<keyword>": {
"award_singular": "<singular-award-name>",
"award_plural": "<singular-award-name>",
"template": "<award-tpl>",
"images": [
"<img-url>",
"<img-url>",
"<img-url>"
],
},
"<keyword>": {
"award_singular": "<plural-award-name>",
"award_plural": "<plural-award-name>",
"template": "<award-tpl>",
"images": [
"<img-url>",
"<img-url>"
],
}
}
52 changes: 41 additions & 11 deletions modules/awards.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re, json, random, string
from jinja2 import Environment, FileSystemLoader
from modules.shared.base import APIProcess
from modules.shared.utils import normalize_str

Expand All @@ -11,6 +12,12 @@ def __init__(self, source_version, subreddit_name):
# internal variables
self.subreddit_name = subreddit_name

# setup templates
self.template_env = Environment(
loader=FileSystemLoader('templates'),
autoescape=False
)

# prepare search patterns and keyword info
negate_char_config = self.reddit.config.custom['bot_negate_char']
negate_char = re.escape(negate_char_config)
Expand All @@ -37,7 +44,39 @@ def __init__(self, source_version, subreddit_name):
self.keyword_re = re.compile(keyword_pattern)

def add_reply(self, comment, matched_keywords):
print(f'@@@ {matched_keywords}')
reply = []

for keyword in sorted(matched_keywords):
award_details = self.keyword_mapping[keyword]

template_name = award_details['template']
template = self.template_env.get_template(template_name)

template_args = self.reddit.config.custom.copy()
template_args.update({
'sender': comment.author.name,
'recipient': 'Samus?',
'award_singular': award_details['award_singular'],
'award_plural': award_details['award_plural'],
'times': 2,
'image_url': random.choice(award_details['images']),
})

reply.append(template.render(**template_args))

print('\n\n---\n\n'.join(reply))

def process_comment(self, comment):
tokens = normalize_str(comment.body).split()

matches = map(self.keyword_re.fullmatch, tokens)
matched_keywords = {
match['keyword'] for match in matches
if match is not None and match['negated'] is None
}

if matched_keywords:
self.add_reply(comment, matched_keywords)

def run(self):
"""Start process"""
Expand All @@ -54,13 +93,4 @@ def run(self):
for comment in subreddit.stream.comments(skip_existing=True):
# Use author.name to avoid another API call
if normalize_str(comment.author.name) not in blacklist:
tokens = normalize_str(comment.body).split()

matches = map(self.keyword_re.fullmatch, tokens)
matched_keywords = {
match['keyword'] for match in matches
if match is not None and match['negated'] is None
}

if matched_keywords:
self.add_reply(comment, matched_keywords)
self.process_comment(comment)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Jinja2==2.11.1
Unidecode==1.1.1
docopt==0.6.2
praw==6.4.0
Expand Down
3 changes: 3 additions & 0 deletions templates/award.md.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Here's your {{award_singular}}]({{image_url}}), /u/{{recipient}}!

^({{recipient}} has received {% if times == 1 %}{{award_singular}} {{times}} time{% else %}{{award_plural}} {{times}} times{% endif %} this week! Given by {{sender}}. [info]({{bot_info}}))

0 comments on commit 0bb1520

Please sign in to comment.