From 3395d256853bbf185b14969f9de5510d6c4e8b3f Mon Sep 17 00:00:00 2001 From: mr_destructive Date: Sat, 9 Jul 2022 22:57:10 +0530 Subject: [PATCH] feat: check for existing articles before posting --- pyscript/app.py | 11 ++++++++--- pyscript/codenewbie.py | 34 +++++++++++++++++++++++----------- pyscript/dev.py | 29 ++++++++++++++++++++++------- pyscript/hashnode.py | 39 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 89 insertions(+), 24 deletions(-) diff --git a/pyscript/app.py b/pyscript/app.py index 4fd5998..320f3ea 100644 --- a/pyscript/app.py +++ b/pyscript/app.py @@ -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"] diff --git a/pyscript/codenewbie.py b/pyscript/codenewbie.py index 75ac67f..2069da0 100644 --- a/pyscript/codenewbie.py +++ b/pyscript/codenewbie.py @@ -1,6 +1,5 @@ import requests import json -import sys def codenewbie(article): @@ -8,7 +7,7 @@ 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" @@ -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") diff --git a/pyscript/dev.py b/pyscript/dev.py index 1543375..f90cf7e 100644 --- a/pyscript/dev.py +++ b/pyscript/dev.py @@ -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") diff --git a/pyscript/hashnode.py b/pyscript/hashnode.py index e1651b7..d1830d0 100644 --- a/pyscript/hashnode.py +++ b/pyscript/hashnode.py @@ -1,3 +1,4 @@ +from pathlib import Path import requests import json import sys @@ -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"] @@ -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"