Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.25 KB | None | 0 0
  1. proc get_title {url} {
  2.   global redirections
  3.   if {$redirections > 10} {set redirections 0; return "FUCK YOU!@%$&^!*#"}
  4.   if {[catch {set token [::http::geturl "$url" ]}]} {
  5.    putlog "Couldn't connect to $url"
  6.    return ""
  7.   }
  8.   upvar #0 $token state
  9.   array set header $state(meta)
  10.   if {![regexp {(.*)([2][0-9][0-9])(.*)} $state(http)]} {
  11.    if {[info exists header(Location)] && [regexp {(.*)([3][0-9][0-9])(.*)} $state(http)]} {
  12.     incr redirections
  13.     if {[regexp {^/} $header(Location)]} {
  14.      regsub {(http[s]?://[^/]+)(.*)} $url {\1} newurl
  15.      append newurl $header(Location)
  16.     } else {
  17.      set newurl "$header(Location)"
  18.     }
  19.     putlog "Redirecting $url to $newurl"
  20.     ::http::cleanup $token
  21.     return [get_title $newurl]
  22.    } else {
  23.     ::http::cleanup $token
  24.     return ""
  25.    }
  26.   }
  27.   set redirections 0
  28.   set charset [regexp -inline -nocase -- $state(charset) [encoding names]]
  29.   if {$charset == ""} {set charset utf-8}
  30.   set body [encoding convertto $charset $state(body)]
  31.   set tweetregexp {<div class=["]tweet-text js-tweet-text["]>(.+?)</div>}
  32.  
  33.   if {![regexp -nocase {.*</title>.*} $body]} {::http::cleanup $token; return ""}
  34.   set title [lindex [regexp -inline -nocase -- {.*<title[^.]*>(.*)</title>} $body] 1]
  35.   if {[regexp {http[s]?:\/\/(www\.)?twitter\.com} $url]} {
  36.    if {[regexp -nocase $tweetregexp $body]} {
  37.     set username [regexp -inline -- {<a class='tweet-screen-name.+?title='(.+?)'>(.+?)</a>} $body]
  38.    set title "[lindex $username 1] ([lindex $username 2])"
  39.    set title [regsub {:.+?\.\.\.:} [format "%s: %s" $title [encoding convertto utf-8 [lindex [regexp -all -nocase -inline -- $tweetregexp $body] 1]]] {:}]
  40.    regsub -nocase -all {<a [^>]+?[^>]*>([^<>]+?)</a>} $title {\1} title
  41.   }
  42.   if {[regexp -nocase {.*<span class="entry-content">(.+?)</span>.*} $body]} {
  43.    set title [regsub {:.+?\.\.\.:} [format "%s: %s" $title [encoding convertto utf-8 [lindex [regexp -all -nocase -inline -- {<span class="entry-content">(.+?)</span>} $body] 1]]] {:}]
  44.    regsub -nocase -all {<a [^>]+?[^>]*>([^<>]+?)</a>} $title {\1} title
  45.   }
  46.  
  47.  }
  48.  regsub -all {\n|\t} $title {} title
  49.  regsub -all {  +} $title { } title
  50.  regsub -all {[\[\]]} $title {} title
  51.  ::http::cleanup $token
  52.  return $title
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement