Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Buffer/Buffer_Create_draft.ipynb
2973 views
Kernel: Python 3

Buffer.png

Buffer - Create draft

Give Feedback | Bug report

Tags: #buffer #draft #create #socialmedia #socialnetworks #socialmediaplatforms

Last update: 2023-10-05 (Created: 2023-10-05)

Description: This notebook creates a draft for a specific profile on Buffer.

Input

Import libraries

import requests import json

Setup variables

  • 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"

  • 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.

  • cookie: This variable is used to store the cookie information from your Buffer session.

buffer_url = "https://publish.buffer.com/profile/xxxxxxxxxxxxxx/tab/queue" text = "My Draft" cookie = naas.secret.get("BUFFER_COOKIES") or "Inspect your Buffer and Copy/Paste your cookie"

Model

Create draft

def parse_cookie(cookie): data = {} for c in cookie.split(";"): key = c.split("=", 1)[0].strip() value = c.split("=", 1)[-1].strip() data[key] = value return data def create_draft(buffer_url, cookie, text): # Init result = None cookies = parse_cookie(cookie) profile_id = buffer_url.split("/profile/")[-1].split("/")[0] # Base URL url = 'https://publish.buffer.com/rpc/composerApiProxy' # Headers headers = { 'authority': 'publish.buffer.com', 'accept': 'application/json', 'accept-language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7', 'content-type': 'application/json', } args = { "url": "/1/updates/create.json", "args": { "now": False, "top": False, "is_draft": True, "shorten": True, "text": text, "scheduling_type": "direct", "fb_text": "", "entities": None, "annotations": [], "profile_ids": [profile_id], "attachment": False, "via": None, "source": None, "version": None, "created_source": "queue", "channel_data": None }, "HTTPMethod": "POST" } data = { "args": json.dumps(args) # Convert the inner dictionary to a string } res = requests.post(url, headers=headers, data=json.dumps(data), cookies=cookies) res.raise_for_status if res.status_code == 200: result = res.json().get("result") else: print(res.status_code) print(res.text) return result result = create_draft(buffer_url, cookies, text) result

Output

Display result

result