Mercurial > hg > egg-tcls
comparison feeds.tcl @ 141:c99df41a691a
Work on making the feeds script use the SQLite3 backend.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 04 Jun 2013 15:52:32 +0300 |
parents | 593874678e45 |
children | 4c51eeba993f |
comparison
equal
deleted
inserted
replaced
140:b0648e05c855 | 141:c99df41a691a |
---|---|
1 ########################################################################## | 1 ########################################################################## |
2 # | 2 # |
3 # FeedCheck v0.7 by Matti 'ccr' Hamalainen <ccr@tnsp.org> | 3 # FeedCheck v0.8 by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
4 # (C) Copyright 2008-2010 Tecnic Software productions (TNSP) | 4 # (C) Copyright 2008-2013 Tecnic Software productions (TNSP) |
5 # | 5 # |
6 # Requires get_feeds.tcl to be run as a cronjob, for example | 6 # Requires get_feeds.tcl to be run as a cronjob, for example |
7 # 15 * * * * /absolute/path/to/get_feeds.tcl | 7 # 15 * * * * /absolute/path/to/get_feeds.tcl |
8 # | 8 # |
9 # This script is freely distributable under GNU GPL (version 2) license. | 9 # This script is freely distributable under GNU GPL (version 2) license. |
10 # | 10 # |
11 ########################################################################## | 11 ########################################################################## |
12 | 12 |
13 # Datafile, MUST be set to same as in get_feeds.tcl | 13 # SQLite3 database, MUST be set to same as in feeds.tcl |
14 set feeds_datafile "/home/niinuska/bot/data.feeds" | 14 set feeds_dbfile "/home/niinuska/bot/feeds.sqlite" |
15 | |
15 | 16 |
16 # Set channels and feed filters here: | 17 # Set channels and feed filters here: |
17 # "channel" "feedname|feedname2" | 18 # "channel" "feedname|feedname2" |
18 # feednames can use matching, e.g. "mtv3*" would match all beginning with "mtv3" | 19 # feednames can use matching, e.g. "mtv3*" would match all beginning with "mtv3" |
19 array set feeds_channels { | 20 array set feeds_channels { |
20 "mazmlame" "the adventurers|oots|mestari|blastwave" | 21 "mazmlame" "the adventurers|oots|mestari|blastwave" |
21 "fireball" "mestari" | 22 "fireball" "mestari|mtv3/rikos" |
22 "tnsp" "the adventurers|oots|mestari" | 23 "tnsp" "the adventurers|oots|mestari" |
23 } | 24 } |
24 | 25 |
25 | 26 |
26 set feeds_preferredmsg "PRIVMSG" | 27 set feeds_preferredmsg "PRIVMSG" |
28 | 29 |
29 ########################################################################## | 30 ########################################################################## |
30 # No need to look below this line | 31 # No need to look below this line |
31 ########################################################################## | 32 ########################################################################## |
32 set feeds_name "FeedCheck" | 33 set feeds_name "FeedCheck" |
33 set feeds_message "$feeds_name v0.7 by ccr/TNSP" | 34 set feeds_message "$feeds_name v0.8 by ccr/TNSP" |
34 | 35 |
35 putlog "$feeds_message" | 36 putlog "$feeds_message" |
36 bind pub - !feeds feeds_pubfetch | 37 bind pub - !feeds feeds_pubfetch |
37 bind pub - !last feeds_publast | 38 bind pub - !last feeds_publast |
38 | 39 |
39 | 40 |
40 # ------------------------------------------------------------------------ | 41 # ------------------------------------------------------------------------ |
42 proc feeds_escape { str } { | |
43 return [string map {' ''} $str] | |
44 } | |
45 | |
41 proc feeds_smsg {uchan umsg} { | 46 proc feeds_smsg {uchan umsg} { |
42 global feeds_preferredmsg | 47 global feeds_preferredmsg |
43 putserv "$feeds_preferredmsg $uchan :$umsg" | 48 putserv "$feeds_preferredmsg $uchan :$umsg" |
44 } | 49 } |
45 | 50 |
46 proc feeds_log {umsg} { | 51 proc feeds_log {umsg} { |
47 global feeds_name | 52 global feeds_name |
48 putlog "$feeds_name: $umsg" | 53 putlog "$feeds_name: $umsg" |
49 } | 54 } |
50 | 55 |
51 proc feeds_msg {urec} { | 56 proc feeds_msg {utime ufeed uurl utitle} { |
52 global feeds_channels | 57 global feeds_channels |
53 set uname [string tolower [lindex $urec 1]] | 58 feeds_log "$utime $ufeed $uurl $utitle" |
54 foreach {uchan ufilter} [array get feeds_channels] { | 59 foreach {uchan ufilter} [array get feeds_channels] { |
55 foreach umatch [split [string tolower $ufilter] "|"] { | 60 foreach umatch [split [string tolower $ufilter] "|"] { |
56 if {[string match $umatch $uname]} { | 61 if {[string match $umatch $ufeed]} { |
57 feeds_smsg "#$uchan" "[lindex $urec 1]: \002[lindex $urec 3]\002 -- [lindex $urec 2]" | 62 feeds_smsg "#$uchan" "$ufeed: \002$utitle\002 -- $uurl" |
58 } | 63 } |
59 } | 64 } |
60 } | 65 } |
61 } | 66 } |
62 | 67 |
66 } | 71 } |
67 return [clock format $utime -format "%d.%m.%Y %H:%M"] | 72 return [clock format $utime -format "%d.%m.%Y %H:%M"] |
68 } | 73 } |
69 | 74 |
70 | 75 |
76 ### Open database, etc | |
77 global feeds_dbh | |
78 if {[catch {sqlite3 feeds_dbh $feeds_dbfile} uerrmsg]} { | |
79 puts "Could not open SQLite3 database '$feeds_dbfile': $uerrmsg." | |
80 exit 2 | |
81 } | |
82 | |
83 | |
71 # ------------------------------------------------------------------------ | 84 # ------------------------------------------------------------------------ |
72 proc feeds_check {} { | 85 proc feeds_check_start {} { |
73 global feeds_datafile | 86 global feeds_dbfile |
74 | 87 |
75 set results {} | |
76 | |
77 # Otetaan aika | |
78 set newtime [clock seconds] | |
79 | |
80 # Haetaan edellinen | |
81 set oldtime 0 | 88 set oldtime 0 |
82 if {![catch {set ufile [open "$feeds_datafile.time" r 0600]} uerrmsg]} { | 89 if {![catch {set ufile [open "$feeds_dbfile.time" r 0600]} uerrmsg]} { |
83 gets $ufile oldtime | 90 gets $ufile oldtime |
84 close $ufile | 91 close $ufile |
85 } else { | 92 } else { |
86 feeds_log "Could not open timefile: $uerrmsg" | 93 feeds_log "Could not open timefile: $uerrmsg" |
94 set oldtime [clock seconds] | |
87 } | 95 } |
96 | |
97 return $oldtime | |
98 } | |
99 | |
100 | |
101 proc feeds_check_end {} { | |
102 global feeds_dbfile | |
88 | 103 |
89 # Tarkistetaan | 104 if {![catch {set ufile [open "$feeds_dbfile.time" w 0600]} uerrmsg]} { |
90 if {![catch {set ufile [open "$feeds_datafile" r 0600]} uerrmsg]} { | 105 puts $ufile [clock seconds] |
91 while {![eof $ufile]} { | |
92 gets $ufile uline | |
93 set urec [split $uline "½"] | |
94 if {[llength $urec] == 4 && [lindex $urec 0] > $oldtime} { | |
95 lappend results $urec | |
96 } | |
97 } | |
98 close $ufile | 106 close $ufile |
99 } else { | |
100 feeds_log "Could not open datafile: $uerrmsg" | |
101 } | 107 } |
102 | |
103 # Talletetaan | |
104 if {[llength $results] > 0} { | |
105 if {![catch {set ufile [open "$feeds_datafile.time" w 0600]} uerrmsg]} { | |
106 puts $ufile $newtime | |
107 close $ufile | |
108 } | |
109 } | |
110 | |
111 return $results | |
112 } | 108 } |
113 | 109 |
114 # ------------------------------------------------------------------------ | 110 # ------------------------------------------------------------------------ |
115 proc feeds_exec {} { | 111 proc feeds_exec {} { |
116 # feeds_log "Checking for new entries." | 112 global feeds_dbh |
117 set uresult [feeds_check] | 113 |
118 set nresult [llength $uresult] | 114 set oldtime [feeds_check_start] |
119 if {$nresult >= 20} { | 115 set found 0 |
120 feeds_log "$nresult entries, probably broken datafile. Ignoring." | 116 |
121 } else { | 117 set usql "SELECT feed AS ufeed, title AS utitle, url AS uurl, utime AS utime FROM feed_data WHERE utime > $oldtime ORDER BY utime ASC" |
122 foreach urec $uresult { feeds_msg $urec } | 118 feeds_dbh eval $usql { |
119 feeds_msg $utime $ufeed $uurl $utitle | |
120 set found 1 | |
123 } | 121 } |
122 | |
123 if {$found} { | |
124 feeds_check_end | |
125 } | |
126 | |
124 timer 5 feeds_exec | 127 timer 5 feeds_exec |
125 } | 128 } |
126 | 129 |
127 if {![info exists feeds_running]} { | 130 if {![info exists feeds_running]} { |
128 feeds_log "Starting timed feed check." | 131 feeds_log "Starting timed feed check." |
129 timer 5 feeds_exec | 132 timer 5 feeds_exec |
130 set feeds_running 1 | 133 set feeds_running 1 |
131 } | 134 } |
132 | 135 |
136 | |
133 # ------------------------------------------------------------------------ | 137 # ------------------------------------------------------------------------ |
134 proc feeds_pubfetch {unick uhost uhand uchan utext} { | 138 proc feeds_pubfetch {unick uhost uhand uchan utext} { |
135 feeds_log "Manual check invoked on $uchan." | 139 |
136 set uresult [feeds_check] | 140 global feeds_dbh |
137 set nresult [llength $uresult] | 141 # feeds_log "Manual check invoked on $uchan." |
142 | |
143 set oldtime [feeds_check_start] | |
144 set nresult [feeds_dbh onecolumn "SELECT COUNT(*) FROM feed_data WHERE utime > $oldtime"] | |
145 | |
138 if {$nresult > 0} { | 146 if {$nresult > 0} { |
147 feeds_check_end | |
139 if {$nresult >= 20} { | 148 if {$nresult >= 20} { |
140 feeds_smsg $uchan "$nresult uutta, tod. näk. epäsynkissä. Ignoorataan." | 149 feeds_smsg $uchan "$nresult uutta, tod. näk. epäsynkissä. Ignoorataan." |
141 } else { | 150 } else { |
142 foreach urec $uresult { feeds_msg $urec } | 151 set usql "SELECT feed AS ufeed, title AS utitle, url AS uurl, utime AS utime FROM feed_data WHERE utime > $oldtime ORDER BY utime ASC" |
152 feeds_dbh eval $usql { | |
153 feeds_msg $utime $ufeed $uurl $utitle | |
154 set found 1 | |
155 } | |
143 feeds_smsg $uchan "$nresult uutta." | 156 feeds_smsg $uchan "$nresult uutta." |
144 } | 157 } |
145 } else { | 158 } else { |
146 feeds_smsg $uchan "Ei uusia." | 159 feeds_smsg $uchan "Ei uusia." |
147 } | 160 } |
148 | |
149 } | 161 } |
150 | 162 |
151 | 163 |
152 # ------------------------------------------------------------------------ | 164 # ------------------------------------------------------------------------ |
153 proc feeds_checklast {ufeed} { | 165 proc feeds_publast {unick uhost uhand uchan utext} { |
154 global feeds_datafile | 166 global feeds_dbh |
155 | 167 set ufound 0 |
156 set result "" | 168 |
157 set oldtime 0 | 169 set usql "SELECT url AS uurl, feed AS ufeed, title AS utitle, utime AS utime FROM feed_data WHERE feed LIKE '%[string tolower [feeds_escape $utext]]%' ORDER BY utime DESC LIMIT 1" |
158 set matsi [string tolower "*$ufeed*"] | 170 feeds_dbh eval $usql { |
159 | 171 feeds_smsg $uchan "Uusin '$ufeed' / [feeds_ctime $utime]: $utitle -- $uurl" |
160 # Tarkistetaan | 172 set ufound 1 |
161 if {![catch {set ufile [open "$feeds_datafile" r 0600]} uerrmsg]} { | |
162 while {![eof $ufile]} { | |
163 gets $ufile uline | |
164 set urec [split $uline "½"] | |
165 if {[llength $urec] == 4 && [lindex $urec 0] >= $oldtime} { | |
166 if {[string match $matsi [string tolower [lindex $urec 1]]]} { | |
167 set result $urec | |
168 set oldtime [lindex $urec 0] | |
169 } | |
170 } | |
171 } | |
172 close $ufile | |
173 } else { | |
174 feeds_log "Could not open datafile: $uerrmsg" | |
175 } | 173 } |
176 | |
177 return $result | |
178 } | |
179 | 174 |
180 | 175 if {$ufound == 0} { |
181 proc feeds_publast {unick uhost uhand uchan utext} { | |
182 set uresult [feeds_checklast $utext] | |
183 if {$uresult != ""} { | |
184 feeds_smsg $uchan "Uusin '[lindex $uresult 1]' / [feeds_ctime [lindex $uresult 0]]: [lindex $uresult 3] -- [lindex $uresult 2]" | |
185 } else { | |
186 feeds_smsg $uchan "Ei osumia haulla '$utext'." | 176 feeds_smsg $uchan "Ei osumia haulla '$utext'." |
187 } | 177 } |
188 } | 178 } |