diff --git a/Medium/Medium_Publish_article_from_Mardown_file.ipynb b/Medium/Medium_Publish_article_from_Mardown_file.ipynb new file mode 100644 index 0000000000..49a647f3fe --- /dev/null +++ b/Medium/Medium_Publish_article_from_Mardown_file.ipynb @@ -0,0 +1,299 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b2986071-1a29-4f66-995e-72565d384ce8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "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\n", + "#step 1 : import the necessary libraries here i.e., requests as json and naas are already present." + ] + }, + { + "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", + "- `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": [ + "#step 2 : store the API token that's generated from Medium -> settings -> security and apps -> integration tokens into naas.\n", + "TOKEN = naas.secret.get(name=\"MEDIUM_TOKEN\")\n", + "file_name = \"sample.md\" #this is file to be uploaded in medium as an article ." + ] + }, + { + "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": [ + "#step 3 : In this step we setup publish_post fill the necessary headers & url to send the auth request.It'll send back a \n", + "#author_id user's unique identifier.And we will request to post content to the url containing the author_id at -> #1\n", + "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", + " #get the author's complete api end point url.\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", + " #post the data .\n", + " response = requests.post(url, headers=headers, json=payload) #1\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", + "#step 4 : Read the content of the Markdown file here let's say from \"sample.md\" file\n", + "with open(\"sample.md\", \"r\", encoding=\"utf-8\") as markdown_file:\n", + " content = markdown_file.read()\n", + "\n", + "#step 5 : Publish the post\n", + "post_url = publish_post(\"Sample Post Title\", content)\n", + "\n", + "print(\"Post published successfully!\")" + ] + }, + { + "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 +}