Xposts.tcl

TCL Guest 8 Views Size: 2.52 KB Posted on: Sep 26, 26 @ 12:50 AM
  1. # xposts.tcl
  2. # grabs summary from x.com links using fxtwitter API
  3.  
  4. package require json  ;#tcllib
  5.  
  6. # Channel flag
  7. setudef flag enableX
  8.  
  9. # Binds
  10. bind pubm - "*://x.com/*" x_grab_proc
  11. bind pubm - "*://twitter.com/*" x_grab_proc
  12.  
  13. proc x_grab_proc {nick host hand chan text} {
  14.     if {![channel get $chan enableX]} { return }
  15.    
  16.     putlog "Processing X link in $chan from $nick: $text"
  17.    
  18.     # Match URL
  19.     if {![regexp {https?://(x|twitter)\.com/([^/]+)/status/(\d+)} $text match domain user id]} {
  20.         putlog "No matching status URL found in: $text"
  21.         return
  22.     }
  23.    
  24.     putlog "Matched URL: domain=$domain user=$user id=$id"
  25.    
  26.     set api_url "https://api.fxtwitter.com/${user}/status/${id}"
  27.    
  28.     # Fetch with curl
  29.     if {[catch {set raw_data [exec /usr/bin/curl -s -m 10 $api_url]} curl_err]} {
  30.         putlog "Curl fetch failed for $api_url: $curl_err"
  31.         return
  32.     }
  33.    
  34.     putlog "Curl succeeded for $api_url - Raw data length: [string length $raw_data]"
  35.    
  36.     # Parse JSON
  37.     if {[catch {set dict [::json::json2dict $raw_data]} json_err]} {
  38.         putlog "JSON parse error for $api_url: $json_err - Raw data: [string range $raw_data 0 200]"
  39.         return
  40.     }
  41.    
  42.     putlog "JSON parsed successfully - Keys: [dict keys $dict]"
  43.    
  44.     # Check for API error
  45.     if {[dict exists $dict code] && [dict get $dict code] != 200} {
  46.         putlog "API error for $api_url: code [dict get $dict code] - message [dict get $dict message]"
  47.         return
  48.     }
  49.    
  50.     # Extract fields
  51.     if {![dict exists $dict tweet]} {
  52.         putlog "No 'tweet' key in JSON response for $api_url"
  53.         return
  54.     }
  55.    
  56.     set tweet_dict [dict get $dict tweet]
  57.     set author [dict get $tweet_dict author name]
  58.     set raw_ts [dict get $tweet_dict created_at]
  59.     if {[catch {
  60.         set epoch [clock scan $raw_ts -format "%a %b %d %H:%M:%S %z %Y"]
  61.         set timestamp [clock format $epoch -format "%d %b %Y %H:%M:%S %Z"]
  62.     } ts_err]} {
  63.         putlog "Timestamp parse error: $ts_err - falling back to raw"
  64.         set timestamp $raw_ts
  65.     }
  66.     set text [dict get $tweet_dict text]
  67.    
  68.     # Remove newlines, trim, and strip unicode
  69.     regsub -all {\n} $text " " text
  70.     set text [string trim $text]
  71.     regsub -all {[^\x00-\x7F]} $text "" text  ;
  72.    
  73.     # Output
  74.     putserv "PRIVMSG $chan :X Post by $author, posted on $timestamp - $text"
  75.    
  76.     putlog "Successfully output summary for id $id"
  77. }
  78.  
  79. putlog "xposts.tcl loaded.  Use .chanset #chan +enableX"

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.