This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

abcnews

Old posts that have not been replied to for several years.
Locked
d
drewila

abcnews

Post by drewila »

I love this script but for some reason it stops parsing news after a few hours. No errors, nothing, it just stops, as if the script was unloaded. The only way to make it work again is by .restart. A friend said it may be because of a utimer problem?

I don't know anything about TCL scripting, so if anybody could please hint on what may be wrong, I'd appreciate it a lot. :)

abcnews.tcl:

Code: Select all

#############################################################
### ABCNews Script v1.1 by ThEdGE
### 
### This will echo all the new headlines from abcnews.com in your channel,
### also the ability to get the last 5 headlines with a trigger.
###
### V1.1:
###  Cleaned up some code, and made some cosmetic changes.
###  Fixed problem with regsub not being compatible.
###  Changed the HTTP Request header.
#############################################################

### Options, read the discriptions carefully, 
### leave default if you dont know sure what to put!

### Set the channel(s) where the script will function.
set abcchans {
 "#mychatchannel"
}


### Set the trigger for the last 5 headlines.
## Default: !showlast
set lasttrigger "!showlast"


### Set the help msg trigger for the last 5 headlines.
## Default: !abchelp
set helptrigger "!abchelp"


### Set the update interval for the news announcing,
### please note that one update is about 25kb,
### so a interval of 3mins is about 12MB a day.
### A interval around 10mins will skip news postings!
## Default: 3
set interval "3"


### Set a file for the script to use for data storage.
## Default: scripts/abcnews.data
set datafile "scripts/abcnews.data"

### Here you can select what part of the world you want the script to announce headlines for.
###	Available options are: 
###				af : Africa
###				as : Asia
###				er : Europe
###				la : Latin America
###				me : Middle East
###				na : North America
###				wo : The World
## Default: wo
set part "wo"


### Dont touch the code below, or it will fux0r :)
#############################################################
#############################################################

bind PUB - $lasttrigger abclast
bind PUB - $helptrigger abchelp


