Generate RSS for comments
This commit is contained in:
parent
6237b30e6f
commit
37095bbe79
5 changed files with 45 additions and 8 deletions
|
@ -15,6 +15,8 @@ from app.models.report import Report
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import config
|
import config
|
||||||
|
import PyRSS2Gen
|
||||||
|
import markdown
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
queue = Queue()
|
queue = Queue()
|
||||||
|
@ -146,9 +148,11 @@ def reply_comment_email(data):
|
||||||
# update Comment row
|
# update Comment row
|
||||||
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
comment.save()
|
comment.save()
|
||||||
|
|
||||||
logger.info('commit comment: %d' % comment_id)
|
logger.info('commit comment: %d' % comment_id)
|
||||||
|
|
||||||
|
# rebuild RSS
|
||||||
|
rss(token)
|
||||||
|
|
||||||
# send approval confirmation email to admin
|
# send approval confirmation email to admin
|
||||||
email_body = get_template('approve_comment').render(original=message)
|
email_body = get_template('approve_comment').render(original=message)
|
||||||
mail(from_email, 'Re: ' + subject, email_body)
|
mail(from_email, 'Re: ' + subject, email_body)
|
||||||
|
@ -334,6 +338,31 @@ def report(token):
|
||||||
Report.delete().execute()
|
Report.delete().execute()
|
||||||
|
|
||||||
|
|
||||||
|
def rss(token):
|
||||||
|
site = Site.select().where(Site.token == token).get()
|
||||||
|
rss_title = get_template('rss_title_message').render(site=site.name)
|
||||||
|
md = markdown.Markdown()
|
||||||
|
|
||||||
|
items = []
|
||||||
|
for row in Comment.select().join(Site).where(
|
||||||
|
Site.token == token, Comment.published).order_by(-Comment.published).limit(10):
|
||||||
|
items.append(PyRSS2Gen.RSSItem(
|
||||||
|
title='%s - http://%s%s' % (row.author_name, site.url, row.url),
|
||||||
|
link="http://%s%s" % (site.url, row.url),
|
||||||
|
description=md.convert(row.content),
|
||||||
|
guid=PyRSS2Gen.Guid('%s%d' % (token, row.id)),
|
||||||
|
pubDate=row.published
|
||||||
|
))
|
||||||
|
|
||||||
|
rss = PyRSS2Gen.RSS2(
|
||||||
|
title=rss_title,
|
||||||
|
link="http://" + site.url,
|
||||||
|
description="Commentaires du site '%s'" % site.name,
|
||||||
|
lastBuildDate=datetime.now(),
|
||||||
|
items=items)
|
||||||
|
rss.write_xml(open(config.RSS_FILE, "w"), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
def mail(to_email, subject, message):
|
def mail(to_email, subject, message):
|
||||||
|
|
||||||
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
headers = {'Content-Type': 'application/json; charset=utf-8'}
|
||||||
|
@ -368,6 +397,10 @@ def start(template_dir):
|
||||||
logger.info("load templates from directory %s" % template_dir)
|
logger.info("load templates from directory %s" % template_dir)
|
||||||
env = Environment(loader=FileSystemLoader(template_dir))
|
env = Environment(loader=FileSystemLoader(template_dir))
|
||||||
|
|
||||||
|
# generate RSS for all sites
|
||||||
|
for site in Site.select():
|
||||||
|
rss(site.token)
|
||||||
|
|
||||||
# start processor thread
|
# start processor thread
|
||||||
proc = Processor()
|
proc = Processor()
|
||||||
proc.start()
|
proc.start()
|
||||||
|
|
1
app/templates/en/rss_title_message.tpl
Normal file
1
app/templates/en/rss_title_message.tpl
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Comments : {{ site }}
|
1
app/templates/fr/rss_title_message.tpl
Normal file
1
app/templates/fr/rss_title_message.tpl
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Commentaires du site : {{ site }}
|
|
@ -18,3 +18,4 @@ SECRET = "Uqca5Kc8xuU6THz9"
|
||||||
|
|
||||||
ROOT_URL = 'http://localhost:8000'
|
ROOT_URL = 'http://localhost:8000'
|
||||||
|
|
||||||
|
RSS_FILE = 'comments.xml'
|
|
@ -3,6 +3,7 @@ Flask==0.10.1
|
||||||
Flask-Cors==2.0.1
|
Flask-Cors==2.0.1
|
||||||
itsdangerous==0.24
|
itsdangerous==0.24
|
||||||
Jinja2==2.7.3
|
Jinja2==2.7.3
|
||||||
|
Markdown==2.6.2
|
||||||
MarkupSafe==0.23
|
MarkupSafe==0.23
|
||||||
peewee==2.6.0
|
peewee==2.6.0
|
||||||
PyMySQL==0.6.6
|
PyMySQL==0.6.6
|
||||||
|
|
Loading…
Add table
Reference in a new issue