Mercurial > hg > egg-tcls
view fetch_feeds.tcl @ 323:9380535b8f12
feeds: Bump version.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 18 May 2015 13:44:08 +0300 |
parents | b4adc56446f6 |
children | 51c08336d7b1 |
line wrap: on
line source
#!/usr/bin/tclsh # # NOTICE! Change above path to correct tclsh binary path! # ############################################################################## # # FeedCheck fetcher v1.0 by Matti 'ccr' Hamalainen <ccr@tnsp.org> # (C) Copyright 2008-2015 Tecnic Software productions (TNSP) # # This script is freely distributable under GNU GPL (version 2) license. # ############################################################################## package require sqlite3 source [file dirname [info script]]/utillib.tcl ### The configuration should be in config.feeds in same directory ### as this script. Or change the line below to point where ever ### you wish. See "config.feeds.example" for an example config file. source [file dirname [info script]]/config.feeds ############################################################################## package require http if {[info exists http_user_agent] && $http_user_agent != ""} { ::http::config -urlencoding utf8 -useragent $http_user_agent } else { ::http::config -urlencoding utf8 -useragent "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0" } if {[info exists http_use_proxy] && $http_use_proxy != 0} { ::http::config -proxyhost $http_proxy_host -proxyport $http_proxy_port } if {[info exists http_tls_support] && $http_tls_support != 0} { package require tls ::http::register https 443 [list ::tls::socket -request 1 -require 1 -tls1 1 -cadir $http_tls_cadir] } ############################################################################## proc fetch_dorequest { urlStr urlStatus urlSCode urlCode urlData urlMeta } { upvar 1 $urlStatus ustatus upvar 1 $urlSCode uscode upvar 1 $urlCode ucode upvar 1 $urlData udata upvar 1 $urlMeta umeta if {[catch {set utoken [::http::geturl $urlStr -timeout 6000 -binary 1 -headers {Accept-Encoding identity}]} uerrmsg]} { puts "HTTP request failed: $uerrmsg" return 0 } set ustatus [::http::status $utoken] if {$ustatus == "timeout"} { puts "HTTP request timed out ($urlStr)" return 0 } if {$ustatus != "ok"} { puts "Error in HTTP transaction: [::http::error $utoken] ($urlStr)" return 0 } set ustatus [::http::status $utoken] set uscode [::http::code $utoken] set ucode [::http::ncode $utoken] set udata [::http::data $utoken] array set umeta [::http::meta $utoken] ::http::cleanup $utoken return 1 } proc add_entry {uname uprefix uurl utitle} { global currclock feeds_db nitems set utmp [utl_convert_html_ent $uurl] if {[string match "http://*" $utmp] || [string match "https://*" $utmp]} { set utest "$utmp" } else { if {[string range $uprefix end end] != "/" && [string range $utmp 0 0] != "/"} { set utest "$uprefix/$utmp" } else { set utest "$uprefix$utmp" } } set usql "SELECT title FROM feeds WHERE url='[utl_escape $utest]' AND feed='[utl_escape $uname]'" if {![feeds_db exists $usql]} { # puts "NEW: $utest : $utitle" set usql "INSERT INTO feeds (feed,utime,url,title) VALUES ('[utl_escape $uname]', $currclock, '[utl_escape $utest]', '[utl_escape [utl_convert_html_ent $utitle]]')" incr nitems if {[catch {feeds_db eval $usql} uerrmsg]} { puts "\nError: $uerrmsg on:\n$usql" exit 15 } } } proc add_rss_feed {datauri dataname dataprefix} { if {[catch {set utoken [::http::geturl $datauri -binary 1 -timeout 6000 -headers {Accept-Encoding identity}]} uerrmsg]} { puts "Error getting $datauri: $uerrmsg" return 1 } set upage [::http::data $utoken] ::http::cleanup $utoken set umatches [regexp -all -nocase -inline -- "<item>.\*\?<title><..CDATA.(.\*\?)\\\]\\\]></title>.\*\?<link>(http.\*\?)</link>.\*\?</item>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname $dataprefix [lindex $umatches [expr $n+2]] [lindex $umatches [expr $n+1]] } if {$nmatches == 0} { set umatches [regexp -all -nocase -inline -- "<item>.\*\?<title>(.\*\?)</title>.\*\?<link>(http.\*\?)</link>.\*\?</item>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname $dataprefix [lindex $umatches [expr $n+2]] [lindex $umatches [expr $n+1]] } } if {$nmatches == 0} { set umatches [regexp -all -nocase -inline -- "<item \[^>\]*>.\*\?<title>(.\*\?)</title>.\*\?<link>(http.\*\?)</link>.\*\?</item>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname $dataprefix [lindex $umatches [expr $n+2]] [lindex $umatches [expr $n+1]] } } return 0 } ############################################################################## ### Fetch and parse Halla-aho's blog page data proc fetch_halla_aho { } { set datauri "http://www.halla-aho.com/scripta/"; set dataname "Mestari" if {![fetch_dorequest $datauri ustatus uscode ucode upage umeta]} { return 0 } set umatches [regexp -all -nocase -inline -- "<a href=\"(\[^\"\]+\.html)\"><b>(\[^<\]+)</b>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname $datauri [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] } set umatches [regexp -all -nocase -inline -- "<a href=\"(\[^\"\]+\.html)\">(\[^<\]\[^b\]\[^<\]+)</a>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname $datauri [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] } } ### The Adventurers proc fetch_adventurers { } { set datauri "http://www.peldor.com/chapters/index_sidebar.html"; set dataname "The Adventurers" if {![fetch_dorequest $datauri ustatus uscode ucode upage umeta]} { return 0 } set umatches [regexp -all -nocase -inline -- "<a href=\"(\[^\"\]+)\">(\[^<\]+)</a>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname "http://www.peldor.com/" [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] } } ### Order of the Stick proc fetch_oots { } { set datauri "http://www.giantitp.com/comics/oots.html"; set dataname "OOTS" if {![fetch_dorequest $datauri ustatus uscode ucode upage umeta]} { return 0 } set umatches [regexp -all -nocase -inline -- "<a href=\"(/comics/oots\[0-9\]+\.html)\">(\[^<\]+)</a>" $upage] set nmatches [llength $umatches] for {set n 0} {$n < $nmatches} {incr n 3} { add_entry $dataname "http://www.giantitp.com" [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] } } ### Open database, etc set nitems 0 set currclock [clock seconds] global feeds_db if {[catch {sqlite3 feeds_db $feeds_dbfile} uerrmsg]} { puts "Could not open SQLite3 database '$feeds_dbfile': $uerrmsg." exit 2 } ### Fetch the feeds fetch_halla_aho fetch_adventurers fetch_oots #add_rss_feed "http://www.kaleva.fi/rss/145.xml" "Kaleva/Tiede" "" add_rss_feed "http://www.effi.org/xml/uutiset.rss" "EFFI" "" add_rss_feed "http://static.mtv3.fi/rss/uutiset_rikos.rss" "MTV3/Rikos" "" #add_rss_feed "http://www.blastwave-comic.com/rss/blastwave.xml" "Blastwave" "" #add_rss_feed "http://lehti.samizdat.info/feed/" "Lehti" "" ### Close database feeds_db close puts "$nitems new items."