Mercurial > hg > egg-tcls
annotate feeds.tcl @ 422:880a07485275
Add utl_ctime() to utillib and use it elsewhere.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 08 Jan 2017 03:52:15 +0200 |
parents | 9a33575eb999 |
children | 44c9128097cd |
rev | line source |
---|---|
70 | 1 ########################################################################## |
0 | 2 # |
323 | 3 # FeedCheck v1.0 by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
265
908edc54005a
feeds: Move configuration to separate file.
Matti Hamalainen <ccr@tnsp.org>
parents:
226
diff
changeset
|
4 # (C) Copyright 2008-2015 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 ########################################################################## |
0 | 29 set feeds_name "FeedCheck" |
323 | 30 set feeds_message "$feeds_name v1.0 by ccr/TNSP" |
0 | 31 |
32 putlog "$feeds_message" | |
33 bind pub - !feeds feeds_pubfetch | |
34 bind pub - !last feeds_publast | |
35 | |
36 | |
70 | 37 # ------------------------------------------------------------------------ |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
38 proc feeds_escape { str } { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
39 return [string map {' ''} $str] |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
40 } |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
41 |
0 | 42 proc feeds_smsg {uchan umsg} { |
43 global feeds_preferredmsg | |
44 putserv "$feeds_preferredmsg $uchan :$umsg" | |
45 } | |
46 | |
47 proc feeds_log {umsg} { | |
48 global feeds_name | |
49 putlog "$feeds_name: $umsg" | |
50 } | |
51 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
52 proc feeds_msg {utime ufeed uurl utitle} { |
0 | 53 global feeds_channels |
54 foreach {uchan ufilter} [array get feeds_channels] { | |
143 | 55 foreach umatch [split $ufilter "|"] { |
56 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
|
57 feeds_smsg "$uchan" "$ufeed: \002$utitle\002 -- $uurl" |
0 | 58 } |
59 } | |
60 } | |
61 } | |
62 | |
63 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
64 ### Open database, etc |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
65 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
|
66 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
|
67 exit 2 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
68 } |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
69 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
70 |
70 | 71 # ------------------------------------------------------------------------ |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
72 proc feeds_check_start {} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
73 global feeds_dbfile |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
74 |
0 | 75 set oldtime 0 |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
76 if {![catch {set ufile [open "$feeds_dbfile.time" r 0600]} uerrmsg]} { |
0 | 77 gets $ufile oldtime |
78 close $ufile | |
79 } else { | |
80 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
|
81 set oldtime [clock seconds] |
0 | 82 } |
141
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 return $oldtime |
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 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
87 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
88 proc feeds_check_end {} { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
89 global feeds_dbfile |
0 | 90 |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
91 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
|
92 puts $ufile [clock seconds] |
0 | 93 close $ufile |
94 } | |
95 } | |
96 | |
70 | 97 # ------------------------------------------------------------------------ |
0 | 98 proc feeds_exec {} { |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
99 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
|
100 |
146
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
101 # 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
|
102 set oldtime [feeds_check_start] |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
103 |
142 | 104 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
|
105 set nresult [feeds_dbh onecolumn "SELECT COUNT(*) FROM feeds WHERE utime > $oldtime"] |
361
299cdd79b7ee
feeds: Fix some limit checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
351
diff
changeset
|
106 if {$nresult > $feeds_sync_limit} { |
393 | 107 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
|
108 } else { |
393 | 109 feeds_log "$nresult new entries since $oldtime .." |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
110 feeds_dbh eval $usql { |
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
111 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
|
112 } |
0 | 113 } |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
114 |
351
0f55bbb7fea3
feeds: Improve safety checks and add new configuration setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
323
diff
changeset
|
115 if {$nresult > 0} { |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
116 feeds_check_end |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
117 } |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
118 |
144 | 119 set feeds_running [clock seconds] |
120 timer $feeds_check_period feeds_exec | |
0 | 121 } |
122 | |
146
7106dd8db4de
Improve entity parsing, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
145
diff
changeset
|
123 |
144 | 124 if {[info exists feeds_running]} { |
125 set feeds_last [expr [clock seconds] - $feeds_running] | |
126 } else { | |
127 set feeds_last -1 | |
128 } | |
129 | |
130 if {$feeds_last < 0 || $feeds_last > [expr $feeds_check_period * 60]} { | |
0 | 131 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
|
132 feeds_exec |
0 | 133 } |
134 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
135 |
70 | 136 # ------------------------------------------------------------------------ |
0 | 137 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
|
138 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
|
139 # 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
|
140 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
141 set oldtime [feeds_check_start] |
142 | 142 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
|
143 |
0 | 144 if {$nresult > 0} { |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
145 feeds_check_end |
361
299cdd79b7ee
feeds: Fix some limit checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
351
diff
changeset
|
146 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
|
147 feeds_smsg $uchan "$nresult uutta, tod. näk. epäsynkissä. Ignoorataan." |
0 | 148 } else { |
142 | 149 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
|
150 feeds_dbh eval $usql { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
151 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
|
152 set found 1 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
153 } |
0 | 154 feeds_smsg $uchan "$nresult uutta." |
155 } | |
156 } else { | |
157 feeds_smsg $uchan "Ei uusia." | |
158 } | |
159 } | |
160 | |
161 | |
70 | 162 # ------------------------------------------------------------------------ |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
163 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
|
164 global feeds_dbh |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
165 set ufound 0 |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
166 |
142 | 167 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" |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
168 feeds_dbh eval $usql { |
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
169 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
|
170 set ufound 1 |
0 | 171 } |
172 | |
141
c99df41a691a
Work on making the feeds script use the SQLite3 backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
173 if {$ufound == 0} { |
0 | 174 feeds_smsg $uchan "Ei osumia haulla '$utext'." |
175 } | |
176 } |