Skip to content

Commit

Permalink
feat: cli command and proper key reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive committed Jul 10, 2022
1 parent 9444520 commit c614fbd
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 140 deletions.
2 changes: 1 addition & 1 deletion crossposter/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from app import *
from crossposter import *
127 changes: 75 additions & 52 deletions crossposter/app.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,85 @@
import sys
import frontmatter
from pathlib import Path
from publications.dev import devto
from publications.codenewbie import codenewbie
from publications.hashnode import hashnode
from publications.medium import medium
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):
for key in keys:
if key in dictionary.keys():
return dictionary[key]
return input(f"Enter the {keys[0]} for post: ")
file_markdown = sys.argv[1]

post = frontmatter.load(file_markdown)

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"])
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']
status = get_default_or_input(post, ["status", "published"])
if status == "published":
article["published"] = "true"
else:
article["published"] = "false"
article["body_markdown"] = post.content
if "series" in post:
article["series"] = post["series"]

print(f"1. dev.to \n2. hashnode.com\n3. codenewbie\n4. medium.com\n")
opt = input("Where you would like to post? (1/2/3/4) : ")

if opt == "1":
devto(article, output)
elif opt == "2":
hashnode(article, output)
elif opt == "3":
codenewbie(article, output)
elif opt == "4":
medium(article, output)
else:
print("Invalid Option")


def main():
file_markdown = sys.argv[1]

post = frontmatter.load(file_markdown)

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"])
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']
status = get_default_or_input(post, ["status", "published"])
if status == "published":
article["published"] = "true"
else:
article["published"] = "false"
article["body_markdown"] = post.content
if "series" in post:
article["series"] = post["series"]

print(f"1. dev.to \n2. hashnode.com\n3. codenewbie\n4. medium.com\n")
opt = input("Where you would like to post? (1/2/3/4) : ")

key_file = Path("keys.txt")
key_file.touch(exist_ok=True)
if key_file.is_file():

f = open(key_file, "r")
lines = f.readlines()
f = open(key_file, "w")
lines.append("dev.to:\n")
lines.append("medium.com:\n")
lines.append("hashnode:\n")
lines.append("hashnode_id:\n")
lines.append("codenewbie:\n")
f.writelines(lines)
f.close()

if opt == "1":
devto(article, output)
elif opt == "2":
hashnode(article, output)
elif opt == "3":
codenewbie(article, output)
elif opt == "4":
medium(article, output)
else:
print("Invalid Option")


if __name__ == "__main__":
main()
27 changes: 19 additions & 8 deletions crossposter/publications/codenewbie.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import requests
import json
import sys
from crossposter.utils import replace_line


def codenewbie(article, output):

with open("keys.txt", "r") as file:
keys = file.readlines()
for line in open("keys.txt", "r"):
if line.startswith("codenewbie:"):
codenewbie_keys = line.split("codenewbie:")[1]

if codenewbie_keys != "\n":
codenewbie_keys = codenewbie_keys.strip()
else:
codenewbie_keys = input("Enter the Codenewbie API Key: ")
replace_line("keys.txt", 4, f"dev.to: {codenewbie_keys}\n")

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

post = {}

Expand All @@ -34,21 +41,25 @@ def codenewbie(article, output):
else:
if post[key]:
if not key == "published":
codenewbie_frontmatter += f"{key}: \"{post[key]}\"\n"
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()
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)
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"]:
flag = False
Expand Down
33 changes: 20 additions & 13 deletions crossposter/publications/dev.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import requests
import json
import sys
from crossposter.utils import replace_line


def devto(article, output):

with open("keys.txt", "r") as file:
keys = file.readlines()
dev_keys = []
for line in open("keys.txt", "r"):
if line.startswith("dev.to:"):
dev_keys = line.split("dev.to:")[1]

if dev_keys != "\n":
dev_keys = dev_keys.strip()
else:
dev_keys = input("Enter the DEV API Key: ")
replace_line("keys.txt", 0, f"dev.to: {dev_keys}\n")

dev_frontmatter = "---\n"
post = {}
Expand All @@ -17,21 +26,17 @@ def devto(article, output):
else:
if post[key]:
if not key == "published":
dev_frontmatter += f"{key}: \"{post[key]}\"\n"
dev_frontmatter += f'{key}: "{post[key]}"\n'
else:
dev_frontmatter += f"{key}: {post[key]}\n"

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

filename = post['title'].replace(" ", "_").lower()

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

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


dev_keys = keys[0]
dev_keys = dev_keys.split("dev.to:")[1].strip()

API_ENDPOINT = "https://dev.to/api/articles"
Expand All @@ -55,11 +60,13 @@ def devto(article, output):
},
}
"""
header={"api-key": dev_keys}
header = {"api-key": dev_keys}
flag = True
#author_data = json.loads(requests.get("https://dev.to/api/users/me", headers=header).content)
#author_username = author_data["username"]
author_articles_list = json.loads(requests.get("https://dev.to/api/articles/me/published", headers=header).content)
# author_data = json.loads(requests.get("https://dev.to/api/users/me", headers=header).content)
# author_username = author_data["username"]
author_articles_list = json.loads(
requests.get("https://dev.to/api/articles/me/published", headers=header).content
)
for article_data in author_articles_list:
if article["body_markdown"] == article_data["body_markdown"]:
flag = False
Expand Down
48 changes: 15 additions & 33 deletions crossposter/publications/hashnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,22 @@
def hashnode(article, output):
markdown = sys.argv[1]

key_file = Path('keys.txt')
key_file.touch(exist_ok=True)
if key_file.is_file():
for line in open("keys.txt", "r"):
if line.startswith("hashnode:"):
hashnode_keys = line.split("hashnode:")[1]
if line.startswith("hashnode_id:"):
hashnode_id = line.split("hashnode_id:")[1]

f = open(key_file, "r")
lines = f.readlines()
print(key_file)
f = open(key_file, "w")
lines.append("dev.to:\n")
lines.append("medium.com:\n")
lines.append("hashnode:\n")
lines.append("hashnode_id:\n")
lines.append("codenewbie:\n")
f.writelines(lines)
f.close()

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

if keys:
hashnode_keys = keys[2].split("hashnode:")[1].strip()
hashnode_id = keys[3].split("hashnode_id:")[1].strip()
if hashnode_keys != "\n":
hashnode_keys = hashnode_keys.strip()
else:
hashnode_keys = input("Enter the hashnode Keys: ")
hashnode_id = input("Enter your hashnode ID: ")

f = open(key_file, "r")
lines = f.readlines()
lines[2] = "hashnode:" + hashnode_keys + "\n"
lines[3] = "hashnode_id:" + hashnode_id + "\n"

f = open(key_file, "w")
f.writelines(lines)
f.close()
hashnode_keys = input("Enter the Hashnode API Key: ")
replace_line("keys.txt", 2, f"hashnode: {hashnode_keys}\n")
if hashnode_id != "\n":
hashnode_id = hashnode_id.strip()
else:
hashnode_id= input("Enter your Hashnode ID: ")
replace_line("keys.txt", 3, f"hashnode_id: {hashnode_id}\n")

title = str(article["title"])
subtitle = article["description"]
Expand All @@ -52,7 +34,7 @@ def hashnode(article, output):
"\n", "\\n"
) # .replace("\\c", "\c").replace("\r", "\t")
content = "".join(content.splitlines())
content = str(content.replace("\"", "\'"))
content = str(content.replace('"', "'"))

API_ENDPOINT = "https://api.hashnode.com"

Expand Down
Loading

0 comments on commit c614fbd

Please sign in to comment.