001 (ns dog-and-duck.scratch.parser
002 (:require ;; [clojure.java.io :refer [file]]
003 ;; [clojure.string :refer [ends-with?]]
004 [clojure.walk :refer [keywordize-keys]]
005 [clojure.data.json :as json]
006 [dog-and-duck.quack.quack :as q]))
007
008 ;;; Copyright (C) Simon Brooke, 2022
009
010 ;;; This program is free software; you can redistribute it and/or
011 ;;; modify it under the terms of the GNU General Public License
012 ;;; as published by the Free Software Foundation; either version 2
013 ;;; of the License, or (at your option) any later version.
014
015 ;;; This program is distributed in the hope that it will be useful,
016 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
017 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018 ;;; GNU General Public License for more details.
019
020 ;;; You should have received a copy of the GNU General Public License
021 ;;; along with this program; if not, write to the Free Software
022 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
023
024 (defn clean
025 "Take this `json` input, and return a sequence of ActivityPub objects
026 represented by it."
027 [json]
028 (let [feed (json/read-str json)]
029 (map
030 keywordize-keys
031 (filter
032 q/object?
033 (cond (map? feed) (list (keywordize-keys feed))
034 (coll? feed) (map keywordize-keys feed))))))
035
036 ;; (clean (slurp "resources/activitystreams-test-documents/core-ex1-jsonld.json"))
037
038 ;; (map
039 ;; #(when
040 ;; (ends-with? (str %) ".json")
041 ;; (let [objects (clean (slurp %))]
042 ;; (list (str %)
043 ;; (count objects)
044 ;; (map :type objects))))
045 ;; (file-seq (file "resources/activitystreams-test-documents")))
046
047 ;; (-> "resources/activitystreams-test-documents/simple0020.json" slurp clean first :actor)