| View previous topic :: View next topic |
| Author |
Message |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 6:21 pm Post subject: [SOLVED] Need help with file handling - thanks nml375! |
|
|
hello there eggdrop users
i need a little help with this file handling stuff
i'm working on a url catcher script, which catches every said urls on the channel, and makes them shown on a webpage
there's one script like this out there which uses mysqltcl and stores the data into a mysql database
what i am doing is really simple
catches all said urls with *http* and puts them in index.html
my problem that the bot puts every url after the latest one
so if there is a link on the page, the second one goes after
what i actually want is to put a pre defined line into the html
for example <---post here--->
and to make the bot put every post after this line and before the old posts
so every new post will be on the first place
| Code: | proc catch_url_pubm {nick host hand chan arg} {
if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
if {![catch {open /home/zerodtk/public_html/log/index.php "a+"} af]} {
set type "unknown"
if {![catch {::http::geturl $url} http]} {
if {[regexp -nocase {content-type image|content-type .?image} [::http::meta $http]]} {
set type "image"
puts $af "$nick added a new image $url"
close $af
} else |
etc etc
i think an IF command would do the job, but i can't get it work
let me know if you know any solution for this
thanks
Last edited by zerodtk on Sat Mar 28, 2009 8:56 am; edited 3 times in total |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Mar 25, 2009 7:28 pm Post subject: |
|
|
Actually, this will take a little more than a simple if-conditional.
The problem at hand, is that when you write to a file, by default the file pointer will be at the end. If you move it to an earlier point within the file (using seek), you'll instead overwrite whatever was there before.
To make this work, you'll need to read the whole file into memory, split the file in two (at the point you wish to insert the text), and combine your new url and the two halves into a new string. This you then write to your file.
Below you'll see an example on how to do this. Implementing/adapting this to suite your code shouldn't be too hard. Feel free to ask if anything is unclear or confusing.
| Code: | #string contains the whole file content
#url contains the whole line to insert into the file
#af is the handle of the opened file. File must be opened with read&write permissions.
set string [read $af]
set key "<!-- post here --->"
set j [expr [string first $key $string] + [string length $key]]
set i [expr $j - 1]
set first [string range $string 0 $i]
set last [string range $string $j end]
seek $af $j
puts -nonewline $af "${first}${url}${last}" |
Edit: Added missing "[expr ". _________________ NML_375, idling at #eggdrop@IrcNET
Last edited by nml375 on Wed Mar 25, 2009 9:19 pm; edited 2 times in total |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 8:14 pm Post subject: |
|
|
whenever i want to put in a set
f.e.
set string [read $af]
it says
Tcl error [catch_url_pubm]: wrong # args: should be "set varName ?newValue?"
any advice? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Mar 25, 2009 8:23 pm Post subject: |
|
|
That looks very odd. The syntax is correct.
You could try and enable the .set command (see your configfile on how to do this), and type ".set errorInfo" on the dcc partyline right after the error occurs. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 8:29 pm Post subject: |
|
|
well,
[01:28:41] [(Angela]: [01:26] #z3r0# set errorinfo
[01:28:41] [(Angela]: Error: can't read "errorinfo": no such variable
hm? |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 8:38 pm Post subject: |
|
|
it was my mistake...
.set errorInfo worked
| Code: | [01:37:46] [(Angela]: [01:35] #z3r0# set errorInfo
[01:37:46] [(Angela]: Currently: wrong # args: should be "set varName ?newValue?"
[01:37:46] [(Angela]: Currently: while executing
[01:37:46] [(Angela]: Currently: "set j [string first $key $string] + [string length $key]]"
[01:37:46] [(Angela]: Currently: (procedure "catch_url_pubm" line 6)
[01:37:46] [(Angela]: Currently: invoked from within
[01:37:46] [(Angela]: Currently: "catch_url_pubm $_pubm1 $_pubm2 $_pubm3 $_pubm4 $_pubm5" |
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Mar 25, 2009 8:57 pm Post subject: |
|
|
Ahh...
Guess I edited my code once too many, I'll update my previous post in a sec _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 9:11 pm Post subject: |
|
|
the error is now gone
this what happens
the file looks like
| Code: | header goes here
<!-- post here --->
footer goes here |
instead of putting the post between <!-- post here ---> and footer
it looks like
| Code: | | <!-- post here ---> http://www.google.com/2 |
it deletes the footer and the header too
and just changes the current link to another after adding a new one... |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Mar 25, 2009 9:22 pm Post subject: |
|
|
That's odd..
I missed including the file pointer in the puts-line, but that shouldn't wipe your file.. (updated post to fix that though)
Do you think you could post your implementation of my code? _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 9:36 pm Post subject: |
|
|
it was set to w+ not a+
actually its not deleting the header now
but deletes the <!-- post here ---> and the first few characters of the footer..
| Code: | proc catch_url_pubm {nick host hand chan arg} {
if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
if {![catch {open scripts/urllogger.txt "a+"} af]} {
set string [read $af]
set key "<!-- post here --->"
set j [expr [string first $key $string] + [string length $key]]
set i [expr $j - 1]
set first [string range $string 0 $i]
set last [string range $string $j end]
seek $af $j
puts -nonewline $af "$first $url $last"
# puts $af "$url"
close $af
} } }
bind pubm -|- *http* catch_url_pubm |
it was
| Code: | header goes here
<!-- post here --->
footer goes here |
and now
| Code: |
header goes here
http://www.google.com/ ter |
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Mar 25, 2009 9:54 pm Post subject: |
|
|
Actually, I do spot a flaw in my code. But it should produce an entirely different behaviour to what you are describing.
'k.. after a few minutes of thinking.. the problem at hand, is that you're opening the file in append-mode. That basically means that the file pointer is set to the end of the file... reading the file thus returns nothing. Opening the file in normal read/write mode solves that issue. I also removed some double-work I did in my first post.
The code below should behave properly.
| Code: | proc catch_url_pubm {nick host hand chan arg} {
if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
if {![catch {open scripts/urllogger.txt "RDWR CREAT"} af]} {
set string [read $af]
set key "<!-- post here --->"
set j [expr [string first $key $string] + [string length $key]]
set last [string range $string $j end]
seek $af $j start
puts -nonewline $af "\n${url}${last}"
close $af
}
}
}
bind pubm -|- *http* catch_url_pubm |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 9:58 pm Post subject: |
|
|
i think we are in the finish..
but there's one more thing
| Code: | header goes here
<!-- post here ---
http://www.google.com/
footer goes here |
the last > from <!-- post here ---> is missing...
everything else is just fine  |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Mar 25, 2009 10:10 pm Post subject: |
|
|
'k.. ohh, forgot, seek uses bytes, not characters.
Guess we'll have to do it the other way *sigh*
| Code: | proc catch_url_pubm {nick host hand chan arg} {
if {[regexp -nocase {(http://[^\s]+)} $arg -> url]} {
if {![catch {open scripts/urllogger.txt "RDWR CREAT"} af]} {
set string [read $af]
set key "<!-- post here --->"
set j [expr [string first $key $string] + [string length $key]]
set i [expr $j - 1]
set first [string range $string 0 $i]
set last [string range $string $j end]
seek $af 0 start
puts -nonewline $af "${first}\n${url}\n${last}"
close $af
}
}
}
bind pubm -|- *http* catch_url_pubm |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Wed Mar 25, 2009 10:15 pm Post subject: |
|
|
it works!!! thank you very much!!!
i can't believe it all works now
i will put it in the full script tomorrow morning and will let you know
thanks again!! |
|
| Back to top |
|
 |
zerodtk Voice
Joined: 25 Mar 2009 Posts: 20
|
Posted: Thu Mar 26, 2009 8:52 am Post subject: |
|
|
hey! all working! everything's just fine
thanks again |
|
| Back to top |
|
 |
|