From 7569c66384dd7ce78f39f858b690e64022fa6169 Mon Sep 17 00:00:00 2001 From: gicrisf Date: Thu, 17 Mar 2022 04:17:30 +0100 Subject: [PATCH 1/2] Adding Tweet Lookup Emacs Lisp example --- Tweet-Lookup/get_tweets_with_bearer_token.el | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Tweet-Lookup/get_tweets_with_bearer_token.el diff --git a/Tweet-Lookup/get_tweets_with_bearer_token.el b/Tweet-Lookup/get_tweets_with_bearer_token.el new file mode 100644 index 0000000..e924010 --- /dev/null +++ b/Tweet-Lookup/get_tweets_with_bearer_token.el @@ -0,0 +1,60 @@ +;;; get_tweets_with_bearer_token.el --- Description -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2022 gicrisf +;; +;; Author: gicrisf +;; Maintainer: gicrisf +;; Created: march 17, 2022 +;; Modified: march 17, 2022 +;; Version: 1.0.0 +;; Keywords: data +;; Homepage: https://github.com/twitterdev/Twitter-API-v2-sample-code +;; Package-Requires: ((emacs "24.3")) +;; +;; This file is not part of GNU Emacs. +;; +;;; Commentary: +;; +;; Description +;; Sample code for the Twitter API v2 endpoints +;; +;;; Code: + +(require 'request) + +(defvar bearer-token (getenv "BEARER_TOKEN")) + +(setq tweet_fields "tweet.fields=lang,author_id") +;; Tweet fields are adjustable. +;; Options include: +;; attachments, author_id, context_annotations, +;; conversation_id, created_at, entities, geo, id, +;; in_reply_to_user_id, lang, non_public_metrics, organic_metrics, +;; possibly_sensitive, promoted_metrics, public_metrics, referenced_tweets, +;; source, text, and withheld + +(setq ids "ids=1278747501642657792,1255542774432063488") +;; You can adjust ids to include a single Tweet. +;; Or you can add to up to 100 comma-separated IDs + +;; Let's build up the URL +(setq url (concat "https://api.twitter.com/2/tweets?" ids "&" tweet_fields)) + +;; Headers for the GET call +(setq auth_header (concat "Bearer " bearer_token)) +(setq hdrs '()) +(add-to-list 'hdrs (cons "Authorization" (concat "Bearer " bearer_token))) + +(request url + :type "GET" + :headers hdrs + :parser 'json-read + :error + (cl-function (lambda (&rest args &key error-thrown &allow-other-keys) + (message "Got error: %S" error-thrown))) + :success (cl-function + (lambda (&key data &allow-other-keys) + (message "Got: %s" data)))) + +(provide 'get_tweets_with_bearer_token) +;;; get_tweets_with_bearer_token.el ends here From 981dbf2e73d0168f09b9f59d420d1ebc888d345e Mon Sep 17 00:00:00 2001 From: gicrisf Date: Thu, 17 Mar 2022 04:26:17 +0100 Subject: [PATCH 2/2] Better comments --- Tweet-Lookup/get_tweets_with_bearer_token.el | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Tweet-Lookup/get_tweets_with_bearer_token.el b/Tweet-Lookup/get_tweets_with_bearer_token.el index e924010..350b61d 100644 --- a/Tweet-Lookup/get_tweets_with_bearer_token.el +++ b/Tweet-Lookup/get_tweets_with_bearer_token.el @@ -21,10 +21,13 @@ ;;; Code: (require 'request) +;; https://github.com/tkf/emacs-request +;; Retrieve your Environment Variable with the bearer token +;; Alternately, you could just put the token here as the other vars +;; (setq bearer-token ...) (defvar bearer-token (getenv "BEARER_TOKEN")) -(setq tweet_fields "tweet.fields=lang,author_id") ;; Tweet fields are adjustable. ;; Options include: ;; attachments, author_id, context_annotations, @@ -32,19 +35,22 @@ ;; in_reply_to_user_id, lang, non_public_metrics, organic_metrics, ;; possibly_sensitive, promoted_metrics, public_metrics, referenced_tweets, ;; source, text, and withheld +(setq tweet_fields "tweet.fields=lang,author_id") -(setq ids "ids=1278747501642657792,1255542774432063488") ;; You can adjust ids to include a single Tweet. ;; Or you can add to up to 100 comma-separated IDs +(setq ids "ids=1278747501642657792,1255542774432063488") ;; Let's build up the URL (setq url (concat "https://api.twitter.com/2/tweets?" ids "&" tweet_fields)) -;; Headers for the GET call -(setq auth_header (concat "Bearer " bearer_token)) +;; Headers for the request (setq hdrs '()) +;; We need just this field for authorization +(setq auth_header (concat "Bearer " bearer_token)) (add-to-list 'hdrs (cons "Authorization" (concat "Bearer " bearer_token))) +;; Core function with the actual GET request (request url :type "GET" :headers hdrs