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

Add Boolean functionality to Medium and Devto plugin #405

Merged
merged 5 commits into from
Apr 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v8/devto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ How to Use it
4. Run ``nikola devto``

At that point your posts with the "devto" metadata set to "yes" or "true" should be published.
Any article that does not have "devtp" metadata, or is set to "false" will be skipped.
This plugin is testing if your article was already published so don't worry for double posting.

Enjoy!
27 changes: 15 additions & 12 deletions v8/devto/devto_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from nikola import utils
from nikola.plugin_categories import Command

LOGGER = utils.get_logger('Devto')
LOGGER = utils.get_logger("Devto")


class CommandDevto(Command):
Expand All @@ -50,21 +50,24 @@ class CommandDevto(Command):
def _execute(self, options, args):
"""Publish to Dev.to."""

if not os.path.exists('devto.json'):
LOGGER.error(
'Please put your credentials in devto.json as described in the README.')
if not os.path.exists("devto.json"):
LOGGER.error("Please put your credentials in devto.json as described in the README.")
return False
with open('devto.json') as inf:
with open("devto.json") as inf:
creds = json.load(inf)
api = pydevto.PyDevTo(api_key=creds['TOKEN'])
api = pydevto.PyDevTo(api_key=creds["TOKEN"])

articles = api.articles()
self.site.scan_posts()

posts = self.site.timeline

devto_titles = {item["title"] for item in articles}
to_post = [post for post in posts if post.title() not in devto_titles and post.meta('devto')]
to_post = [
post
for post in posts
if post.title() not in devto_titles and post.meta("devto") and (post.meta("devto").lower() not in ["no", "false", "0"])
]

if len(to_post) == 0:
LOGGER.info("Nothing new to post...")
Expand All @@ -73,16 +76,16 @@ def _execute(self, options, args):
with open(post.source_path, 'r', encoding='utf-8') as file:
data = file.read()

if post.source_ext() == '.md':
if post.source_ext() == ".md":
content = "".join(data)
elif post.source_ext() == '.rst':
content = pypandoc.convert_file(post.source_path, to='gfm', format='rst')
elif post.source_ext() == ".rst":
content = pypandoc.convert_file(post.source_path, to="gfm", format="rst")

m_post = api.create_article(
title=post.title(),
body_markdown=content,
published=True,
canonical_url=post.permalink(absolute=True),
tags=post.tags
tags=post.tags,
)
LOGGER.info('Published {} to {}'.format(post.meta('slug'), m_post['url']))
LOGGER.info("Published {} to {}".format(post.meta("slug"), m_post["url"]))
1 change: 1 addition & 0 deletions v8/medium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ This plugin lets syndicate Nikola content to your Medium site.
5. Run ``nikola medium``

At that point your posts with the "medium" metadata set to "yes" should be published.
Any article that does not have "medium" metadata, or is set to "false" will be skipped.

**Be aware that if you use the command again after a short period of time it might post duplicate article, medium is taking a bit of time before updating the list of your posted articles that I compare from to avoid that.**
2 changes: 1 addition & 1 deletion v8/medium/medium_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _execute(self, options, args):
to_post = [
post
for post in posts
if post.title() not in medium_titles and post.meta("medium")
if post.title() not in medium_titles and post.meta("medium") and (post.meta("medium").lower() not in ["no", "false", "0"])
]

if len(to_post) == 0:
Expand Down