summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/weepinbell/microformats_parser.clj (renamed from src/weepinbell/microformats.clj)3
-rw-r--r--src/weepinbell/microformats_wrapper.clj52
-rw-r--r--src/weepinbell/wrapper.clj4
3 files changed, 55 insertions, 4 deletions
diff --git a/src/weepinbell/microformats.clj b/src/weepinbell/microformats_parser.clj
index 9184d9a..4bf915c 100644
--- a/src/weepinbell/microformats.clj
+++ b/src/weepinbell/microformats_parser.clj
@@ -1,4 +1,4 @@
-(ns weepinbell.microformats
+(ns weepinbell.microformats-parser
(:require [clojure.string :refer [includes? lower-case]]
[taoensso.timbre :as timbre])
(:import [com.kylewm.mf2j Mf2Parser]
@@ -32,4 +32,3 @@
(timbre/warn (format "MF2 Parsing failed for %s after %d attempts. Error: %s"
url attempt (or msg "Unknown error")))
nil))))))))
-
diff --git a/src/weepinbell/microformats_wrapper.clj b/src/weepinbell/microformats_wrapper.clj
new file mode 100644
index 0000000..05a49f4
--- /dev/null
+++ b/src/weepinbell/microformats_wrapper.clj
@@ -0,0 +1,52 @@
+(ns weepinbell.microformats-wrapper
+ (:require [weepinbell.utils :refer [javamap->map]]
+ [weepinbell.storage :as store]))
+
+(defn- find-item
+ "Find specific type of items (like h-entry, h-card) in parsed microformats."
+ [typ mf]
+ (let [items (get mf "items")
+ get-type #(first (get % "type"))]
+ (filter #(= (get-type %) typ) items)))
+
+(defn- get-prop
+ "Use keyword get get a property value from a microformats item."
+ [entry prop]
+ (-> entry
+ javamap->map
+ :properties
+ javamap->map
+ (get prop)))
+
+(defn- get-prop-first
+ "Alias of (first (get-prop ...))"
+ [entry prop]
+ (first (get-prop entry prop)))
+
+(defn- get-entry-type
+ "Get type of an h-entry item in keyword. (:like-of, :repost-of, :in-reply-to)"
+ [entry]
+ ;; rsvp type is not supported yet
+ (or
+ (->> [:like-of :repost-of :in-reply-to]
+ (filter #(get-prop entry %))
+ first)
+ :mention))
+
+(defn- get-card-info
+ "Get essential information of an h-card item and flatten it into a map."
+ [card]
+ {:name (get-prop-first card :name)
+ :nickname (get-prop-first card :nickname)
+ :photo (get-prop-first card :photo)
+ :url (get-prop-first card :url)})
+
+(defn wrap-entry
+ "Wrap parsed raw microformats (h-entry) data. If no h-entry found, return nil."
+ [entry]
+ (when entry
+ {:author (get-card-info (get-prop-first entry :author))
+ :type (get-entry-type entry)
+ :published (get-prop-first entry :published)
+ :content (javamap->map (get-prop-first entry :content))}))
+
diff --git a/src/weepinbell/wrapper.clj b/src/weepinbell/wrapper.clj
index 10cad52..97d81cf 100644
--- a/src/weepinbell/wrapper.clj
+++ b/src/weepinbell/wrapper.clj
@@ -3,7 +3,7 @@
file-exists?
download-file]]
[weepinbell.config :refer [get-config]]
- [weepinbell.microformats :as microformats]
+ [weepinbell.microformats-parser :as mf-parser]
[taoensso.timbre :as timbre]
[md5.core :refer [string->md5-hex]]))
@@ -33,7 +33,7 @@
"Parse microformats and collect essential info. Wrap source, target and collected data into a Clojure map and return it."
[source target]
(let [mf (-> source
- microformats/parse
+ mf-parser/parse
javamap->map
:items first
javamap->map)