-
Notifications
You must be signed in to change notification settings - Fork 454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Medium-Publish article from markdown file #2302
Merged
srini047
merged 9 commits into
master
from
2276-medium-publish-article-from-mardown-file
Oct 16, 2023
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f418bd5
feat(Medium): Add Publish article from Mardown file
FlorentLvr afa690d
d1
MSaiKiran9 4d5e8b5
1
MSaiKiran9 5538c65
2
MSaiKiran9 cf4050e
Delete Medium/sample.md
srini047 616699e
rev
MSaiKiran9 eb173b8
1
MSaiKiran9 3e964a0
1.1
MSaiKiran9 62958b1
fix: minor bugs
srini047 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,296 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b2986071-1a29-4f66-995e-72565d384ce8", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5d7b0c0b-5e6c-4be3-b901-704fc00b6c08", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# Medium - Publish article from Mardown file" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ba4944a7-f4e5-4e89-9e3e-b4ee2e12c15f", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #medium #publish #article #markdown #file #api" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c96fc2a3-0910-4b6d-a735-1e83cd5fa24e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [SaiKiran M](www.linkedin.com/in/msaikiran9)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "edcf9f6c-c073-46c4-92ab-cc72866daf37", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-10-09 (Created: 2023-10-09)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "9b1586dd-d9e8-4f75-a583-c5cd5952bb05", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook explains how to publish an article from a Markdown file via the Medium API. It is usefull for organizations that need to quickly publish content on Medium." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cc1d8d75-0c1f-41ad-a6b0-b0eb43fcc665", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- [Programmatically Publish a Markdown File as a Medium Story with Python](https://betterprogramming.pub/programmatically-publish-a-markdown-file-as-a-medium-story-with-python-b2b072a5f968)\n", | ||
"- [Medium API Documentation](https://github.com/Medium/medium-api-docs)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "32f771ab-b85d-44c2-8555-004b07b512ea", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "74c2e136-cd6b-4079-9338-0b609b927692", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e4bad8c4-13c7-4c1c-b97d-46fc1ced422c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"try:\n", | ||
" import naas\n", | ||
" import json\n", | ||
" import requests\n", | ||
"except ImportError:\n", | ||
" print(\"One or more modules not found. Installing necessary packages...\")\n", | ||
" !pip install requests\n", | ||
" import requests" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "85e28cc8-0742-4aed-a9ee-c0b98cc00de4", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- `token`: Medium access token. [Instructions to get the token](https://github.com/Medium/medium-api-docs#22-authentication).\n", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"- `file_name`: Name of the Markdown file to be published." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7acc98ec-b364-4164-8524-bb0b7109d6c4", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"TOKEN = naas.secret.get(name=\"key\")\n", | ||
"file_name = \"sample.md\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "252ce263-9e42-4b03-aa2a-57efb0294c64", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "20f812e5-9880-4c5e-92d8-71591450bdc1", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Publish article" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f0e2693b-779a-4c19-9c6d-e46c756e8236", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"Long description of the function without break" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "650f0106-6407-4fa8-a4e5-019762a526b1", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def publish_post(title, content):\n", | ||
" headers = {\n", | ||
" \"Authorization\": f\"Bearer {TOKEN}\",\n", | ||
" \"Content-Type\": \"application/json\"\n", | ||
" }\n", | ||
" \n", | ||
" payload = {\n", | ||
" \"title\": title,\n", | ||
" \"contentFormat\": \"markdown\",\n", | ||
" \"content\": content,\n", | ||
" \"tags\": [\"example\", \"python\"],\n", | ||
" \"publishStatus\": \"public\",\n", | ||
" \"canonicalUrl\": \"http://example.com/sample\"\n", | ||
" } \n", | ||
" \n", | ||
" response = requests.get(\"https://api.medium.com/v1/me\", headers=headers, params={\"Authorization\": \"Bearer {}\".format(TOKEN)})\n", | ||
" if response.status_code == 200:\n", | ||
" author_id = response.json()['data']['id']\n", | ||
" url = \"https://api.medium.com/v1/users/{}/posts\".format(author_id)\n", | ||
" else:\n", | ||
" return\n", | ||
" \n", | ||
" response = requests.post(url, headers=headers, json=payload)\n", | ||
" if response.status_code == 201:\n", | ||
" return response.json()[\"data\"][\"url\"]\n", | ||
" else:\n", | ||
" raise Exception(f\"Failed to publish post: {response.json()}\")\n", | ||
"\n", | ||
"# Read the content of the Markdown file\n", | ||
"with open(\"sample.md\", \"r\", encoding=\"utf-8\") as markdown_file:\n", | ||
" content = markdown_file.read()\n", | ||
"\n", | ||
"# Publish the post\n", | ||
"post_url = publish_post(\"Sample Post Title\", content)\n", | ||
"\n", | ||
"print(\"Post published successfully!\")\n", | ||
"print(\"Post URL:\", post_url)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a4948757-3194-45e2-96f5-2e4463071eb3", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2aff52b9-5335-4240-9306-cb1238b29506", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "5b0af2a9-5227-45ac-9da9-d0bffabbbe0c", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"print(post_url)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MSaiKiran9 The file name has to be renamed according to the naming convention.
Have a look here.