Skip to content

Commit

Permalink
continuous import plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed May 27, 2017
1 parent eb8ef27 commit dd3b440
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
30 changes: 30 additions & 0 deletions v7/continuous-import/README.md
@@ -0,0 +1,30 @@
The continuous import plugin lets Nikola work as a hub for the different
ways in which you put content out on the Internet. For example, you may
post book reviews in GoodReads, or movie reviews at a different site,
or have a Tumblr, or post links at Reddit.

Using continuous import you can have that content presented also in your own
site, making it the authoritative source for all things you write, and giving
you a backup in case those sites ever disappear.

TODO: explain how it works

```
# This is a list of feeds whose contents will be imported as posts into
# your blog via the "nikola continuous_import" command.
FEEDS = {
'goodreads': {
'url': 'https://www.goodreads.com/review/list_rss/1512608?key=N4VCVq54T-sxk8wx4UMRYIZKMVqQpbZN4gdYG22R4dv04LM2&shelf=read',
'template': 'goodreads.tmpl',
'output_folder': 'posts/goodreads',
'format': 'html',
'lang': 'en',
'metadata': {
'title': 'title',
'date': 'published',
'tags': 'books, goodreads',
}
}
}
```
13 changes: 13 additions & 0 deletions v7/continuous-import/continuous_import.plugin
@@ -0,0 +1,13 @@
[Core]
name = continuous_import
module = continuous_import

[Documentation]
author = Roberto Alsina
version = 0.1
website = https://getnikola.com/
description = Seamlessly merge other feeds into your blog

[Nikola]
plugincategory = Command

79 changes: 79 additions & 0 deletions v7/continuous-import/continuous_import.py
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-

# Copyright © 2012-2016 Roberto Alsina and others.

# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Create a new site."""

from __future__ import print_function, unicode_literals
import os

import feedparser

from nikola.plugin_categories import Command
from nikola.utils import get_logger, STDERR_HANDLER, slugify, LocaleBorg, makedirs


LOGGER = get_logger('init', STDERR_HANDLER)


class CommandContinuousImport(Command):
"""Import and merge feeds into your blog."""

name = "continuous_import"

doc_usage = ""
needs_config = True
doc_purpose = "Import and merge feeds into your blog."
cmd_options = []

def _execute(self, options={}, args=None):
"""Import and merge feeds into your blog."""
for name, feed in self.site.config['FEEDS'].items():
LOGGER.info('Processing {}', name)
items = self.fetch(feed)['entries']
for item in items:
self.generate(item, feed)

def fetch(self, feed):
url = feed['url']
parsed = feedparser.parse(url)
return parsed

def generate(self, item, feed):
compiler = self.site.compilers[feed['format']]
title = item[feed['metadata']['title']]
output_name = os.path.join(feed['output_folder'],
slugify(title, feed['lang'])) + compiler.extension()
post = self.site.render_template(
feed['template'],
None,
dict(
item=item,
feed=feed,
lang=feed['lang'],
))
makedirs(os.path.dirname(output_name))
with open(output_name, "w+") as post_file:
post_file.write(post)
12 changes: 12 additions & 0 deletions v7/continuous-import/templates/mako/goodreads.tmpl
@@ -0,0 +1,12 @@
<!--
%for k,v in feed['metadata'].items():
.. ${k}: ${item.get(v, v)}
%endfor
-->

<div>
<img class='img-thumbnail pull-left' src="${item['book_medium_image_url']}">
<p>Author: ${item['author_name']}</p>

<p>${item['user_review']}</p>
</div>

0 comments on commit dd3b440

Please sign in to comment.