view feeds.tcl @ 154:7a549487fba3

feeds: Cleanups, minor bugfix in feed update checking.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Jun 2014 18:46:33 +0300
parents 7106dd8db4de
children 0ff78e418e77
line wrap: on
line source

##########################################################################
#
# FeedCheck v0.8 by Matti 'ccr' Hamalainen <ccr@tnsp.org>
# (C) Copyright 2008-2013 Tecnic Software productions (TNSP)
#
# Requires get_feeds.tcl to be run as a cronjob, for example
# 15 * * * *     /absolute/path/to/get_feeds.tcl
#
# See also create_feeds_db.tcl OR convert_feeds_db.tcl, as you will
# need to either create a SQLite3 database or convert old text flat
# file to SQLite3.
#
# This script is freely distributable under GNU GPL (version 2) license.
#
##########################################################################

# SQLite3 database, MUST be set to same as in feeds.tcl
set feeds_dbfile "/home/niinuska/bot/feeds.sqlite"

# How often to check for newly added feed items from the database?
# (in minutes) This has nothing to do how often the backend script
# is run and actually scans / pulls the feeds, that is done in CRON.
# See get_feeds.tcl for more information.
set feeds_check_period 5

# Set channels and feed filters here:
# "channel" "feedname|feedname2"
# feednames can use matching, e.g. "mtv3*" would match all beginning with "mtv3"
array set feeds_channels {
  "mazmlame" "the adventurers|oots|mestari|blastwave"
  "fireball" "mestari"
  "tnsp" "the adventurers|oots|mestari"
}


set feeds_preferredmsg "PRIVMSG"


##########################################################################
# No need to look below this line
##########################################################################
set feeds_name "FeedCheck"
set feeds_message "$feeds_name v0.8 by ccr/TNSP"

putlog "$feeds_message"
bind pub - !feeds feeds_pubfetch
bind pub - !last feeds_publast


# ------------------------------------------------------------------------
proc feeds_escape { str } {
  return [string map {' ''} $str]
}

proc feeds_smsg {uchan umsg} {
  global feeds_preferredmsg
  putserv "$feeds_preferredmsg $uchan :$umsg"
}

proc feeds_log {umsg} {
  global feeds_name
  putlog "$feeds_name: $umsg"
}

proc feeds_msg {utime ufeed uurl utitle} {
  global feeds_channels
  foreach {uchan ufilter} [array get feeds_channels] {
    foreach umatch [split $ufilter "|"] {
      if {[string match -nocase $umatch $ufeed]} {
        feeds_smsg "#$uchan" "$ufeed: \002$utitle\002 -- $uurl"
      }
    }
  }
}

proc feeds_ctime {utime} {
  if {$utime == "" || $utime == "*"} {
    set utime 0
  }
  return [clock format $utime -format "%d.%m.%Y %H:%M"]
}


### Open database, etc
if {[catch {sqlite3 feeds_dbh $feeds_dbfile} uerrmsg]} {
  puts "Could not open SQLite3 database '$feeds_dbfile': $uerrmsg."
  exit 2
}


# ------------------------------------------------------------------------
proc feeds_check_start {} {
  global feeds_dbfile

  set oldtime 0
  if {![catch {set ufile [open "$feeds_dbfile.time" r 0600]} uerrmsg]} {
    gets $ufile oldtime
    close $ufile
  } else {
    feeds_log "Could not open timefile: $uerrmsg"
    set oldtime [clock seconds]
  }

  return $oldtime
}


proc feeds_check_end {} {
  global feeds_dbfile
  
  if {![catch {set ufile [open "$feeds_dbfile.time" w 0600]} uerrmsg]} {
    puts $ufile [clock seconds]
    close $ufile
  }
}

# ------------------------------------------------------------------------
proc feeds_exec {} {
  global feeds_dbh feeds_check_period feeds_running

#  feeds_log "Timed feed check."
  set oldtime [feeds_check_start]
  set found 0
  
  set usql "SELECT feed AS ufeed, title AS utitle, url AS uurl, utime AS utime FROM feeds WHERE utime > $oldtime ORDER BY utime ASC"
  feeds_dbh eval $usql {
    feeds_msg $utime $ufeed $uurl $utitle
    set found 1
  }

  if {$found} {
    feeds_check_end
  }

  set feeds_running [clock seconds]
  timer $feeds_check_period feeds_exec
}


if {[info exists feeds_running]} {
  set feeds_last [expr [clock seconds] - $feeds_running]
} else {
  set feeds_last -1
}

if {$feeds_last < 0 || $feeds_last > [expr $feeds_check_period * 60]} {
  feeds_log "Starting timed feed check."
  feeds_exec
}


# ------------------------------------------------------------------------
proc feeds_pubfetch {unick uhost uhand uchan utext} {
  global feeds_dbh
#  feeds_log "Manual check invoked on $uchan."

  set oldtime [feeds_check_start]
  set nresult [feeds_dbh onecolumn "SELECT COUNT(*) FROM feeds WHERE utime > $oldtime"]

  if {$nresult > 0} {
    feeds_check_end
    if {$nresult >= 20} {
      feeds_smsg $uchan "$nresult uutta, tod. näk. epäsynkissä. Ignoorataan."
    } else {
      set usql "SELECT feed AS ufeed, title AS utitle, url AS uurl, utime AS utime FROM feeds WHERE utime > $oldtime ORDER BY utime ASC"
      feeds_dbh eval $usql {
        feeds_msg $utime $ufeed $uurl $utitle
        set found 1
      }
      feeds_smsg $uchan "$nresult uutta."
    }
  } else {
    feeds_smsg $uchan "Ei uusia."
  }
}


# ------------------------------------------------------------------------
proc feeds_publast {unick uhost uhand uchan utext} {
  global feeds_dbh
  set ufound 0

  set usql "SELECT url AS uurl, feed AS ufeed, title AS utitle, utime AS utime FROM feeds WHERE feed LIKE '%[string tolower [feeds_escape $utext]]%' ORDER BY utime DESC LIMIT 1"
  feeds_dbh eval $usql {
    feeds_smsg $uchan "Uusin '$ufeed' / [feeds_ctime $utime]: $utitle -- $uurl"
    set ufound 1
  }

  if {$ufound == 0} {
    feeds_smsg $uchan "Ei osumia haulla '$utext'."
  }
}