Skip to content

Commit

Permalink
Add handling for Twitter API’s alt text
Browse files Browse the repository at this point in the history
See #11
  • Loading branch information
joewiz committed Feb 8, 2023
1 parent 594d423 commit 337fa79
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
12 changes: 10 additions & 2 deletions modules/process-tweets.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ declare function pt:photo($text as xs:string, $photo-map as map(*)) {
let $chunks := pt:chop($text, $photo-map?indices)
let $text := $photo-map?display_url
let $url := $photo-map?expanded_url
let $alt :=
if (map:contains($photo-map, "ext_alt_text")) then
if (exists($photo-map?ext_alt_text)) then
attribute alt { $photo-map?ext_alt_text }
else
()
else
()
return
($chunks[1], <a href="{$url}"><img src="{$photo-map?media_url_https}"/></a>, $chunks[3])
($chunks[1], <a href="{$url}"><img src="{$photo-map?media_url_https}"/>{$alt}</a>, $chunks[3])
};

(: apply entities, from last to first; entities must already be in last-to-first order :)
Expand Down Expand Up @@ -92,7 +100,7 @@ declare function pt:apply-entities($text as xs:string, $entities as map(*)*, $se
: so we need to sort them first before applying them :)
declare function pt:process-entities($tweet as map(*)) {
let $text := $tweet?full_text
let $entities-map := map:get($tweet, 'entities')
let $entities-map := map:merge((map:get($tweet, 'extended_entities'), map:get($tweet, 'entities')), map { "duplicates": "use-first" })
let $entities-to-process :=
for $entity-key in map:keys($entities-map)
let $entity := map:get($entities-map, $entity-key)
Expand Down
4 changes: 2 additions & 2 deletions modules/twitter-downloader.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare function twitter-dl:download-last-posts($count as xs:integer?, $max-id a
let $_ := util:log-app('info', 'hsg-twitter', 'Sending Twitter request, count=' || $count || ', maxid=' || $max-id)
let $request-response := twitter:user-timeline(
config:consumer-key(), config:consumer-secret(), config:access-token(), config:access-token-secret(),
(), (), (), $count, $max-id, true(), true(), false(), false())
(), (), (), $count, $max-id, true(), true(), false(), false(), true())

let $request := $request-response[1]
let $response-head := $request-response[2]
Expand Down Expand Up @@ -134,7 +134,7 @@ declare function twitter-dl:download-last-json($max-id as xs:unsignedLong?) {
let $_ := util:log-app('info', 'hsg-twitter', 'Sending Twitter request1 , maxid=' || $max-id)
let $request-response := twitter:user-timeline(
config:consumer-key(), config:consumer-secret(), config:access-token(), config:access-token-secret(),
(), (), (), 1, $max-id, true(), true(), false(), false())
(), (), (), 1, $max-id, true(), true(), false(), false(), true())

let $request := $request-response[1]
let $response-head := $request-response[2]
Expand Down
6 changes: 4 additions & 2 deletions modules/twitter.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ declare function twitter:user-timeline(
$trim-user as xs:boolean?,
$exclude-replies as xs:boolean?,
$contributor-details as xs:boolean?,
$include-rts as xs:boolean?
$include-rts as xs:boolean?,
$include-ext-alt-text as xs:boolean?
) {
let $api-method := '/statuses/user_timeline.json'
let $http-method := 'GET'
Expand All @@ -45,7 +46,8 @@ declare function twitter:user-timeline(
if ($trim-user) then concat('trim_user=', $trim-user) else (),
if ($exclude-replies) then concat('exclude_replies=', $exclude-replies) else (),
if ($contributor-details) then concat('contributor_details=', $contributor-details) else (),
if ($include-rts) then concat('include_rts=', $include-rts) else ()
if ($include-rts) then concat('include_rts=', $include-rts) else (),
if ($include-ext-alt-text) then concat('include_ext_alt_text=', $include-ext-alt-text) else ()
),
'&amp;'
)
Expand Down
6 changes: 4 additions & 2 deletions modules/user-timeline.xq
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let $trim-user := true()
let $exclude-replies := false()
let $contributor-details := false()
let $include-rts := false()
let $include-ext-alt-text := true()
let $request-response :=
twitter:user-timeline(
config:consumer-key(),
Expand All @@ -31,7 +32,8 @@ let $request-response :=
$trim-user,
$exclude-replies,
$contributor-details,
$include-rts
$include-rts,
$include-ext-alt-text
)
return
twitter:echo-response($request-response)
twitter:echo-response($request-response)

0 comments on commit 337fa79

Please sign in to comment.