urbit/lib/twitter.hoon

151 lines
3.5 KiB
Plaintext
Raw Normal View History

:: A Twitter API library.
::
:::: /hoon/twitter/lib
::
/? 314
2016-03-08 00:42:23 +03:00
/- twitter
2016-04-07 23:07:21 +03:00
/+ interpolate, hep-to-cab
2016-03-08 00:42:23 +03:00
=+ sur-twit:^twitter :: XX
2016-11-08 04:40:00 +03:00
=, ^eyre
2016-11-01 03:12:56 +03:00
=, bytes:eyre
=, js:eyre
2016-11-08 04:40:00 +03:00
=, url:eyre
2016-11-01 03:12:56 +03:00
=, xml:eyre
!:
:::: functions
::
|%
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)]))
::
++ 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']~ ~)
[url(r ~) med hed ?~(r.url ~ (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
2016-04-01 02:15:10 +03:00
=+ args:reqs
|%
++ mean
2016-03-08 04:38:01 +03:00
|= {msg/@t num/@ud} ^- tank
rose+[": " `~]^~[leaf+"Error {<num>}" leaf+(trip msg)]
2016-04-01 02:15:10 +03:00
::
++ user-url
2016-04-01 03:39:48 +03:00
|= a/scr ^- purf
:_ ~
%^ into-url:interpolate 'https://twitter.com/:scr'
~
~[scr+a]
2016-04-01 02:15:10 +03:00
::
++ post-url
|= {a/scr b/tid} ^- purf
:_ ~
%^ into-url:interpolate 'https://twitter.com/:scr/status/:tid'
~
2016-04-01 02:15:10 +03:00
~[scr+a tid+(tid:print b)]
--
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-10-24 20:36:01 +03:00
++ fasp |*(a/{@tas *} [(hep-to-cab -.a) +.a])
2016-11-11 21:32:50 +03:00
++ usor (cook crip (plus ;~(pose aln cab)))
2016-03-08 00:42:23 +03:00
++ mean (ot errors+(ar (ot message+so code+ni ~)) ~):jo
2016-03-21 23:35:29 +03:00
++ post
=+ jo
2016-03-21 23:35:29 +03:00
%+ ce post:sur-twit
%- ot
2016-03-08 00:42:23 +03:00
:~ id+ni
2016-11-11 21:32:50 +03:00
user+(ot (fasp screen-name+(su usor)) ~)
2016-03-08 00:42:23 +03:00
(fasp created-at+da)
2016-11-02 04:43:27 +03:00
text+(cu crip (su (star escp:^parse))) :: parse html escapes
==
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 -) ~)
2016-11-11 21:32:50 +03:00
(ot (fasp screen-name+(su usor)) ~)
--
++ print
=+ args:reqs
|%
++ tid |=(@u `@t`(rsh 3 2 (scot %ui +<)))
++ scr |=(@t +<)
++ lsc
|= a/$@(^scr ^lsc) ^- @t
?@(a `@t`a (join ',' a))
::
++ lid
|= a/$@(^tid (list ^tid)) ^- @t
?~ a ~|(%nil-id !!)
?@(a (tid a) (join ',' (turn `(list ^tid)`a tid)))
--
++ request
=< apex
=+ args:reqs
2016-03-11 07:37:47 +03:00
|%
++ apex
|= {a/endpoint b/quay} ^- hiss
=+ [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}
2016-04-07 23:07:21 +03:00
%+ into-path-partial:interpolate
(path:hep-to-cab pax)
2016-03-09 07:25:44 +03:00
=- (weld - quy)
%+ turn ban
|= p/param
2016-03-08 04:38:01 +03:00
^- {@t @t}
2016-04-07 23:07:21 +03:00
:- (hep-to-cab -.p)
2016-03-09 07:25:44 +03:00
?+ -.p p.p :: usually plain text
?($source-id $target-id) (tid:print p.p)
?($id $name $user-id) (lid:print p.p)
$screen-name (lsc:print p.p)
==
--
--