Skip to content

Commit

Permalink
fix: view output file without posting
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive committed Oct 24, 2022
1 parent dde085c commit c15573d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 38 deletions.
4 changes: 4 additions & 0 deletions crossposter/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
from rich import print
from pathlib import Path
from .utils import generate_file
from .publications.dev import devto
from .publications.codenewbie import codenewbie
from .publications.hashnode import hashnode
Expand Down Expand Up @@ -46,6 +47,7 @@ def main():
parser.add_argument("--dev", action="store_true", help="Post to dev.to")
parser.add_argument("--med", action="store_true", help="Post to medium.com")
parser.add_argument("--cdb", action="store_true", help="Post to codenewbie")
parser.add_argument("--opf", action="store_true", help="Save to a File")
args = parser.parse_args()
post = frontmatter.load(file_markdown)

Expand Down Expand Up @@ -104,6 +106,8 @@ def main():
medium(article, output)
elif args.cdb:
codenewbie(article, output)
elif args.opf:
generate_file(article, output)
else:
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) : ")
Expand Down
58 changes: 34 additions & 24 deletions crossposter/publications/codenewbie.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import requests
import json
import sys
from crossposter.utils import replace_line
from crossposter.utils import hard_to_soft_wraps, replace_line


def codenewbie_file(article, output):
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"

lines = hard_to_soft_wraps(codenewbie_frontmatter)

filename = post["title"].replace(" ", "_").lower()
output_file = output / f"{filename}_codenewbie_post.md"
with open(output_file, "w") as f:
f.writelines(lines)
print("The Codenewbie frontmatter is generated in the file -> ", output_file)
return post


def codenewbie(article, output):

print("Cross-Posting on codenewbie.community")
from rich.progress import Progress

with Progress() as progress:
task = progress.add_task("Crossposting..", total=100)
while not progress.finished:
progress.update(task, advance=10)

codenewbie_keys = []
for line in open("keys.txt", "r"):
if line.startswith("codenewbie:"):
Expand All @@ -18,6 +48,8 @@ def codenewbie(article, output):
codenewbie_keys = input("Enter the Codenewbie API Key: ")
replace_line("keys.txt", 4, f"dev.to: {codenewbie_keys}\n")

post = codenewbie_file(article, output)

post = {}

for key in article:
Expand All @@ -33,28 +65,6 @@ def codenewbie(article, output):

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(
Expand Down
30 changes: 17 additions & 13 deletions crossposter/publications/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,8 @@
from crossposter.utils import hard_to_soft_wraps, replace_line


def devto(article, output):

def devto_file(article, output):
print("Cross-Posting on dev.to")
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 @@ -36,11 +25,26 @@ def devto(article, output):
import re

lines = hard_to_soft_wraps(dev_frontmatter)

with open(output_file, "w") as f:
f.writelines(lines)
print("The DEV frontmatter is generated in the file -> ", output_file)
return post


def devto(article, output):

post = devto_file(article, output)

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")
API_ENDPOINT = "https://dev.to/api/articles"

data = {
Expand Down
8 changes: 8 additions & 0 deletions crossposter/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import re
from crossposter.publications.codenewbie import codenewbie_file

from crossposter.publications.dev import devto_file


def replace_line(file_name, line_num, text):
Expand Down Expand Up @@ -42,3 +45,8 @@ def hard_to_soft_wraps(content):

# returned soft wrapped content
return content


def generate_file(article, output):
devto_file(article, output)
codenewbie_file(article, output)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

HERE = pathlib.Path(__file__).parent

VERSION = "0.5.1"
VERSION = "0.5.2"
PACKAGE_NAME = "crossposter"
AUTHOR = "Meet Gor"
AUTHOR_EMAIL = "[email protected]"
Expand Down

0 comments on commit c15573d

Please sign in to comment.