Skip to content

Commit

Permalink
[pmonks#6] A common file for compatible code
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilio Silva Schlenker committed May 20, 2019
1 parent ca1c388 commit 261b70f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 80 deletions.
89 changes: 9 additions & 80 deletions src/unfurl/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,8 @@
(:require [clojure.string :as s]
[clj-http.client :as http]
[hickory.core :as hc]
[hickory.select :as hs]))

(defn- strip-nil-values
"Strips entries with nil values from map m."
[m]
(apply dissoc
m
(for [[k v] m :when (nil? v)] k)))

; See http://oembed.com/
(defn- unfurl-oembed
[url]
;####TODO: implement this
nil)

(defn- meta-tag-name
[meta-tag]
(if-let [meta-tag-name (:name meta-tag)]
meta-tag-name
(:property meta-tag)))

(defn- meta-tag-value
[meta-tags tag-name]
(let [value (first (map :content
(filter #(= tag-name (meta-tag-name %))
(map :attrs meta-tags))))
value (when value (s/trim value))]
(when (pos? (count value))
value)))

(defn- unfurl-html
[title-tags meta-tags]
(strip-nil-values {
:title (first (:content (first title-tags)))
:description (meta-tag-value meta-tags "description")
}))

; See https://getstarted.sailthru.com/site/horizon-overview/horizon-meta-tags/
(defn- unfurl-sailthru
[meta-tags]
(strip-nil-values {
:title (meta-tag-value meta-tags "sailthru.title")
:description (meta-tag-value meta-tags "sailthru.description")
:preview-url (meta-tag-value meta-tags "sailthru.image.full")
}))

; See https://swiftype.com/documentation/meta_tags
(defn- unfurl-swiftype
[meta-tags]
(strip-nil-values {
:title (meta-tag-value meta-tags "st:title")
:preview-url (meta-tag-value meta-tags "st:image")
}))

; See https://dev.twitter.com/cards/markup
(defn- unfurl-twitter
[meta-tags]
(strip-nil-values {
:url (meta-tag-value meta-tags "twitter:url")
:title (meta-tag-value meta-tags "twitter:title")
:description (meta-tag-value meta-tags "twitter:description")
:preview-url (meta-tag-value meta-tags "twitter:image")
}))

; See http://ogp.me/
(defn- unfurl-opengraph
[meta-tags]
(strip-nil-values {
:url (meta-tag-value meta-tags "og:url")
:title (meta-tag-value meta-tags "og:title")
:description (meta-tag-value meta-tags "og:description")
:preview-url (meta-tag-value meta-tags "og:image")
}))
[hickory.select :as hs]
[unfurl.common :as co]))

(defn- http-get
"'Friendly' form of http/get that adds request information to any exceptions that get thrown by clj-http."
Expand Down Expand Up @@ -141,10 +70,10 @@
proxy-port nil }}]
(if url
; Use oembed services first, and then fallback if it's not supported for the given URL
(if-let [oembed-data (unfurl-oembed url)]
(if-let [oembed-data (co/unfurl-oembed url)]
oembed-data
(let [request { :url url
:options (strip-nil-values { :accept :html
:options (co/strip-nil-values { :accept :html
:follow-redirects follow-redirects
:socket-timeout timeout-ms
:conn-timeout timeout-ms
Expand All @@ -162,8 +91,8 @@
title-tags (hs/select (hs/descendant (hs/tag :title)) parsed-body)
meta-tags (hs/select (hs/descendant (hs/tag :meta)) parsed-body)]
(if meta-tags
(merge (unfurl-html title-tags meta-tags)
(unfurl-sailthru meta-tags)
(unfurl-swiftype meta-tags)
(unfurl-twitter meta-tags)
(unfurl-opengraph meta-tags)))))))))
(merge (co/unfurl-html title-tags meta-tags)
(co/unfurl-sailthru meta-tags)
(co/unfurl-swiftype meta-tags)
(co/unfurl-twitter meta-tags)
(co/unfurl-opengraph meta-tags)))))))))
92 changes: 92 additions & 0 deletions src/unfurl/common.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
;
; Copyright © 2016 Peter Monks
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;

(ns unfurl.common
(:require [clojure.string :as s]
[hickory.core :as hc]
[hickory.select :as hs]))

(defn strip-nil-values
"Strips entries with nil values from map m."
[m]
(apply dissoc
m
(for [[k v] m :when (nil? v)] k)))

; See http://oembed.com/
(defn unfurl-oembed
[url]
;####TODO: implement this
nil)

(defn- meta-tag-name
[meta-tag]
(if-let [meta-tag-name (:name meta-tag)]
meta-tag-name
(:property meta-tag)))

(defn- meta-tag-value
[meta-tags tag-name]
(let [value (first (map :content
(filter #(= tag-name (meta-tag-name %))
(map :attrs meta-tags))))
value (when value (s/trim value))]
(when (pos? (count value))
value)))

(defn unfurl-html
[title-tags meta-tags]
(strip-nil-values {
:title (first (:content (first title-tags)))
:description (meta-tag-value meta-tags "description")
}))

; See https://getstarted.sailthru.com/site/horizon-overview/horizon-meta-tags/
(defn unfurl-sailthru
[meta-tags]
(strip-nil-values {
:title (meta-tag-value meta-tags "sailthru.title")
:description (meta-tag-value meta-tags "sailthru.description")
:preview-url (meta-tag-value meta-tags "sailthru.image.full")
}))

; See https://swiftype.com/documentation/meta_tags
(defn unfurl-swiftype
[meta-tags]
(strip-nil-values {
:title (meta-tag-value meta-tags "st:title")
:preview-url (meta-tag-value meta-tags "st:image")
}))

; See https://dev.twitter.com/cards/markup
(defn unfurl-twitter
[meta-tags]
(strip-nil-values {
:url (meta-tag-value meta-tags "twitter:url")
:title (meta-tag-value meta-tags "twitter:title")
:description (meta-tag-value meta-tags "twitter:description")
:preview-url (meta-tag-value meta-tags "twitter:image")
}))

; See http://ogp.me/
(defn unfurl-opengraph
[meta-tags]
(strip-nil-values {
:url (meta-tag-value meta-tags "og:url")
:title (meta-tag-value meta-tags "og:title")
:description (meta-tag-value meta-tags "og:description")
:preview-url (meta-tag-value meta-tags "og:image")
}))

0 comments on commit 261b70f

Please sign in to comment.