# xposts.tcl
# grabs summary from x.com links using fxtwitter API
package require json ;#tcllib
# Channel flag
setudef flag enableX
# Binds
bind pubm - "*://x.com/*" x_grab_proc
bind pubm - "*://twitter.com/*" x_grab_proc
proc x_grab_proc {nick host hand chan text} {
if {![channel get $chan enableX]} { return }
putlog "Processing X link in $chan from $nick: $text"
# Match URL
if {![regexp {https?://(x|twitter)\.com/([^/]+)/status/(\d+)} $text match domain user id]} {
putlog "No matching status URL found in: $text"
return
}
putlog "Matched URL: domain=$domain user=$user id=$id"
set api_url "https://api.fxtwitter.com/${user}/status/${id}"
# Fetch with curl
if {[catch {set raw_data [exec /usr/bin/curl -s -m 10 $api_url]} curl_err]} {
putlog "Curl fetch failed for $api_url: $curl_err"
return
}
putlog "Curl succeeded for $api_url - Raw data length: [string length $raw_data]"
# Parse JSON
if {[catch {set dict [::json::json2dict $raw_data]} json_err]} {
putlog "JSON parse error for $api_url: $json_err - Raw data: [string range $raw_data 0 200]"
return
}
putlog "JSON parsed successfully - Keys: [dict keys $dict]"
# Check for API error
if {[dict exists $dict code] && [dict get $dict code] != 200} {
putlog "API error for $api_url: code [dict get $dict code] - message [dict get $dict message]"
return
}
# Extract fields
if {![dict exists $dict tweet]} {
putlog "No 'tweet' key in JSON response for $api_url"
return
}
set tweet_dict [dict get $dict tweet]
set author [dict get $tweet_dict author name]
set raw_ts [dict get $tweet_dict created_at]
if {[catch {
set epoch [clock scan $raw_ts -format "%a %b %d %H:%M:%S %z %Y"]
set timestamp [clock format $epoch -format "%d %b %Y %H:%M:%S %Z"]
} ts_err]} {
putlog "Timestamp parse error: $ts_err - falling back to raw"
set timestamp $raw_ts
}
set text [dict get $tweet_dict text]
# Remove newlines, trim, and strip unicode
regsub -all {\n} $text " " text
set text [string trim $text]
regsub -all {[^\x00-\x7F]} $text "" text ;
# Output
putserv "PRIVMSG $chan :X Post by $author, posted on $timestamp - $text"
putlog "Successfully output summary for id $id"
}
putlog "xposts.tcl loaded. Use .chanset #chan +enableX"