Skip to content

Commit

Permalink
feat: send text to idea in buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Oct 11, 2023
1 parent 77bc867 commit e677fff
Showing 1 changed file with 91 additions and 20 deletions.
111 changes: 91 additions & 20 deletions Buffer/Buffer_Send_text_to_Idea.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
{
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -189,25 +261,24 @@
},
{
"cell_type": "markdown",
"id": "bf514ffc-32f8-4b57-be27-36c4dfe7f693",
"metadata": {
"papermill": {},
"tags": []
},
"id": "f845450c-2afa-4b4d-8705-aa4b2201a587",
"metadata": {},
"source": [
"### Display result"
]
},
{
"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": {
Expand Down Expand Up @@ -238,4 +309,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit e677fff

Please sign in to comment.