Skip to content

Commit

Permalink
feat: added medium.com suppport
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive committed Jul 10, 2022
1 parent 32400fc commit a3366f2
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 47 deletions.
31 changes: 21 additions & 10 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import sys
import frontmatter
from dev import devto
from codenewbie import codenewbie
from hashnode import hashnode
from pathlib import Path
from publications.dev import devto
from publications.codenewbie import codenewbie
from publications.hashnode import hashnode
from publications.medium import medium
import json

def get_default_or_input(dictionary, keys):
Expand All @@ -17,15 +19,22 @@ def get_default_or_input(dictionary, keys):
with open("config.json", "r") as out:
config = json.load(out)

output_folder = config["output_folder"]

output = Path(output_folder)
output.mkdir(parents=True, exist_ok=True)

blog_link = config["blog_link"]

article = {}
article["title"] = get_default_or_input(post, ["title"])
article["description"] = get_default_or_input(post, ["subtitle", "description"])
slug = get_default_or_input(post, ["slug", "canonical_url"])
image_url = get_default_or_input(post, ["image_url"])
canonical_url = blog_link + str(slug)
article["canonical_url"] = canonical_url
slug = get_default_or_input(post, ["slug","canonical_url"])
if post["slug"]:
slug = blog_link + str(slug)
image_url = get_default_or_input(post, ["image_url", "cover_image"])

article["canonical_url"] = slug
article["cover_image"] = image_url
article["tags"] = get_default_or_input(post, ["tags"])
# article['date']=post['date']
Expand All @@ -42,10 +51,12 @@ def get_default_or_input(dictionary, keys):
opt = input("Where you would like to post? (1/2/3/4) : ")

if opt == "1":
devto(article)
devto(article, output)
elif opt == "2":
hashnode(article)
hashnode(article, output)
elif opt == "3":
codenewbie(article)
codenewbie(article, output)
elif opt == "4":
medium(article, output)
else:
print("Invalid Option")
3 changes: 2 additions & 1 deletion src/m.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
title: "Configure Neovim in Lua"
description: "something"
canonical_url: "https://www.meetgor.com/https://www.meetgor.com/https://www.meetgor.com/neovim-config-lua"
canonical_url: "https://www.meetgor.com/neovim-config-lua"
cover_image: "https://res.cloudinary.com/techstructive-blog/image/upload/v1656522785/blog-media/defer-golang-16.png"
published: false
---

Expand Down
Empty file added src/publications/__init__.py
Empty file.
49 changes: 26 additions & 23 deletions src/codenewbie.py → src/publications/codenewbie.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
import requests
import json
import sys


def codenewbie(article):
def codenewbie(article, output):

with open("keys.txt", "r") as file:
keys = file.readlines()

codenewbie_keys = keys[4]
codenewbie_keys = codenewbie_keys.split("codenewbie:")[1].strip()

API_ENDPOINT = "https://community.codenewbie.org/api/articles"

"""
data = {
'Content-Type': 'application/json',
'article': {
'title': article['title'],
'description': article['subtitle'],
'canonical_url': article['canonical_url'],
'published': article['published'],
'tags': article['tags'],
'series': article['series'],
'cover_image': article['cover_image'],
'body_markdown': article['content']
},
}
"""

post = {}

for key in article:
Expand All @@ -43,6 +25,29 @@ def codenewbie(article):

flag = True

codenewbie_frontmatter = "---\n"
post = {}
for key in article:
post[key] = article[key]
if key == "body_markdown":
codenewbie_frontmatter += f"---\n\n{post[key]}"
else:
if post[key]:
if not key == "published":
codenewbie_frontmatter += f"{key}: \"{post[key]}\"\n"
else:
codenewbie_frontmatter += f"{key}: {post[key]}\n"

with open(sys.argv[1], "w") as f:
f.write(codenewbie_frontmatter)

filename = post['title'].replace(" ", "_").lower()
output_file = output / f"{filename}_codenewbie_post.md"

with open(output_file, "w") as file:
file.write(codenewbie_frontmatter)

flag = True
author_articles_list = json.loads(requests.get("https://community.codenewbie.org/api/articles/me/published", headers=header).content)
for article_data in author_articles_list:
if article["body_markdown"] == article_data["body_markdown"]:
Expand All @@ -53,9 +58,7 @@ def codenewbie(article):
flag = False

if flag:
response = requests.post(
url=API_ENDPOINT, json=data, headers=header
).json()
response = requests.post(url=API_ENDPOINT, json=data, headers=header).json()
if "url" in response:
print("The article URL is: ", response["url"])
else:
Expand Down
12 changes: 1 addition & 11 deletions src/dev.py → src/publications/dev.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import frontmatter
import requests
import json
import sys
from pathlib import Path

def devto(article):
def devto(article, output):

with open("keys.txt", "r") as file:
keys = file.readlines()


with open("config.json", "r") as out:
config = json.load(out)

output_folder = config["output_folder"]

output = Path(output_folder)
output.mkdir(parents=True, exist_ok=True)

dev_frontmatter = "---\n"
post = {}
Expand Down
3 changes: 1 addition & 2 deletions src/hashnode.py → src/publications/hashnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys


def hashnode(article):
def hashnode(article, output):
markdown = sys.argv[1]

key_file = Path('keys.txt')
Expand All @@ -25,7 +25,6 @@ def hashnode(article):

with open(key_file, "r") as file:
keys = file.readlines()
print(keys)

if keys:
hashnode_keys = keys[2].split("hashnode:")[1].strip()
Expand Down
62 changes: 62 additions & 0 deletions src/publications/medium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import requests
import json

def replace_line(file_name, line_num, text):
lines = open(file_name, 'r').readlines()
print(lines)
lines[line_num] = text
out = open(file_name, 'w')
out.writelines(lines)
out.close()

def medium(article, output):

USERNAME_ENDPOINT = "https://api.medium.com/v1/me"
with open('keys.txt', 'r') as f:
keys = f.readlines()

if len(keys[2].split("medium.com"))>1:
medium_token = keys[2].split("medium.com:")[1].strip()
else:
medium_token = input("Enter your medium token: ")
replace_line("keys.txt", 2, f"medium.com:{medium_token}\n")


header = {"Authorization": "Bearer " + medium_token, "Content-Type": "application/json"}

medium_id = json.loads(requests.get(USERNAME_ENDPOINT, headers=header).content)["data"]["id"]

API_ENDPOINT = f"https://api.medium.com/v1/users/{medium_id}/posts"

post = {}
for key in article:
post[key] = article[key]

filename = post['title'].replace(" ", "_").lower()
output_file = output / f"{filename}_medium_post.md"

medium_content = ""
with open(output_file, "w") as f:
medium_content += f"## {post['title']}\n\n"
if post["cover_image"]:
medium_content += f"![{post['title']} 's cover image]({post['cover_image']})\n\n"

medium_content+= post["body_markdown"]

f.write(medium_content)

if post["published"] == "true":
status="public"
else:
status="draft"


request_josn = {"title": post["title"], "contentFormat": "markdown", "content": medium_content, "publishStatus": status}

response = requests.post(API_ENDPOINT, headers=header, json=request_josn)

if response.status_code == 200:
print("Article Posted at : ", response.content)
else:
print(response)
print(response.content)

0 comments on commit a3366f2

Please sign in to comment.