scittle

Volod 2023-12-30T15:50:29.600079Z

Hi πŸ‘‹ I am playing with scittle ( ajax.core) and Youtube API. I cannot understand how to work with the response from the HTTP request. Please advise.

πŸ™‡ 1
βœ… 1
Volod 2023-12-30T15:52:22.957819Z

The function I use to make a call.

(require '[reagent.core :as r]
'[reagent.dom :as rdom]
'[ajax.core :refer [GET json-response-format]]
)

(def API-KEY "SECRET")


(defn download-all-comments [video-id]
    (GET
     ""
      ;; ""
      {
      :response-format :json
      :keywords? false
      :params {:part "snippet,replies"
                :maxResults 100
                :order "relevance"
                :textFormat "plainText"
                :videoId video-id
                :key API-KEY}
       :handler #(let [response %
                       items (-> response .-items)]
                   (js/console.log response)
                   (doseq [item items]
                     (let [snippet (-> item .-snippet)
                           topLevelComment (-> snippet .-topLevelComment)
                           textDisplay (-> topLevelComment .-snippet .-textDisplay)]
                       (println "Text display" textDisplay))))
      :error-handler #(println "Error" %)
      }))
The JSON response looks like the following
{
  "kind": "youtube#commentThreadListResponse",
  "etag": "lWAwf_dsD-6XTpMgp0EQ9SZ3UKU",
  "nextPageToken": "token=",
  "pageInfo": {
    "totalResults": 100,
    "resultsPerPage": 100
  },
  "items": [
    {
      "kind": "youtube#commentThread",
      "etag": "vxhgkEi2Mo4BJGF82RQMu5OBKDo",
      "id": "UgxnkvKqeYq5HKtMrHp4AaABAg",
      "snippet": {
        "channelId": "UCGZXYc32ri4D0gSLPf2pZXQ",
        "videoId": "dkklExEndHI",
        "topLevelComment": {
          "kind": "youtube#comment",
          "etag": "_6rWhUOMIj07KMaYYyG4gIwY270",
          "id": "UgxnkvKqeYq5HKtMrHp4AaABAg",
          "snippet": {
            "channelId": "UCGZXYc32ri4D0gSLPf2pZXQ",
            "videoId": "dkklExEndHI",
            "textDisplay": "sunlonger is blessed by God. when i listen to it always getting shivers. ",
            "textOriginal": "sunlonger is blessed by God. when i listen to it always getting shivers. ",
            "authorDisplayName": "@InterGlam",
            "authorProfileImageUrl": "",
            "authorChannelUrl": "",
            "authorChannelId": {
              "value": "UCrLOmuVrIXV_9KCoPKYE0mg"
            },
            "canRate": true,
            "viewerRating": "none",
            "likeCount": 3,
            "publishedAt": "2011-03-29T15:38:53Z",
            "updatedAt": "2011-03-29T15:38:53Z"
          }
        },
        "canReply": true,
        "totalReplyCount": 0,
        "isPublic": true
      }
    }
]}

Volod 2023-12-30T15:52:32.769679Z

The list of items is always empty when I try to access it. 🀷

Volod 2023-12-30T15:53:44.541319Z

console.log shows something like this and, I guess, it’s some internal representation of the json payload (dict)?

borkdude 2023-12-30T15:54:00.919399Z

Maybe it’s response , body and then items?

borkdude 2023-12-30T15:56:22.022899Z

ah got it

borkdude 2023-12-30T15:56:39.320459Z

the response is a Clojure map, not a JS object, so you need to use get to access items

borkdude 2023-12-30T15:56:48.958619Z

(-> response (get "items"))

Volod 2023-12-30T15:57:39.918899Z

Thank you, let me try that πŸ™‚

borkdude 2023-12-30T15:58:02.921059Z

strictly speaking you don't need ajax.core, you could also use the browser fetch API:

(-> (js/fetch "url")
  (.then (fn [response])
           (.json response)))
will give the response as a Promise of a JSON JS object

Volod 2023-12-30T15:59:42.847959Z

It works! Thank you very much for the get fn. I will try with js/fetch too. πŸ™‡

πŸ‘ 1
borkdude 2023-12-30T16:00:16.520129Z

if you don't disable :keywords? with ajax.core you can use (-> response :items)

Volod 2023-12-30T16:00:33.346309Z

I tried to set up cider as described in the docs but so far I get only nrepl connected without eval working in the editor like in your demo. It’s probably a separate thread πŸ˜…

borkdude 2023-12-30T16:00:58.305099Z

there is a thread about it here: https://clojurians.slack.com/archives/C034FQN490E/p1703238977455889

borkdude 2023-12-30T16:01:11.098179Z

one problem I found is that I had to uninstall clj-refactor

Volod 2023-12-30T16:01:48.003729Z

Thank you so much. I’ll check the thread πŸ™‡