factor/extra/bitly/bitly.factor

70 lines
1.9 KiB
Factor
Raw Normal View History

2012-09-22 15:54:51 -04:00
! Copyright (C) 2010-2012 Slava Pestov, John Benediktsson.
2010-04-13 01:54:30 -04:00
! See http://factorcode.org/license.txt for BSD license.
2012-09-22 15:54:51 -04:00
USING: assocs http.client json.reader kernel namespaces
sequences urls ;
2016-03-15 03:22:01 -04:00
IN: bitly
2010-04-13 01:54:30 -04:00
2012-09-22 15:54:51 -04:00
SYMBOLS: bitly-api-user bitly-api-key ;
2010-04-13 01:54:30 -04:00
<PRIVATE
2012-09-22 15:54:51 -04:00
: <bitly-url> ( path -- url )
"http://api.bitly.com/v3/" prepend >url
bitly-api-user get "login" set-query-param
bitly-api-key get "apiKey" set-query-param
"json" "format" set-query-param ;
2010-04-13 01:54:30 -04:00
2010-04-13 03:54:36 -04:00
ERROR: bad-response json status ;
2012-09-22 15:54:51 -04:00
: check-status ( json -- json )
2010-04-13 04:43:10 -04:00
dup "status_code" of 200 = [
dup "status_txt" of
bad-response
2010-04-13 03:54:36 -04:00
] unless ;
2012-09-22 15:54:51 -04:00
: json-data ( url -- json )
http-get nip json> check-status "data" of ;
2012-09-22 15:54:51 -04:00
: get-short-url ( short-url path -- data )
<bitly-url> swap "shortUrl" set-query-param json-data ;
: get-long-url ( long-url path -- data )
<bitly-url> swap "longUrl" set-query-param json-data ;
2010-04-13 01:54:30 -04:00
PRIVATE>
: shorten-url ( long-url -- short-url )
2012-09-22 15:54:51 -04:00
"shorten" get-long-url "url" of ;
: expand-url ( short-url -- url )
"expand" get-short-url "expand" of first "long_url" of ;
: valid-user? ( user api-key -- ? )
"validate" <bitly-url>
swap "x_apiKey" set-query-param
swap "x_login" set-query-param
json-data "valid" of 1 = ;
: clicks ( short-url -- clicks )
"clicks" get-short-url "clicks" of first "global_clicks" of ;
: referrers ( short-url -- referrers )
"referrers" get-short-url "referrers" of ;
: countries ( short-url -- countries )
"countries" get-short-url "countries" of ;
: clicks-by-minute ( short-url -- clicks )
"clicks_by_minute" get-short-url "clicks_by_minute" of ;
: clicks-by-day ( short-url -- clicks )
"clicks_by_day" get-short-url "clicks_by_day" of ;
: lookup ( long-urls -- short-urls )
"lookup" <bitly-url>
swap "url" set-query-param
json-data "lookup" of [ "short_url" of ] map ;
: info ( short-url -- title )
"info" get-short-url "info" of first "title" of ;