diff --git a/v8/medium/README.md b/v8/medium/README.md new file mode 100644 index 00000000..6cde2002 --- /dev/null +++ b/v8/medium/README.md @@ -0,0 +1,28 @@ +What is this? +------------- + +This plugin lets syndicate Nikola content to your Medium site. + +KNOWN BUGS +---------- + +Because of Medium's API limitations if you run this twice your posts will be published twice. +So, use with care? + +How to Use it +------------- + +0. Setup a Medium account. +1. Install the plugin using ``nikola plugin -i medium`` +3. Get an integration token from your `Medium Settings Page `__ +4. Save the Client ID and Client Secret in ``medium.json`` like this: + +``` +{ + "TOKEN": "asdasdasdasd", +} +``` + +4. Run ``nikola medium`` + +At that point your posts with the "medium" metadata set to "yes" should be published. Have fun! diff --git a/v8/medium/medium.plugin b/v8/medium/medium.plugin new file mode 100644 index 00000000..c363c101 --- /dev/null +++ b/v8/medium/medium.plugin @@ -0,0 +1,12 @@ +[Core] +Name = medium +Module = medium_plugin + +[Nikola] +PluginCategory = Command + +[Documentation] +Author = Roberto Alsina +Version = 0.1 +Website = http://plugins.getnikola.com/#medium +Description = Publish posts to Medium diff --git a/v8/medium/medium_plugin.py b/v8/medium/medium_plugin.py new file mode 100644 index 00000000..a23e7bb8 --- /dev/null +++ b/v8/medium/medium_plugin.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2017 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. + +from __future__ import print_function, unicode_literals + +import json +import os + +from medium import Client +from nikola import utils +from nikola.plugin_categories import Command + +LOGGER = utils.get_logger('Medium', utils.STDERR_HANDLER) + + +class CommandMedium(Command): + """Publish to Medium.""" + + name = "medium" + needs_config = True + doc_usage = "" + doc_purpose = "publish to Medium" + + def _execute(self, options, args): + """Publish to Medium.""" + if not os.path.exists('medium.json'): + LOGGER.error( + 'Please put your credentials in medium.json as described in the README.') + return False + with open('medium.json') as inf: + creds = json.load(inf) + client = Client() + client.access_token = creds['TOKEN'] + user = client.get_current_user() + self.site.scan_posts() + for post in self.site.timeline: + if post.meta('medium'): + m_post = client.create_post( + user_id=user["id"], + title=post.title(), + content=post.text(), + content_format="html", + publish_status="public", + canonical_url=post.permalink(absolute=True), + tags=post.tags + ) + print('Published %s to %s' % (post.meta('slug'), m_post['url'])) diff --git a/v8/medium/requirements.txt b/v8/medium/requirements.txt new file mode 100644 index 00000000..d343e578 --- /dev/null +++ b/v8/medium/requirements.txt @@ -0,0 +1 @@ +medium