shrub/lib/twitter.hoon

148 lines
3.9 KiB
Plaintext
Raw Normal View History

:: A Twitter API library.
::
:::: /hoon/twitter/lib
::
/? 314
2016-03-08 00:42:23 +03:00
/- twitter
=+ sur-twit:^twitter :: XX
!:
:::: functions
::
|%
++ fass :: rewrite path
2016-03-08 00:42:23 +03:00
|= a/path
2016-03-09 06:49:58 +03:00
%+ turn a
|=(b/@t (gsub '-' '_' b))
::
++ gsub :: replace chars
2016-03-08 00:42:23 +03:00
|= {a/@t b/@t t/@t}
^- @t
2016-03-08 00:42:23 +03:00
?: =('' t) t
%+ mix (lsh 3 1 $(t (rsh 3 1 t)))
=+ c=(end 3 1 t)
?:(=(a c) b c)
::
2016-03-09 07:25:44 +03:00
++ join
|= {a/char b/(list @t)} ^- @t
%+ rap 3
?~ b ~
|-(?~(t.b b [i.b a $(b t.b)]))
::
2016-03-09 05:44:43 +03:00
++ interpolate-some :: [/a/:b/c [d+'bar' b+'foo']~] -> [/a/foo/c [d+'bar']~]
|= {pax/path quy/quay} ^- {path quay}
=+ ^= inline :: required names
%- ~(gas in *(set term))
(murn pax replacable:interpolate-path)
=^ inter quy
(skid quy |=({a/knot @} (~(has in inline) a)))
[(interpolate-path pax inter) quy]
::
++ interpolate-path :: [/a/:b/c [%b 'foo']~] -> /a/foo/c
=+ replacable=|=(a/knot `(unit term)`(rush a ;~(pfix col sym)))
|= {a/path b/(list (pair term knot))} ^- path
?~ a ?~(b ~ ~|(unused-values+b !!))
=+ (replacable i.a)
?~ - [i.a $(a t.a)] :: literal value
?~ b ~|(no-value+u !!)
?. =(u p.i.b) ~|(mismatch+[u p.i.b] !!)
[q.i.b $(a t.a, b t.b)]
::
++ valve :: produce request
2016-03-11 07:37:47 +03:00
|= {med/?($get $post) pax/path quy/quay}
^- hiss
2016-03-09 23:52:57 +03:00
=+ url=(scan "https://api.twitter.com/1.1/.json" auri:epur) :: base path
2016-03-09 06:49:58 +03:00
=. q.q.url (welp q.q.url pax)
2016-03-09 23:52:57 +03:00
=. r.url quy
^- hiss
2016-03-09 06:49:58 +03:00
?- med
2016-03-09 23:52:57 +03:00
$get [url med *math ~]
2016-03-09 06:49:58 +03:00
$post
2016-03-11 07:37:47 +03:00
=+ hed=(my content-type+['application/x-www-form-urlencoded']~ ~)
2016-03-09 23:52:57 +03:00
[url(r ~) med hed (some (tact +:(tail:earn r.url)))]
2016-03-09 06:49:58 +03:00
==
::
++ find-req
=+ all=doc-data-dry:reqs
|= a/_-:*endpoint:reqs ^- {?($get $post) path}
?~ all ~|(endpoint-lost+a !!) :: type error, should never happen
?: =(a -:*typ.i.all)
+.i.all
$(all t.all)
--
!:
:::: library
::
|%
2016-03-09 07:32:45 +03:00
++ render :: response printers
|%
++ mean
2016-03-08 04:38:01 +03:00
|= {msg/@t num/@ud} ^- tank
rose+[": " `~]^~[leaf+"Error {<num>}" leaf+(trip msg)]
--
2016-03-09 07:32:45 +03:00
++ parse :: json reparsers
|%
2016-03-12 03:06:47 +03:00
++ ce |*({a/_* b/fist:jo} (cu:jo |=(c/a c) b)) :: output type
2016-03-08 04:38:01 +03:00
++ fasp |*(a/{@tas *} [(gsub '-' '_' -.a) +.a]) :: XX usable electroplating
++ user (cook crip (plus ;~(pose aln cab)))
2016-03-08 00:42:23 +03:00
++ mean (ot errors+(ar (ot message+so code+ni ~)) ~):jo
++ stat
=+ jo
2016-03-12 03:06:47 +03:00
%+ ce {id+@u who+@ta now+@da txt+@t}
%- ot
2016-03-08 00:42:23 +03:00
:~ id+ni
user+(ot (fasp screen-name+(su user)) ~)
(fasp created-at+da)
text+so
==
2016-03-08 04:38:01 +03:00
++ usel
=+ jo
2016-03-12 03:06:47 +03:00
%+ ce (list who/@ta)
2016-03-08 00:42:23 +03:00
=- (ot users+(ar -) ~)
(ot (fasp screen-name+(su user)) ~)
--
++ request
=< apex
=+ args:reqs
2016-03-11 07:37:47 +03:00
|%
++ apex
|= {a/endpoint b/quay}
=+ [med pax]=(find-req -.a)
(valve med (cowl pax +.a b))
::
2016-03-09 07:25:44 +03:00
++ lutt |=(@u `@t`(rsh 3 2 (scot %ui +<)))
++ llsc
:: => args:reqs
2016-03-09 07:25:44 +03:00
|= a/$@(scr (list scr)) ^- @t
?@(a `@t`a (join ',' a))
::
++ llst
2016-03-09 07:25:44 +03:00
|= a/$@(@t (list @t)) ^- @t
?@(a `@t`a (join ',' a))
::
++ llid
:: =+ args:reqs
2016-03-09 07:25:44 +03:00
|= a/$@(tid (list tid)) ^- @t
?~ a ~|(%nil-id !!)
?@(a (lutt a) (join ',' (turn `(list tid)`a lutt)))
::
++ cowl :: handle parameters
2016-03-08 00:42:23 +03:00
|= $: pax/path
2016-03-09 07:25:44 +03:00
ban/(list param)
2016-03-08 00:42:23 +03:00
quy/quay
==
2016-03-09 07:25:44 +03:00
^- {path quay}
%+ interpolate-some (fass pax)
=- (weld - quy)
%+ turn ban
|= p/param
2016-03-08 04:38:01 +03:00
^- {@t @t}
2016-03-09 07:25:44 +03:00
:- (gsub '-' '_' -.p)
?+ -.p p.p :: usually plain text
?($source-id $target-id) (lutt p.p)
?($follow $id $name $user-id) (llid p.p)
$track (llst p.p)
$screen-name (llsc p.p)
==
--
--