Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous import #231

Merged
merged 5 commits into from May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions v7/continuous-import/README.md
@@ -0,0 +1,31 @@
The continuous import plugin lets Nikola work as a hub for the different
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an underscore in the plugin directory name would be better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, changing

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?shelf=read',
'template': 'goodreads.tmpl',
'output_folder': 'posts/goodreads',
'format': 'html',
'lang': 'en',
'tags': 'books, goodreads',
'metadata': {
'title': 'title',
'date': ['user_read_at', 'user_date_added', 'published'],
}
}
}

```
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

102 changes: 102 additions & 0 deletions v7/continuous-import/continuous_import.py
@@ -0,0 +1,102 @@
# -*- 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 io
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 get_data(self, item, prop):
if isinstance(prop, (list, tuple)):
# Try each prop until one works
for p in prop:
if p in item and item[p]:
return item[p]
return ''
else:
return item.get(prop, '')

def generate(self, item, feed):
compiler = self.site.compilers[feed['format']]
title = self.get_data(item, feed['metadata']['title'])
output_name = os.path.join(feed['output_folder'],
slugify(title, feed['lang'])) + compiler.extension()
content = self.site.render_template(
feed['template'],
None,
dict(
item=item,
feed=feed,
lang=feed['lang'],
))

metadata = {}
for k, v in feed['metadata'].items():
metadata[k] = self.get_data(item, v)

if 'tags' not in metadata:
metadata['tags'] = feed['tags']

compiler.create_post(
path=output_name,
content=content,
onefile=True,
is_page=False,
**metadata
)
1 change: 1 addition & 0 deletions v7/continuous-import/requirements.txt
@@ -0,0 +1 @@
feedparser
16 changes: 16 additions & 0 deletions v7/continuous-import/templates/mako/goodreads.tmpl
@@ -0,0 +1,16 @@
<div>
<table>
<tr>
<td>
<img class='img-thumbnail' src="${item['book_medium_image_url']}">
</td>
<td valign="top">
<p><b>Author:</b> ${item['author_name']}</p>
<p><b>Rating:</b>
%for i in range(int(item['user_rating'])):
<span class="glyphicon glyphicon-star"></span>
% endfor
</table>

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