### Update procedure.
proc abcupdate {} {
global part datafile

 ### Reset some variables.
 set oldhash ""
 set newhash ""
 set raw ""
 set hl ""
 set url ""
 set title ""
 set time ""
 set partline ""

 ### Check what part of the world is configured.
 if {$part != "wo"} {
  set partline "$part/index.html"
 } else {
  set partline "index.html"
 }

 ### Send HTTP header to ABC and read from the socket.
 if {[catch { set socket [socket "abcnews.go.com" 80] } error]} {
  putlog "ABCNews ERROR: cant open socket!"
  return
 }
 fconfigure $socket -buffering line -blocking 0

 puts -nonewline $socket "GET /wire/World/$partline HTTP/1.1\n"
 puts -nonewline $socket "Connection: Keep-Alive\n"
 puts -nonewline $socket "Host: http://abcnews.go.com\n\n"
 puts -nonewline $socket "Pragma: no-cache\n\n"



 while {![eof $socket]} {
  set line [gets $socket]

  ### Check if line aint empty.
  if {$line != ""} {

   ### Check for the pattern of the first headline and format it.
   if {[regexp {(black9pt).*(</div></TD>)} $line rline]} {
    # url
    regexp {(<a href=\").*(\" )} $rline url
    regsub "<a href=\"" $url " " url
    regsub "\" " $url " " url
    set url [string trim $url]
    # title
    regexp {(class=\"blueSmall\">).*(</a>)} $rline title
    regsub "class=\"blueSmall\">" $title " " title
    regsub "</a>" $title " " title
    set title [string trim $title]
    #time
    regexp {(  ).*(</div>)} $rline time
    regsub "  " $time " " time
    regsub "</div>" $time " " time
    set time [string trim $time]

    ### Get the old hash from the datafile
    set f [open $datafile r]
    fconfigure $f -blocking 0
    set oldhash [gets $f]
    close $f

    set newhash [md5 $title$url$time]

    ### Compair the both hashes, announce news if they differ and create new datafile.
    if {$newhash != $oldhash} {
     abcmsg "\[\002ABCNews\002\] $title \037(\037\037http://abcnews.go.com$url\037\037)\037"
    }
     set f [open $datafile w]
     fconfigure $f -blocking 0
     puts $f "$newhash"
     close $f

     ### Close/flush the socket and start the new timer.
     starttimer
     flush $socket
     close $socket

     return
     # end if
    }
    # end if
   }
   # end while
  }

    ### Close/flush the socket and start the new timer, altho this should not be needed.
    starttimer
    flush $socket
    close $socket

 # end proc
}

### Show last headlines procedure.
proc abclast {nick uhost handle chan arg} {
global abcchans

 ### Check for the proper channel.
 set properchan "0"
 set chan [string tolower $chan]
 foreach channel $abcchans {
 set channel [string tolower $channel]
  if {$chan==$channel} {
   set properchan "1"  
  }
 }
 if {$properchan=="0"} {
  return
 }

 ### Reset some variables.
 set partname ""
 set partline ""
 
 ### Check for arguments and format.
 if {$arg == "" || $arg!="af" && $arg!="as" && $arg!="er" && $arg!="la" && $arg!="me" && $arg!="na"} {
  set partline "index.html"
 } else {
  set partline "$arg/index.html"
 }
 
 ### Format the part of the world to request.
 if {$arg=="af"} { set partname "Africa" 	}
 if {$arg=="as"} { set partname "Asia" 		}
 if {$arg=="er"} { set partname "Europe" 	}
 if {$arg=="la"} { set partname "Latin America" }
 if {$arg=="me"} { set partname "Middle East" 	}
 if {$arg=="na"} { set partname "North America" }
 if {$arg== "" } { set partname "The World" 	}

 if {$partname==""} { set partname "The World" }

 ### Send HTTP header to ABC and read from the socket.
 if {[catch { set socket [socket "abcnews.go.com" 80] } error]} {
  putlog "ABCNews ERROR: cant open socket!"
  return
 }
 fconfigure $socket -buffering line -blocking 0

 puts -nonewline $socket "GET /wire/World/$partline HTTP/1.1\n"
 puts -nonewline $socket "Connection: Keep-Alive\n"
 puts -nonewline $socket "Host: http://abcnews.go.com\n"
 puts -nonewline $socket "Pragma: no-cache\n\n"

 set nr 0
 puthelp "NOTICE $nick : Last \0025\002 headlines from \002ABCNews\002 for \002$partname\002 ->"
 while {![eof $socket]} {
  set line [gets $socket]
  ### Check if line aint empty.
  if {$line != ""} {

   ### Check each line for the pattern of the first headline and format it.
   if {[regexp {(black9pt).*(</div></TD>)} $line rline]} {
    # url
    regexp {(<a href=\").*(\" )} $rline url
    regsub "<a href=\"" $url " " url
    regsub "\" " $url " " url
    set url [string trim $url]
    # title
    regexp {(class=\"blueSmall\">).*(</a>)} $rline title
    regsub "class=\"blueSmall\">" $title " " title
    regsub "</a>" $title " " title
    set title [string trim $title]
    #time
    regexp {(  ).*(</div>)} $rline time
    regsub "  " $time " " time
    regsub "</div>" $time " " time
    set time [string trim $time]

    ### Check the length of the time, if its 7 chars fill it up with a 0.
    set len [string length $time]
    if {$len == "7"} {
     set time "0$time"
    }

    incr nr
    puthelp "NOTICE $nick : \002\[\002$nr \002@\002 $time\002\]\002 $title \037(\037\037http://abcnews.go.com$url\037\037)\037"

    ### If the end is reached Close/flush the socket.
    if {$nr == "5"} {
     flush $socket
     close $socket
     return
     # close if
    }
    # close if
   }
   # close if
  }
  # close while
 }

 ### Close/flush the socket, altho this should not be needed!
 flush $socket
 close $socket
 return

 # close proc 
}

### Procedure which will show the user a bit of info.
proc abchelp {nick uhost handle chan arg} {
global abcchans lasttrigger

 ### Check for the proper channel.
 set properchan "0"
 set chan [string tolower $chan]
 foreach channel $abcchans {
 set channel [string tolower $channel]
  if {$chan==$channel} {
   set properchan "1"  
  }
 }
 if {$properchan=="0"} {
  return
 }

 puthelp "PRIVMSG $nick : \002ABCNews\002 v\0021.0\002 by \002ThEdGE\002 (Help) ->"
 puthelp "PRIVMSG $nick : Use \002$lasttrigger\002 <letters> to get the bot notice you the headlines."
 puthelp "PRIVMSG $nick : Available parts of the world:"
 puthelp "PRIVMSG $nick : \002af\002 : Africa"
 puthelp "PRIVMSG $nick : \002as\002 : Asia"
 puthelp "PRIVMSG $nick : \002er\002 : Europe"
 puthelp "PRIVMSG $nick : \002la\002 : Latin America"
 puthelp "PRIVMSG $nick : \002me\002 : Middle East"
 puthelp "PRIVMSG $nick : \002na\002 : North America"
 puthelp "PRIVMSG $nick : \002wo\002 : The World"
 puthelp "PRIVMSG $nick : For example \"\002$lasttrigger er\002\" to get the headlines for Europe."
}

### Procedure which handles the timers.
proc starttimer {} {
global interval

 ### check if timer is already started, maybe rehashing?
 set timerstarted "0"
 foreach timer [timers] {
  if {[regexp {(abcupdate).*(timer)} $timer temp]} {
   set timerstarted "1"
  } 
 }

 ### Start new timer if not already started.
 if {$timerstarted == "0"} {
  timer $interval abcupdate
 }
}

### Procedure which will echo the final text to the channel.
proc abcmsg {text} {
global abcchans
 foreach chan $abcchans {
  putserv "PRIVMSG $chan : $text"
 }
}

### Start the timer when the script is loaded.
starttimer

    ### Check if datafile exists, create if not found.
    if {[catch { open $datafile r } error]} { 
     set fnew [open $datafile w]
     fconfigure $fnew -blocking 0
     close $fnew
    }

putlog "Loaded: ABCNews script by ThEdGE"
User avatar
Yourname
Master
Posts: 358
Joined: Mon Sep 24, 2001 8:00 pm
Location: Toronto

Post by Yourname »

:evil:
Dormant egghead.
Locked