From 1c55ccd2cd74b3d100771cd4c72471a258e0c517 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Wed, 11 Oct 2023 17:55:01 +0200 Subject: [PATCH 1/3] feat(Buffer): Add Send text to Idea --- Buffer/Buffer_Send_text_to_Idea.ipynb | 241 ++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 Buffer/Buffer_Send_text_to_Idea.ipynb diff --git a/Buffer/Buffer_Send_text_to_Idea.ipynb b/Buffer/Buffer_Send_text_to_Idea.ipynb new file mode 100644 index 0000000000..ff303f89e9 --- /dev/null +++ b/Buffer/Buffer_Send_text_to_Idea.ipynb @@ -0,0 +1,241 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3d064ccd-ca27-4d34-85f6-84ce63dcc206", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "fe7f0846-ce7f-43c9-8202-a90b9e29cd4c", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Buffer - Send text to Idea" + ] + }, + { + "cell_type": "markdown", + "id": "c0403ba8-62ef-4413-94d4-5b225a4ae7ac", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #buffer #idea #text #send #python #api" + ] + }, + { + "cell_type": "markdown", + "id": "d295e708-adaa-45b8-911e-976451f36f80", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" + ] + }, + { + "cell_type": "markdown", + "id": "03594f2a-572a-4303-9161-c8e6b6d42f17", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-10-11 (Created: 2023-10-11)" + ] + }, + { + "cell_type": "markdown", + "id": "61edbaba-cd8f-43f2-b956-247d2408756f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook will show how to send text to Idea using the Buffer API. It is usefull for organizations that need to quickly send text to Idea." + ] + }, + { + "cell_type": "markdown", + "id": "633cba7e-6805-4f74-bfb7-a57f316a1958", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n- [Buffer API Documentation](https://buffer.com/developers/api/overview)\n- [Buffer Python Library](https://github.com/bufferapp/buffer-python)" + ] + }, + { + "cell_type": "markdown", + "id": "dd7865b3-eb7b-4755-9929-54ae4355ac46", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "9db307ec-534b-47b4-a606-6cf4e9213d24", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9de34e94-bcf2-40c1-8970-6a7e07f4917b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "import buffer", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "ad728d7a-2f44-4cf6-af85-20b48336ba49", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n- **access_token**: Access token to use the Buffer API. [Get your access token](https://buffer.com/developers/api/overview)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f30fb5e-a068-4f2c-af42-c6c7c917d40f", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "access_token = \"YOUR_ACCESS_TOKEN\"", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "bf29c888-784f-4427-9a1b-0161fcdd8246", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "090529fe-160d-4099-bfd1-e19fe6356777", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Send text to Idea" + ] + }, + { + "cell_type": "markdown", + "id": "0df9260c-b899-4666-ad12-1dd2ece7124b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "This function will send text to Idea using the Buffer API." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6e09f56c-9254-432f-afdf-5674f16b18aa", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "def send_text_to_idea(text):\n # Create a Buffer client\n client = buffer.Buffer(access_token)\n # Create a post\n post = buffer.Post(text=text)\n # Send the post to Idea\n client.create_update(post, service=\"idea\")", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "9ef12d0f-2efe-4163-90a9-2ec9206c2ce5", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "bf514ffc-32f8-4b57-be27-36c4dfe7f693", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0b478a6e-8cf3-4984-8cd1-f2af23f73462", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "send_text_to_idea(\"This is a test\")", + "outputs": [] + } + ], + "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 +} \ No newline at end of file From 77bc8670403ea2714f3581f75a603a8810b32eae Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Wed, 11 Oct 2023 18:15:05 +0200 Subject: [PATCH 2/3] feat: update template and fix error (status !) 200) --- Buffer/Buffer_Create_draft.ipynb | 82 ++++++++++++++++---------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/Buffer/Buffer_Create_draft.ipynb b/Buffer/Buffer_Create_draft.ipynb index 5b53c1a0b0..69c3fdc37c 100644 --- a/Buffer/Buffer_Create_draft.ipynb +++ b/Buffer/Buffer_Create_draft.ipynb @@ -157,19 +157,19 @@ }, { "cell_type": "markdown", - "id": "e17e49b3-ec98-4ebb-831f-ade9ef8951e0", + "id": "3daa3956-b58a-476f-81ba-c3f3a35ece4c", "metadata": { "papermill": {}, "tags": [] }, "source": [ - "### Parse cookies" + "### Create draft" ] }, { "cell_type": "code", "execution_count": null, - "id": "e356d2ed-3755-4682-9444-bb345aaa3e26", + "id": "7a31b1a2-e325-4943-9d14-23429e63b224", "metadata": { "papermill": {}, "tags": [] @@ -184,44 +184,10 @@ " data[key] = value\n", " return data\n", "\n", - "cookies = parse_cookie(cookie)" - ] - }, - { - "cell_type": "markdown", - "id": "2c979314-482d-4ead-94fa-b2c001637ca8", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "## Output" - ] - }, - { - "cell_type": "markdown", - "id": "3daa3956-b58a-476f-81ba-c3f3a35ece4c", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "### Create draft" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7a31b1a2-e325-4943-9d14-23429e63b224", - "metadata": { - "papermill": {}, - "tags": [] - }, - "outputs": [], - "source": [ - "def create_draft(buffer_url, cookies, text):\n", + "def create_draft(buffer_url, cookie, text):\n", " # Init\n", " result = None\n", + " cookies = parse_cookie(cookie)\n", " profile_id = buffer_url.split(\"/profile/\")[-1].split(\"/\")[0]\n", " \n", " # Base URL\n", @@ -266,13 +232,45 @@ " if res.status_code == 200:\n", " result = res.json().get(\"result\")\n", " else:\n", - " print(response.status_code)\n", - " print(response.text)\n", + " print(res.status_code)\n", + " print(res.text)\n", " return result\n", "\n", "result = create_draft(buffer_url, cookies, text)\n", "result" ] + }, + { + "cell_type": "markdown", + "id": "2c979314-482d-4ead-94fa-b2c001637ca8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "3542ac38-b7a0-40c0-8ed0-12d4d5431b7a", + "metadata": {}, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e356d2ed-3755-4682-9444-bb345aaa3e26", + "metadata": { + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "result" + ] } ], "metadata": { @@ -313,4 +311,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From e677fff5a41d31c0e4d1f1828982b6580c768fac Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Wed, 11 Oct 2023 18:15:22 +0200 Subject: [PATCH 3/3] feat: send text to idea in buffer --- Buffer/Buffer_Send_text_to_Idea.ipynb | 111 +++++++++++++++++++++----- 1 file changed, 91 insertions(+), 20 deletions(-) diff --git a/Buffer/Buffer_Send_text_to_Idea.ipynb b/Buffer/Buffer_Send_text_to_Idea.ipynb index ff303f89e9..2c187a7d7d 100644 --- a/Buffer/Buffer_Send_text_to_Idea.ipynb +++ b/Buffer/Buffer_Send_text_to_Idea.ipynb @@ -63,7 +63,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook will show how to send text to Idea using the Buffer API. It is usefull for organizations that need to quickly send text to Idea." + "**Description:** This notebook sent a text to a Buffer Idea." ] }, { @@ -74,7 +74,8 @@ "tags": [] }, "source": [ - "**References:**\n- [Buffer API Documentation](https://buffer.com/developers/api/overview)\n- [Buffer Python Library](https://github.com/bufferapp/buffer-python)" + "**References:**\n", + "- [Buffer - Create Idea](https://publish.buffer.com/content)" ] }, { @@ -107,30 +108,40 @@ "papermill": {}, "tags": [] }, - "source": "import buffer", - "outputs": [] + "outputs": [], + "source": [ + "import requests\n", + "import json" + ] }, { "cell_type": "markdown", - "id": "ad728d7a-2f44-4cf6-af85-20b48336ba49", + "id": "18fe9773-ca11-4d15-b0c0-e3b86315415d", "metadata": { "papermill": {}, "tags": [] }, "source": [ - "### Setup variables\n- **access_token**: Access token to use the Buffer API. [Get your access token](https://buffer.com/developers/api/overview)" + "### Setup variables\n", + "- `buffer_url`: This variable holds the URL of the specific Buffer profile where you want to create a new post. Example: \"https://publish.buffer.com/profile/xxxxxxxxxxx/tab/queue\"\n", + "- `text`: This variable contains the text content that you want to post on the Buffer profile. You can modify this string to change the content of the post.\n", + "- `cookie`: This variable is used to store the cookie information from your Buffer session. The `input()` function is used to prompt you to manually enter your cookie information. You need to inspect your Buffer session in your web browser and copy/paste your cookie into the prompt. The cookie is used for authentication and maintaining the session with Buffer." ] }, { "cell_type": "code", "execution_count": null, - "id": "9f30fb5e-a068-4f2c-af42-c6c7c917d40f", + "id": "3d26d9bc-d873-4f81-a5f2-fa17e9d1a4dd", "metadata": { "papermill": {}, "tags": [] }, - "source": "access_token = \"YOUR_ACCESS_TOKEN\"", - "outputs": [] + "outputs": [], + "source": [ + "organization_id = \"xxxxxxx\"\n", + "text = \"My Buffer Idea\"\n", + "cookie = input(\"Inspect your Buffer and Copy/Paste your cookie:\")" + ] }, { "cell_type": "markdown", @@ -173,8 +184,69 @@ "papermill": {}, "tags": [] }, - "source": "def send_text_to_idea(text):\n # Create a Buffer client\n client = buffer.Buffer(access_token)\n # Create a post\n post = buffer.Post(text=text)\n # Send the post to Idea\n client.create_update(post, service=\"idea\")", - "outputs": [] + "outputs": [], + "source": [ + "def parse_cookie(cookie):\n", + " data = {}\n", + " for c in cookie.split(\";\"):\n", + " key = c.split(\"=\", 1)[0].strip()\n", + " value = c.split(\"=\", 1)[-1].strip()\n", + " data[key] = value\n", + " return data\n", + "\n", + "def send_text_to_idea(organization_id, cookie, text):\n", + " # Init\n", + " result = None\n", + " cookies = parse_cookie(cookie)\n", + " \n", + " # Base URL\n", + " url = \"https://graph.buffer.com/\"\n", + " \n", + " # Headers\n", + " headers = {\n", + " \"authority\": \"graph.buffer.com\",\n", + " \"accept\": \"*/*\",\n", + " \"accept-language\": \"fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7\",\n", + " \"content-type\": \"application/json\",\n", + " \"origin\": \"https://publish.buffer.com\",\n", + " \"referer\": \"https://publish.buffer.com/\",\n", + " \"sec-ch-ua\": '\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"',\n", + " \"sec-ch-ua-mobile\": \"?0\",\n", + " \"sec-ch-ua-platform\": '\"Windows\"',\n", + " \"sec-fetch-dest\": \"empty\",\n", + " \"sec-fetch-mode\": \"cors\",\n", + " \"sec-fetch-site\": \"same-site\",\n", + " \"user-agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\",\n", + " \"x-buffer-client-id\": \"webapp-publishing\"\n", + " }\n", + "\n", + " data = {\n", + " \"operationName\": \"CreateIdea\",\n", + " \"variables\": {\n", + " \"input\": {\n", + " \"content\": {\n", + " \"text\": text,\n", + " \"media\": [],\n", + " \"tags\": []\n", + " },\n", + " \"organizationId\": organization_id,\n", + " \"source\": \"content\"\n", + " }\n", + " },\n", + " \"query\": 'mutation CreateIdea($input: IdeaCreationInput\\u0021) {\\n ideaCreate(input: $input) {\\n ... on IdeaMutationSuccess {\\n success\\n message\\n idea {\\n id\\n content {\\n text\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n ... on CoreWebAppCommonError {\\n message\\n code\\n error\\n __typename\\n }\\n __typename\\n }\\n}\\n'\n", + " }\n", + " res = requests.post(url, headers=headers, data=json.dumps(data), cookies=cookies)\n", + " res.raise_for_status\n", + " if res.status_code == 200:\n", + " result = res.json()\n", + " else:\n", + " print(res.status_code)\n", + " print(res.text)\n", + " return result\n", + "\n", + "result = send_text_to_idea(organization_id, cookies, text)\n", + "result" + ] }, { "cell_type": "markdown", @@ -189,11 +261,8 @@ }, { "cell_type": "markdown", - "id": "bf514ffc-32f8-4b57-be27-36c4dfe7f693", - "metadata": { - "papermill": {}, - "tags": [] - }, + "id": "f845450c-2afa-4b4d-8705-aa4b2201a587", + "metadata": {}, "source": [ "### Display result" ] @@ -201,13 +270,15 @@ { "cell_type": "code", "execution_count": null, - "id": "0b478a6e-8cf3-4984-8cd1-f2af23f73462", + "id": "14f8340e-0c63-46a6-b572-81f4408b5ddb", "metadata": { "papermill": {}, "tags": [] }, - "source": "send_text_to_idea(\"This is a test\")", - "outputs": [] + "outputs": [], + "source": [ + "result" + ] } ], "metadata": { @@ -238,4 +309,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +}