Mercurial > hg > egg-tcls
annotate feeds.tcl @ 433:2484f438b7d3
feeds: Use utl_escape() instead of our own copy.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Jan 2017 04:08:43 +0200 |
parents | 5763716060a0 |
children | 366324c417d8 |
rev | line source |
---|---|
70 | 1 ########################################################################## |
0 | 2 # |
425
e5810c52d376
Bump some copyright years and versions.
Matti Hamalainen <ccr@tnsp.org>
parents:
424
diff
changeset
|
3 # FeedCheck v1.1 by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
e5810c52d376
Bump some copyright years and versions.
Matti Hamalainen <ccr@tnsp.org>
parents:
424
diff
changeset
|
4 # (C) Copyright 2008-2017 Tecnic Software productions (TNSP) |
0 | 5 # |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
226
diff
changeset
|
6 # Requires fetch_feeds.tcl to be run as a cronjob, for example |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
226
diff
changeset
|
7 # 15 * * * * /absolute/path/to/fetch_feeds.tcl |
0 | 8 # |
145 | 9 # See also create_feeds_db.tcl OR convert_feeds_db.tcl, as you will |
10 # need to either create a SQLite3 database or convert old text flat | |
11 # file to SQLite3. | |
12 # | |
0 | 13 # This script is freely distributable under GNU GPL (version 2) license. |
14 # | |
70 | 15 ########################################################################## |
0 | 16 |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
226
diff
changeset
|
17 ### The configuration should be in config.feeds in same directory |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
226
diff
changeset
|
18 ### 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:
226
diff
changeset
|
19 ### you wish. See "config.feeds.example" for an example config file. |
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
226
diff
changeset
|
20 source [file dirname [info script]]/config.feeds |
0 | 21 |
422
880a07485275
Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
393
diff
changeset
|
22 ### Required utillib.tcl |
880a07485275
Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
393
diff
changeset
|
23 source [file dirname [info script]]/utillib.tcl |
880a07485275
Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
393
diff
changeset
|
24 |
0 | 25 |
70 | 26 ########################################################################## |
0 | 27 # No need to look below this line |
70 | 28 ########################################################################## |
423
44c9128097cd
feeds: Remember to require sqlite3 package.
Matti Hamalainen <ccr@tnsp.org>
parents:
422
diff
changeset
|
29 package require sqlite3 |
44c9128097cd
feeds: Remember to require sqlite3 package.
Matti Hamalainen <ccr@tnsp.org>
parents:
422
diff
changeset
|
30 |
0 | 31 set feeds_name "FeedCheck" |
425
e5810c52d376
Bump some copyright years and versions.
Matti Hamalainen <ccr@tnsp.org>
parents:
424
diff
changeset
|
32 set feeds_message "$feeds_name v1.1 by ccr/TNSP" |
0 | 33 |
34 putlog "$feeds_message" | |
35 bind pub - !feeds feeds_pubfetch | |
36 bind pub - !last feeds_publast | |
37 | |
38 | |
70 | 39 # ------------------------------------------------------------------------ |
0 | 40 proc feeds_smsg {uchan umsg} { |
41 global feeds_preferredmsg | |
42 putserv "$feeds_preferredmsg $uchan :$umsg" | |
43 } | |
44 | |
45 proc feeds_log {umsg} { | |
46 global feeds_name | |
47 putlog "$feeds_name: $umsg" | |
48 } | |
49 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
50 proc feeds_msg {utime ufeed uurl utitle} { |
0 | 51 global feeds_channels |
52 foreach {uchan ufilter} [array get feeds_channels] { | |
143 | 53 foreach umatch [split $ufilter "|"] { |
54 if {[string match -nocase $umatch $ufeed]} { | |
226
0ff78e418e77
feeds: Don't add # to channel names automatically.
Matti Hamalainen <ccr@tnsp.org>
parents:
154
diff
changeset
|
55 feeds_smsg "$uchan" "$ufeed: \002$utitle\002 -- $uurl" |
0 | 56 } |
57 } | |
58 } | |
59 } | |
60 | |
61 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
62 ### Open database, etc |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
63 if {[catch {sqlite3 feeds_dbh $feeds_dbfile} uerrmsg]} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
64 puts "Could not open SQLite3 database '$feeds_dbfile': $uerrmsg." |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
65 exit 2 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
66 } |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
67 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
68 |
70 | 69 # ------------------------------------------------------------------------ |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
70 proc feeds_check_start {} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
71 global feeds_dbfile |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
72 |
0 | 73 set oldtime 0 |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
74 if {![catch {set ufile [open "$feeds_dbfile.time" r 0600]} uerrmsg]} { |
0 | 75 gets $ufile oldtime |
76 close $ufile | |
77 } else { | |
78 feeds_log "Could not open timefile: $uerrmsg" | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
79 set oldtime [clock seconds] |
0 | 80 } |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
81 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
82 return $oldtime |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
83 } |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
84 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
85 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
86 proc feeds_check_end {} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
87 global feeds_dbfile |
424
825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
423
diff
changeset
|
88 |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
89 if {![catch {set ufile [open "$feeds_dbfile.time" w 0600]} uerrmsg]} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
90 puts $ufile [clock seconds] |
0 | 91 close $ufile |
92 } | |
93 } | |
94 | |
70 | 95 # ------------------------------------------------------------------------ |
0 | 96 proc feeds_exec {} { |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
97 global feeds_dbh feeds_check_period feeds_running feeds_sync_limit |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
98 |
146
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
99 # feeds_log "Timed feed check." |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
100 set oldtime [feeds_check_start] |
142 | 101 set usql "SELECT feed AS ufeed, title AS utitle, url AS uurl, utime AS utime FROM feeds WHERE utime > $oldtime ORDER BY utime ASC" |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
102 set nresult [feeds_dbh onecolumn "SELECT COUNT(*) FROM feeds WHERE utime > $oldtime"] |
424
825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
423
diff
changeset
|
103 |
361
299cdd79b7ee
feeds: Fix some limit checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
351
diff
changeset
|
104 if {$nresult > $feeds_sync_limit} { |
393 | 105 feeds_log "$nresult new entries, probably unsynchronized. Ignoring." |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
106 } else { |
431
5763716060a0
feeds: Use utl_ctime for formatting a message timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
425
diff
changeset
|
107 feeds_log "$nresult new entries since [utl_ctime $oldtime] .." |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
108 feeds_dbh eval $usql { |
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
109 feeds_msg $utime $ufeed $uurl $utitle |
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
110 } |
0 | 111 } |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
112 |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
113 if {$nresult > 0} { |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
114 feeds_check_end |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
115 } |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
116 |
144 | 117 set feeds_running [clock seconds] |
118 timer $feeds_check_period feeds_exec | |
0 | 119 } |
120 | |
146
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
121 |
144 | 122 if {[info exists feeds_running]} { |
123 set feeds_last [expr [clock seconds] - $feeds_running] | |
124 } else { | |
125 set feeds_last -1 | |
126 } | |
127 | |
128 if {$feeds_last < 0 || $feeds_last > [expr $feeds_check_period * 60]} { | |
0 | 129 feeds_log "Starting timed feed check." |
154
7a549487fba3
feeds: Cleanups, minor bugfix in feed update checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
146
diff
changeset
|
130 feeds_exec |
0 | 131 } |
132 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
133 |
70 | 134 # ------------------------------------------------------------------------ |
0 | 135 proc feeds_pubfetch {unick uhost uhand uchan utext} { |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
136 global feeds_dbh feeds_sync_limit |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
137 # feeds_log "Manual check invoked on $uchan." |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
138 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
139 set oldtime [feeds_check_start] |
142 | 140 set nresult [feeds_dbh onecolumn "SELECT COUNT(*) FROM feeds WHERE utime > $oldtime"] |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
141 |
0 | 142 if {$nresult > 0} { |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
143 feeds_check_end |
361
299cdd79b7ee
feeds: Fix some limit checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
351
diff
changeset
|
144 if {$nresult > $feeds_sync_limit} { |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
145 feeds_smsg $uchan "$nresult uutta, tod. näk. epäsynkissä. Ignoorataan." |
0 | 146 } else { |
142 | 147 set usql "SELECT feed AS ufeed, title AS utitle, url AS uurl, utime AS utime FROM feeds WHERE utime > $oldtime ORDER BY utime ASC" |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
148 feeds_dbh eval $usql { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
149 feeds_msg $utime $ufeed $uurl $utitle |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
150 set found 1 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
151 } |
0 | 152 feeds_smsg $uchan "$nresult uutta." |
153 } | |
154 } else { | |
155 feeds_smsg $uchan "Ei uusia." | |
156 } | |
157 } | |
158 | |
159 | |
70 | 160 # ------------------------------------------------------------------------ |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
161 proc feeds_publast {unick uhost uhand uchan utext} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
162 global feeds_dbh |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
163 set ufound 0 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
164 |
433
2484f438b7d3
feeds: Use utl_escape() instead of our own copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
431
diff
changeset
|
165 set usql "SELECT url AS uurl, feed AS ufeed, title AS utitle, utime AS utime FROM feeds WHERE feed LIKE '%[string tolower [utl_escape $utext]]%' ORDER BY utime DESC LIMIT 1" |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
166 feeds_dbh eval $usql { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
167 feeds_smsg $uchan "Uusin '$ufeed' / [feeds_ctime $utime]: $utitle -- $uurl" |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
168 set ufound 1 |
0 | 169 } |
170 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
171 if {$ufound == 0} { |
0 | 172 feeds_smsg $uchan "Ei osumia haulla '$utext'." |
173 } | |
174 } |