From e505ca84585a66f8edb7bf768a696a7ae9f20768 Mon Sep 17 00:00:00 2001 From: Mr-Destructive Date: Fri, 1 Apr 2022 16:18:11 +0530 Subject: [PATCH] feat: added python scripts --- {python => pyscript}/__init__.py | 0 pyscript/app.py | 33 ++++++++++++ pyscript/codenewbie.py | 52 +++++++++++++++++++ pyscript/dev.py | 46 +++++++++++++++++ pyscript/hashnode.py | 86 ++++++++++++++++++++++++++++++++ python/app.py | 30 ----------- python/codenewbie.py | 25 ---------- python/dev.py | 23 --------- python/hashnode.py | 55 -------------------- 9 files changed, 217 insertions(+), 133 deletions(-) rename {python => pyscript}/__init__.py (100%) create mode 100644 pyscript/app.py create mode 100644 pyscript/codenewbie.py create mode 100644 pyscript/dev.py create mode 100644 pyscript/hashnode.py delete mode 100644 python/app.py delete mode 100644 python/codenewbie.py delete mode 100644 python/dev.py delete mode 100644 python/hashnode.py diff --git a/python/__init__.py b/pyscript/__init__.py similarity index 100% rename from python/__init__.py rename to pyscript/__init__.py diff --git a/pyscript/app.py b/pyscript/app.py new file mode 100644 index 0000000..4fd5998 --- /dev/null +++ b/pyscript/app.py @@ -0,0 +1,33 @@ +import sys +import frontmatter +from dev import devto +from codenewbie import codenewbie +from hashnode import hashnode + +file_markdown = sys.argv[1] + +post = frontmatter.load(file_markdown) + +article = {} +article["title"] = post["title"] +article["description"] = post["subtitle"] +article["canonical_url"] = post["canonical_url"] +article["cover_image"] = post["cover_image"] +article["tags"] = post["tags"] +# article['date']=post['date'] +article["published"] = post["published"] +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) +elif opt == "2": + hashnode(article) +elif opt == "3": + codenewbie(article) +else: + print("Invalid Option") diff --git a/pyscript/codenewbie.py b/pyscript/codenewbie.py new file mode 100644 index 0000000..75ac67f --- /dev/null +++ b/pyscript/codenewbie.py @@ -0,0 +1,52 @@ +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 = 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: + post[key] = article[key] + + dev_keys = keys[0] + dev_keys = dev_keys.split("dev.to:")[1].strip() + + API_ENDPOINT = "https://dev.to/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) diff --git a/pyscript/dev.py b/pyscript/dev.py new file mode 100644 index 0000000..1543375 --- /dev/null +++ b/pyscript/dev.py @@ -0,0 +1,46 @@ +import requests +import json + + +def devto(article): + + with open("keys.txt", "r") as file: + keys = file.readlines() + + post = {} + + 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" + + data = { + "Content-Type": "application/json", + "article": post, + } + """ + 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'] + }, + } + """ + + 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) diff --git a/pyscript/hashnode.py b/pyscript/hashnode.py new file mode 100644 index 0000000..5dd4e68 --- /dev/null +++ b/pyscript/hashnode.py @@ -0,0 +1,86 @@ +import requests +import json +import sys + + +def hashnode(article): + markdown = sys.argv[1] + + with open("keys.txt", "r") as file: + keys = file.readlines() + + 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"] + cover_image = article["cover_image"] + tags = article["tags"] + published = article["published"] + content = article["body_markdown"].replace( + "\n", "\\n" + ) # .replace("\\c", "\c").replace("\r", "\t") + content = "".join(content.splitlines()) + + API_ENDPOINT = "https://api.hashnode.com" + + data = f""" + mutation{{ + createPublicationStory( + input: {{ + title: "{title}" + contentMarkdown: "{content}", + tags: [ + {{ + _id: "56744721958ef13879b94ffc", + name: "General Programming", + slug: "programming", + }} + ] + coverImageURL:"{cover_image}" + }} + publicationId: "{hashnode_id}", + hideFromHashnodeFeed:false + ) {{ + message + post{{ + title + }} + }} + }}""" + # Work in progress + # data= '{"query":"mutation { + # createPublicationStory( + # input: { + # title: \"'"$title"'\", + # contentMarkdown: \"'"$body"'\" + # tags: [ + # { + # _id: \"56744721958ef13879b94ffc\", + # name: \"General Programming\", + # slug: \"programming\" + # } + # ] + # coverImageURL:\"'"$cover_image"'\" } + # publicationId:\"'"$hash_id"'\", + # hideFromHashnodeFeed:false + # ) { + # message + # post{ + # title + # coverImage + # } + # } + # } + # }' --compressed + # + + header = { + "Content-Type": "application/json", + "Origin": "https://api.hashnode.com", + "Authorization": hashnode_keys, + } + + response = requests.post(url=API_ENDPOINT, json={"query": data}, headers=header) + + print("The article URL is:", response.json()) diff --git a/python/app.py b/python/app.py deleted file mode 100644 index ad76b97..0000000 --- a/python/app.py +++ /dev/null @@ -1,30 +0,0 @@ -import sys -import frontmatter -import dev -import codenewbie - -file_markdown = sys.argv[1] - -with open(file_markdown,'r') as file: - article = file.read() - -post=frontmatter.load(file_markdown) - -title=post['title'] -subtitle=post['subtitle'] -canonical_url=post['canonical_url'] -cover_image=post['cover_image'] -tags=post['tags'] -date=post['date'] -publishe=post['published'] -article=post.content - -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'): - dev.devto(article) -elif(opt == '3'): - codenewbie.codenewbie(article) -else: - print("Invalid Option") diff --git a/python/codenewbie.py b/python/codenewbie.py deleted file mode 100644 index 00795f0..0000000 --- a/python/codenewbie.py +++ /dev/null @@ -1,25 +0,0 @@ -import requests -import sys - -def codenewbie(article): - - with open('keys.txt', 'r') as file: - keys = file.readlines() - - codenewbie_keys = keys[5] - codenewbie_keys = codenewbie_keys.split('codenewbie:')[1].strip() - - API_ENDPOINT = "https://community.codenewbie.org/api/articles" - - data = { - 'Content-Type': 'application/json', - 'article': {'body_markdown':article}, - } - - response = requests.post(url = API_ENDPOINT,json=data, headers={"api-key":codenewbie_keys}).json() - - print("The article URL is:", response['url']) - - if(response['url']): - return True - return False diff --git a/python/dev.py b/python/dev.py deleted file mode 100644 index 1fc0c91..0000000 --- a/python/dev.py +++ /dev/null @@ -1,23 +0,0 @@ -import requests -import sys - -def devto(article): - with open('keys.txt', 'r') as file: - keys = file.readlines() - - dev_keys = keys[0] - dev_keys = dev_keys.split('dev.to:')[1].strip() - - API_ENDPOINT = "https://dev.to/api/articles" - - data = { - 'Content-Type': 'application/json', - 'article': {'body_markdown':article}, - } - - response = requests.post(url = API_ENDPOINT,json=data, headers={"api-key":dev_keys}).json() - - print("The article URL is: ",response['url']) - if(response['url']): - return True - return False diff --git a/python/hashnode.py b/python/hashnode.py deleted file mode 100644 index 7a2f552..0000000 --- a/python/hashnode.py +++ /dev/null @@ -1,55 +0,0 @@ -import requests -import sys - -markdown = sys.argv[1] -with open(markdown,'r') as file: - article = file.read() - -with open('keys.txt', 'r') as file: - keys = file.readlines() - -hashnode_keys = keys[4] -hashnode_keys = hashnode_keys.split('hashnode:')[1].strip() - -API_ENDPOINT = "https://api.hashnode.com" - -# Work in progress -#data= '{"query":"mutation { -# createPublicationStory( -# input: { -# title: \"'"$title"'\", -# contentMarkdown: \"'"$body"'\" -# tags: [ -# { -# _id: \"56744721958ef13879b94ffc\", -# name: \"General Programming\", -# slug: \"programming\" -# } -# ] -# coverImageURL:\"'"$cover_image"'\" } -# publicationId:\"'"$hash_id"'\", -# hideFromHashnodeFeed:false -# ) { -# message -# post{ -# title -# coverImage -# } -# } -# } -# }' --compressed -# -#header = { -# 'Accept-Encoding': 'gzip, deflate', -# 'Content-Type': 'application/json', -# 'Accept': 'application/json', -# 'Connection': 'keep-alive', -# 'DNT': '1' -# 'Origin': 'https://api.hashnode.com' -# 'Authorization': hashnode_keys, -# } -# -#response = requests.post(url = API_ENDPOINT,json=data, headers=header.json()) -# -#print("The article URL is:", response['url']) -