Skip to content

Commit

Permalink
feat: added python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Destructive committed Apr 1, 2022
1 parent 3035f73 commit e505ca8
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 133 deletions.
File renamed without changes.
33 changes: 33 additions & 0 deletions pyscript/app.py
Original file line number Diff line number Diff line change
@@ -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")
52 changes: 52 additions & 0 deletions pyscript/codenewbie.py
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 46 additions & 0 deletions pyscript/dev.py
Original file line number Diff line number Diff line change
@@ -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)
86 changes: 86 additions & 0 deletions pyscript/hashnode.py
Original file line number Diff line number Diff line change
@@ -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())
30 changes: 0 additions & 30 deletions python/app.py

This file was deleted.

25 changes: 0 additions & 25 deletions python/codenewbie.py

This file was deleted.

23 changes: 0 additions & 23 deletions python/dev.py

This file was deleted.

55 changes: 0 additions & 55 deletions python/hashnode.py

This file was deleted.

0 comments on commit e505ca8

Please sign in to comment.