Mercurial > hg > egg-tcls
annotate fetch_feeds.tcl @ 268:96310b1c88fa
feeds: Improve config resiliency.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 23 Jan 2015 10:25:51 +0200 |
parents | 908edc54005a |
children | f47b41d2be64 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/tclsh |
1 | 2 # |
3 # NOTICE! Change above path to correct tclsh binary path! | |
4 # | |
268
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
5 ############################################################################## |
0 | 6 # |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
7 # FeedCheck fetcher v0.9 by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
8 # (C) Copyright 2008-2015 Tecnic Software productions (TNSP) |
0 | 9 # |
10 # This script is freely distributable under GNU GPL (version 2) license. | |
11 # | |
268
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
12 ############################################################################## |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
13 package require sqlite3 |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
14 source [file dirname [info script]]/utillib.tcl |
0 | 15 |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
16 ### The configuration should be in config.feeds in same directory |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
17 ### as this script. Or change the line below to point where ever |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
18 ### you wish. See "config.feeds.example" for an example config file. |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
19 source [file dirname [info script]]/config.feeds |
0 | 20 |
21 | |
268
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
22 ############################################################################## |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
23 |
146
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
24 set feeds_ent_str "-|-|'|'|—|-|‏||—|-|–|--|‪||‬|" |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
25 append feeds_ent_str "|‎||å|å|Å|Å|é|é|:|:| | " |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
26 append feeds_ent_str "|”|\"|“|\"|«|<<|»|>>|"|\"" |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
27 append feeds_ent_str "|ä|ä|ö|ö|Ä|Ä|Ö|Ö|&|&|<|<|>|>" |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
28 append feeds_ent_str "|ä|ä|å|ö|—|-|'|'|–|-|"|\"" |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
29 append feeds_ent_str "|||-|’|'|ü|ü|Ü|Ü|•|*|€|€" |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
30 append feeds_ent_str "|”|\"" |
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
31 set html_ent [split [encoding convertfrom "utf-8" $feeds_ent_str] "|"] |
0 | 32 |
33 package require http | |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
34 if {[info exists http_user_agent] && $http_user_agent != ""} { |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
35 ::http::config -urlencoding iso8859-1 -useragent $http_user_agent |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
36 } else { |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
37 ::http::config -urlencoding iso8859-1 -useragent "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 9.5" |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
38 } |
268
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
39 if {[info exists http_use_proxy] && $http_use_proxy != 0} { |
63 | 40 ::http::config -proxyhost $http_proxy_host -proxyport $http_proxy_port |
0 | 41 } |
42 | |
268
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
43 if {[info exists http_tls_support] && $http_tls_support != 0} { |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
44 package require tls |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
45 ::http::register https 443 [list ::tls::socket -request 1 -require 1 -tls1 1 -cadir $http_tls_cadir] |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
46 } |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
159
diff
changeset
|
47 |
0 | 48 |
268
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
49 ############################################################################## |
96310b1c88fa
feeds: Improve config resiliency.
Matti Hamalainen <ccr@tnsp.org>
parents:
265
diff
changeset
|
50 |
0 | 51 proc convert_ent {udata} { |
63 | 52 global html_ent |
146
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
53 return [string map -nocase $html_ent [string map $html_ent $udata]] |
0 | 54 } |
55 | |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
56 proc add_entry {uname uprefix uurl utitle} { |
142 | 57 global currclock feeds_db nitems |
147 | 58 set utmp [convert_ent $uurl] |
59 if {[string match "http://*" $utmp] || [string match "https://*" $utmp]} { | |
60 set utest "$utmp" | |
61 } else { | |
62 set utest "$uprefix$utmp" | |
63 } | |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
64 |
142 | 65 set usql "SELECT title FROM feeds WHERE url='[escape $utest]' AND feed='[escape $uname]'" |
140
b0648e05c855
Change some variable names, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
139
diff
changeset
|
66 if {![feeds_db exists $usql]} { |
142 | 67 set usql "INSERT INTO feeds (feed,utime,url,title) VALUES ('[escape $uname]', $currclock, '[escape $utest]', '[escape $utitle]')" |
68 incr nitems | |
140
b0648e05c855
Change some variable names, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
139
diff
changeset
|
69 if {[catch {feeds_db eval $usql} uerrmsg]} { |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
70 puts "\nError: $uerrmsg on:\n$usql" |
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
71 exit 15 |
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
72 } |
63 | 73 } |
0 | 74 } |
75 | |
76 | |
77 proc add_rss_feed {datauri dataname dataprefix} { | |
63 | 78 if {[catch {set utoken [::http::geturl $datauri -binary true -timeout 5000]} uerrmsg]} { |
79 puts "Error getting $datauri: $uerrmsg" | |
80 return 1 | |
81 } | |
82 set upage [::http::data $utoken] | |
83 ::http::cleanup $utoken | |
84 | |
85 set umatches [regexp -all -nocase -inline -- "<item>.\*\?<title><..CDATA.(.\*\?)\\\]\\\]></title>.\*\?<link>(http.\*\?)</link>.\*\?</item>" $upage] | |
86 set nmatches [llength $umatches] | |
87 for {set n 0} {$n < $nmatches} {incr n 3} { | |
88 add_entry $dataname $dataprefix [lindex $umatches [expr $n+2]] [lindex $umatches [expr $n+1]] | |
89 } | |
90 | |
91 if {$nmatches == 0} { | |
92 set umatches [regexp -all -nocase -inline -- "<item>.\*\?<title>(.\*\?)</title>.\*\?<link>(http.\*\?)</link>.\*\?</item>" $upage] | |
93 set nmatches [llength $umatches] | |
94 for {set n 0} {$n < $nmatches} {incr n 3} { | |
95 add_entry $dataname $dataprefix [lindex $umatches [expr $n+2]] [lindex $umatches [expr $n+1]] | |
96 } | |
97 } | |
0 | 98 |
63 | 99 if {$nmatches == 0} { |
100 set umatches [regexp -all -nocase -inline -- "<item \[^>\]*>.\*\?<title>(.\*\?)</title>.\*\?<link>(http.\*\?)</link>.\*\?</item>" $upage] | |
101 set nmatches [llength $umatches] | |
102 for {set n 0} {$n < $nmatches} {incr n 3} { | |
103 add_entry $dataname $dataprefix [lindex $umatches [expr $n+2]] [lindex $umatches [expr $n+1]] | |
104 } | |
105 } | |
143 | 106 |
63 | 107 return 0 |
0 | 108 } |
109 | |
110 | |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
111 ### Open database, etc |
142 | 112 set nitems 0 |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
113 set currclock [clock seconds] |
140
b0648e05c855
Change some variable names, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
139
diff
changeset
|
114 global feeds_db |
b0648e05c855
Change some variable names, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
139
diff
changeset
|
115 if {[catch {sqlite3 feeds_db $feeds_dbfile} uerrmsg]} { |
b0648e05c855
Change some variable names, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
139
diff
changeset
|
116 puts "Could not open SQLite3 database '$feeds_dbfile': $uerrmsg." |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
117 exit 2 |
0 | 118 } |
119 | |
120 | |
121 ############################################################################## | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
122 ### Fetch and parse Halla-aho's blog page data |
0 | 123 set datauri "http://www.halla-aho.com/scripta/"; |
124 set dataname "Mestari" | |
125 if {[catch {set utoken [::http::geturl $datauri -binary true -timeout 5000]} uerrmsg]} { | |
63 | 126 puts "Error getting $datauri: $uerrmsg" |
0 | 127 } else { |
63 | 128 set upage [::http::data $utoken] |
129 ::http::cleanup $utoken | |
130 | |
131 set umatches [regexp -all -nocase -inline -- "<a href=\"(\[^\"\]+\.html)\"><b>(\[^<\]+)</b>" $upage] | |
132 set nmatches [llength $umatches] | |
133 for {set n 0} {$n < $nmatches} {incr n 3} { | |
134 add_entry $dataname $datauri [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] | |
135 } | |
0 | 136 |
63 | 137 set umatches [regexp -all -nocase -inline -- "<a href=\"(\[^\"\]+\.html)\">(\[^<\]\[^b\]\[^<\]+)</a>" $upage] |
138 set nmatches [llength $umatches] | |
139 for {set n 0} {$n < $nmatches} {incr n 3} { | |
140 add_entry $dataname $datauri [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] | |
141 } | |
0 | 142 } |
143 | |
144 | |
145 ### The Adventurers | |
146 set datauri "http://www.peldor.com/chapters/index_sidebar.html"; | |
147 set dataname "The Adventurers" | |
148 if {[catch {set utoken [::http::geturl $datauri -binary true -timeout 5000]} uerrmsg]} { | |
63 | 149 puts "Error getting $datauri: $uerrmsg" |
0 | 150 } else { |
63 | 151 set upage [::http::data $utoken] |
152 ::http::cleanup $utoken | |
153 | |
154 set umatches [regexp -all -nocase -inline -- "<a href=\"(\[^\"\]+)\">(\[^<\]+)</a>" $upage] | |
155 set nmatches [llength $umatches] | |
156 for {set n 0} {$n < $nmatches} {incr n 3} { | |
157 add_entry $dataname "http://www.peldor.com/" [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] | |
158 } | |
0 | 159 } |
160 | |
161 | |
162 ### Order of the Stick | |
163 set datauri "http://www.giantitp.com/comics/oots.html"; | |
164 set dataname "OOTS" | |
165 if {[catch {set utoken [::http::geturl $datauri -binary true -timeout 5000]} uerrmsg]} { | |
63 | 166 puts "Error getting $datauri: $uerrmsg" |
0 | 167 } else { |
63 | 168 set upage [::http::data $utoken] |
169 ::http::cleanup $utoken | |
170 | |
171 set umatches [regexp -all -nocase -inline -- "<a href=\"(/comics/oots\[0-9\]+\.html)\">(\[^<\]+)</a>" $upage] | |
172 set nmatches [llength $umatches] | |
173 for {set n 0} {$n < $nmatches} {incr n 3} { | |
174 add_entry $dataname "http://www.giantitp.com" [lindex $umatches [expr $n+1]] [lindex $umatches [expr $n+2]] | |
175 } | |
0 | 176 } |
177 | |
178 | |
69
df3230f8aa46
Translate some comments to english and cosmetic fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
179 ### Generic RSS-feed fetching |
143 | 180 #add_rss_feed "http://www.kaleva.fi/rss/145.xml" "Kaleva/Tiede" "" |
0 | 181 |
182 add_rss_feed "http://www.effi.org/xml/uutiset.rss" "EFFI" "" | |
183 | |
143 | 184 add_rss_feed "http://static.mtv3.fi/rss/uutiset_rikos.rss" "MTV3/Rikos" "" |
0 | 185 |
186 add_rss_feed "http://www.blastwave-comic.com/rss/blastwave.xml" "Blastwave" "" | |
187 | |
188 #add_rss_feed "http://lehti.samizdat.info/feed/" "Lehti" "" | |
189 | |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
190 |
0 | 191 |
139
3305e142eecc
Change feed fetcher to use SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
192 ### Close database |
140
b0648e05c855
Change some variable names, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
139
diff
changeset
|
193 feeds_db close |
142 | 194 |
195 puts "$nitems new items." |