Skip to content

Commit

Permalink
more goodies
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsz committed Nov 2, 2013
1 parent 641d337 commit b6713ac
Showing 1 changed file with 75 additions and 16 deletions.
91 changes: 75 additions & 16 deletions src/bitly.clj
Expand Up @@ -3,9 +3,8 @@
[clj-http.util :as util]
[clojure.data.json :as json]))

(def ^:dynamic *api-user* nil)
(def ^:dynamic *api-key* nil)
(def ^:dynamic *api-base* "http://api.bitly.com/v3/")
(def ^:dynamic *access-token* nil)
(def ^:dynamic *api-base* "https://api-ssl.bitly.com/v3/")

(defn generate-query-string [params]
"Generate query string allowing for duplicate parameters"
Expand All @@ -31,24 +30,21 @@
[k values]))

(defn- build-request-url [method params]
(if (and *api-user* *api-key*)
(let [base-params ["login" *api-user*
"apiKey" *api-key*
"format" "json"]
(if *access-token*
(let [base-params ["access_token" *access-token*]
base-url *api-base*
full-params (concat base-params params)]
(str base-url method "?"
(generate-query-string full-params)))
(throw (IllegalArgumentException. "Must supply a Bitly API user and key."))))
(throw (IllegalArgumentException. "Must supply a Bitly access token."))))

(defn- request-data [url]
(:data (json/read-str (:body (client/get url)))))
(get (json/read-str (:body (client/get url))) "data"))

(defmacro with-auth
"Sets the API user and API key for Bitly API requests"
[api-user api-key & body]
`(binding [*api-user* ~api-user
*api-key* ~api-key]
[access-token & body]
`(binding [*access-token* ~access-token]
(do
~@body)))

Expand All @@ -75,11 +71,68 @@
(:clicks (request-data request-url)))))


(defn user-clicks []
(let [request-url (build-request-url "user/clicks"
(expand-args "unit" "month"))]
(:clicks (request-data request-url))))
(defn user-clicks
"Returns the aggregate number of clicks on all of the authenticated user's bitly links.
Parameters:
* unit - minute, hour, day, week or month, default: day
Note: when unit is minute the maximum value for units is 60.
* units - an integer representing the time units to query data for. Pass -1 to return all units of time.
* timezone - an integer hour offset from UTC (-14 to 14), or a timezone string default: America/New_York.
* rollup - true or false. Return data for multiple units rolled up to a single result instead of a separate value for each period of time.
* limit - 1 to 1000 (default=100).
* unit_reference_ts - an epoch timestamp, indicating the most recent time for which to pull metrics, default: now.
Note: the value of unit_reference_ts rounds to the nearest unit.
Note: historical data is stored hourly beyond the most recent 60 minutes. If a unit_reference_ts is specified, unit cannot be minute.
"
[& {:keys [unit units timezone rollup limit unit_reference_ts] :or {unit "day" units "7" timezone "0" rollup "false" limit "100" unit_reference_ts "now"}}]
(let [request-url (build-request-url "user/clicks" ["unit" unit "units" units "timezone" timezone "rollup" rollup "limit" limit "unit_reference_ts" unit_reference_ts])]
(request-data request-url)))


(defn user-popular-links
"Returns the authenticated user's most-clicked bitly links (ordered by number of clicks) in a given time period.
Parameters
unit - minute | hour | day | week | month default:day
Note: when unit is minute the maximum value for units is 60
units = an integer representing the time units to query data for. pass -1 to return all units of time.
timezone - an integer hour offset from UTC (-14..14), or a timezone string default:America/New_York.
limit=1..1000 (default=100)
unit_reference_ts - an epoch timestamp, indicating the most recent time for which to pull metrics. default:now
Note: the value of unit_reference_ts rounds to the nearest unit.
Note: historical data is stored hourly beyond the most recent 60 minutes. If a unit_reference_ts is specified, unit cannot be minute.
"
[& {:keys [unit units timezone limit unit_reference_ts] :or {unit "day" units "7" timezone "0" limit "100" unit_reference_ts "now"}}]
(let [request-url (build-request-url "user/popular_links" ["unit" unit "units" units "timezone" timezone "limit" limit "unit_reference_ts" unit_reference_ts])]
(request-data request-url)))

(defn user-countries
"Returns aggregate metrics about the countries referring click traffic to all of the authenticated user's bitly links." []
(let [request-url (build-request-url "user/countries" nil)]
(request-data request-url)))


(defn user-info []
(let [request-url (build-request-url "user/info" nil)]
(request-data request-url)))

(defn user-link-history []
(let [request-url (build-request-url "user/link_history" nil)]
(request-data request-url)))

(defn user-network-history []
(let [request-url (build-request-url "user/network_history" nil)]
(request-data request-url)))

(defn user-tracking-domain-list []
(let [request-url (build-request-url "user/tracking_domain_list" nil)]
(request-data request-url)))



;; deprecated
(defn clicks-by-minute [short-urls]
(if (and (coll? short-urls) (> (count short-urls) 15))
(throw (IllegalArgumentException.
Expand All @@ -88,6 +141,7 @@
(expand-args "shortUrl" short-urls))]
(:clicks_by_minute (request-data request-url)))))

;; deprecated
(defn clicks-by-day [short-urls]
(if (and (coll? short-urls) (> (count short-urls) 15))
(throw (IllegalArgumentException.
Expand All @@ -100,6 +154,7 @@
(let [request-url (build-request-url "bitly_pro_domain" ["domain" domain])]
(request-data request-url)))

;; deprecated
(defn lookup [long-urls]
(if (and (coll? long-urls) (> (count long-urls) 15))
(throw
Expand All @@ -113,3 +168,7 @@
(defn info [short-url]
(let [request-url (build-request-url "info" ["shortUrl" short-url])]
(request-data request-url)))

(defn expand [short-url]
(let [request-url (build-request-url "expand" ["shortUrl" short-url])]
(request-data request-url)))

0 comments on commit b6713ac

Please sign in to comment.