Skip to content

Commit

Permalink
feat: check for existing articles before posting
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive committed Jul 9, 2022
1 parent f3c674c commit 3395d25
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
11 changes: 8 additions & 3 deletions pyscript/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
post = frontmatter.load(file_markdown)

article = {}
if any(key in post for key in ["description", "subtitle"]):
article["description"] = post.get("subtitle", "description")
article["title"] = post["title"]
article["description"] = post["subtitle"]
article["canonical_url"] = post["canonical_url"]
article["cover_image"] = post["cover_image"]
slug = post.get("slug", "")
image_url = post.get("image_url", "")
canonical_url = "https://www.meetgor.com/" + slug
article["canonical_url"] = canonical_url
article["cover_image"] = image_url
article["tags"] = post["tags"]
# article['date']=post['date']
article["published"] = post["published"]
article["published"] = post["status"]
article["body_markdown"] = post.content
if "series" in post:
article["series"] = post["series"]
Expand Down
34 changes: 23 additions & 11 deletions pyscript/codenewbie.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import requests
import json
import sys


def codenewbie(article):

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

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

API_ENDPOINT = "https://community.codenewbie.org/api/articles"
Expand All @@ -34,19 +33,32 @@ def codenewbie(article):
for key in article:
post[key] = article[key]

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

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

data = {
"Content-Type": "application/json",
"article": post,
}
print(data)
header = {"api-key": codenewbie_keys}
response = requests.post(
url=API_ENDPOINT, json=data, headers={"api-key": codenewbie_keys}
).json()

print("The article URL is:", response)
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"]:
flag = False
print("ERRR!!!")
break
if article["title"] == article_data["title"]:
flag = False

if flag:
response = requests.post(
url=API_ENDPOINT, json=data, headers=header
).json()
if "url" in response:
print("The article URL is: ", response["url"])
else:
print("The article URL is: ", response)
else:
print("Article already published")
29 changes: 22 additions & 7 deletions pyscript/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ def devto(article):
},
}
"""

response = requests.post(
url=API_ENDPOINT, json=data, headers={"api-key": dev_keys}
).json()
if "url" in response:
print("The article URL is: ", response["url"])
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)
for article_data in author_articles_list:
if article["body_markdown"] == article_data["body_markdown"]:
flag = False
print("ERRR!!!")
break
if article["title"] == article_data["title"]:
flag = False

if flag:
response = requests.post(
url=API_ENDPOINT, json=data, headers={"api-key": dev_keys}
).json()
if "url" in response:
print("The article URL is: ", response["url"])
else:
print("The article URL is: ", response)
else:
print("The article URL is: ", response)
print("Article already published")
39 changes: 36 additions & 3 deletions pyscript/hashnode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
import requests
import json
import sys
Expand All @@ -6,11 +7,42 @@
def hashnode(article):
markdown = sys.argv[1]

with open("keys.txt", "r") as file:
key_file = Path('keys.txt')
key_file.touch(exist_ok=True)
if key_file.is_file():

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()
print(keys)

if keys:
hashnode_keys = keys[2].split("hashnode:")[1].strip()
hashnode_id = keys[3].split("hashnode_id:")[1].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 = keys[3].split("hashnode:")[1].strip()
hashnode_id = keys[4].split("hashnode_id:")[1].strip()
title = str(article["title"])
subtitle = article["description"]
canonical_url = article["canonical_url"]
Expand All @@ -21,6 +53,7 @@ def hashnode(article):
"\n", "\\n"
) # .replace("\\c", "\c").replace("\r", "\t")
content = "".join(content.splitlines())
content = str(content.replace("\"", "\'"))

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

Expand Down

0 comments on commit 3395d25

Please sign in to comment.