Martin0815

Get URLs for multi-photo or animated GIF data from Twitter

Aug 2nd, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.68 KB | None | 0 0
  1. package require http;
  2. package require tls;
  3.  
  4. http::register https 443 ::tls::socket;
  5.  
  6. # get the HTML page respecting the redirections
  7. set ncode 0;
  8.  
  9. while {$ncode == 0 || ($ncode >= 300 && $ncode < 400)} {
  10.     if {[catch {set t [http::geturl $url];} reason options] == 1} {
  11.         return -options $options $reason;
  12.     }
  13.  
  14.     set ncode [http::ncode $t];
  15.     set meta  [http::meta $t];
  16.     if {$ncode >= 300 && $ncode < 400} {
  17.         set url [dict get $meta location];
  18.         http::cleanup $t;
  19.     }
  20. }
  21.  
  22. set encoding [lindex [split [split [dict get $meta content-type] ";"] "="] end]
  23. set data     [http::data $t];
  24.  
  25. http::cleanup $t;
  26.  
  27. # check for a multi-photo or animated-gif HTML page
  28. set multiPhotoNodes [regexp -inline -all {<\w+\s+(?:\w+="[^\"]*"\s+)*class="(?:(?:\w+(?:\s|-))*)(?:multi-photo\s+photo-\d+)(?:(?:(?:\s|-)\w+)*)"(?:[^>]+)>\s*<img\s+(?:\w+="[^\"]*"\s+)*src="([^\"]+)"([^>]+)>} $data];
  29. if {[llength $multiPhotoNodes]} {
  30.     foreach {whole imgUrl} $multiPhotoNodes {
  31.         # do something with the image URL
  32.     }
  33. } else {
  34.     set animatedGifodes [regexp -inline -all {(?:<img\s+(?:(?:\w+="[^\"]*"\s+)*)src="([^\"]+)"\s+(?:\w+="[^\"]*"\s)*class="\w*(?:\s|-)?animated-gif(?:\s|-)?\w*")|(?:<video\s+(?:\w+="[^\"]*"\s)*class="\w*(?:\s|-)?animated-gif(?:\s|-)?\w*"\s*[^>]*>\s+<source\s+(?:(?:\w+="[^\"]*"\s+)*)video-src="([^\"]+)")} $data];
  35.     if {[llength $animatiedGifNodes] == 4} {
  36.         foreach {whole url} $animatedGifodes {
  37.             if {[string match {<img*} $whole]} {
  38.                 # process the thumbnail image URL
  39.             } else {
  40.                 # process the animated GIF video URL
  41.             }
  42.         }
  43.     }
  44. } else {
  45.     # ...
  46. }
Advertisement