Author Topic: urllib is not being nice :(  (Read 3322 times)

0 Members and 1 Guest are viewing this topic.

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
urllib is not being nice :(
« on: October 16, 2011, 04:36:44 pm »
So I am writing a plugin for XChat and I have come to the conclusion that urllib is somehow preventing greenText() from being executed... What is going on :(?

Here is the code:
Code: [Select]
__module_name__ = "IrmageBoard"
__module_version__ = "0.2.0"
__module_description__ = "Liek a real imageboard guies (without the images!!!1)"

import xchat, string, re, random, yaml
from urllib import urlopen

global myFilter
ymlFilter = urlopen("http://xvicario.us/irmageboard/filters.yml")
myFilter = yaml.load(ymlFilter)
ymlFilter.close()

print "\0034",__module_name__, __module_version__, "has been loaded\003"

def greenText(word, word_eol, userdata):
    print "EXECUTING greenText()"
    if word_eol[0].find('>'):
        return xchat.EAT_NONE
        print "FOUND NOTHING!"
    else:
        xchat.command(" ".join(["msg", xchat.get_info("channel"), ''.join(["3",word_eol[0]])]))
        print "FOUND SOMETIHNG!"
    return xchat.EAT_ALL

def filters(word, word_eol, userdata):
    newString = word_eol[0]
    for k, v in myFilter.iteritems():
        if k in newString:
            tempColor = random.randrange(0,16,1)
            tempBack = random.randrange(0,16,1)
            tempReplace = re.compile(k, re.IGNORECASE)
            newString = tempReplace.sub(v, newString)
            newString = "".join(["\003",str(tempColor),",",str(tempBack),newString,"\003"])
    xchat.command(" ".join(["msg", xchat.get_info("channel"), newString]))
    return xchat.EAT_ALL

xchat.hook_command("", greenText)
xchat.hook_command("", filters)

edit: that is everything :/
« Last Edit: October 16, 2011, 05:08:09 pm by jkag »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: urllib is not being nice :(
« Reply #1 on: October 16, 2011, 04:44:35 pm »
Have you tried using urllib2? You can find information on it here.

Can you try this?

Code: [Select]
__module_name__ = "IrmageBoard"
__module_version__ = "0.2.0"
__module_description__ = "Like a real imageboard guies (without the images!!1)"

import string
import re
import random
import yaml

import xchat
import urllib2

global myFilter #Do you really need to use globals? They ain't recommended
ymlFilter = urllib2.urlopen("http://xvicario.us/irmageboard/filters.yml")
myFilter = yaml.load(ymlFilter)
ymlFilter.close()

print "\0034 %s %s has been loaded\003" % (__module_name__, __module_version__)

def greenText(word, word_eol, userdata):
    """TODO Docstring for greenText()"""
    print "EXECUTING greenText()"
    if word_eol[0].find('>'):
        return xchat.EAT_NONE
        print "FOUND NOTHING!"
    else:
        xchat.command(" ".join(["msg", xchat.get_info("channel"),
                      ''.join(["3",word_eol[0]])]))
        print "FOUND SOMETHING!"
    return xchat.EAT_ALL

def filters(word, word_eol, userdata):
    """Filters a word"""
    newString = word_eol[0]
    for k, v in myFilter.iteritems():
        if k in newString:
            tempColor = random.randrange(0, 16, 1)
            tempBack = random.randrange(0, 16, 1)
            tempReplace = re.compile(k, re.IGNORECASE)
            newString = tempReplace.sub(v, newString)
            newString = "".join(["\003", str(tempColor), ", ", str(tempBack),
                                newString,"\003"])
    xchat.command(" ".join(["msg", xchat.get_info("channel"), newString]))
    return xchat.EAT_ALL

All my changes were minimum, but I think I changed a few things with urllib2 that might make a difference.
« Last Edit: October 16, 2011, 04:57:30 pm by ephan »

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: urllib is not being nice :(
« Reply #2 on: October 16, 2011, 04:57:17 pm »
It does the same thing :(

edit: you can actually see my debugging code :P "EXECUTING greenText()" etc lol that stuff never shows up, so i can safely say it doesn't execute. I also disabled the urlopen and the code then worked...
« Last Edit: October 16, 2011, 04:58:34 pm by jkag »

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: urllib is not being nice :(
« Reply #3 on: October 16, 2011, 08:42:20 pm »
It does the same thing :(

edit: you can actually see my debugging code :P "EXECUTING greenText()" etc lol that stuff never shows up, so i can safely say it doesn't execute. I also disabled the urlopen and the code then worked...
I'm assuming that you know that any print output is redirected to XChat's window, right?
(I haven't tested this, sorry! :()

EDIT: Oh wait, I think you're doing it wrong... :P
xchat.hook_command("", greenText) may not work because the initial argument is blank. I don't know how XChat2 responds to this though... but I would suggest trying other hooks too, found here: http://xchat.org/docs/xchatpython.html#head-36ddb85a7e96550366a7165d2aed583da9ea01f5

This is, of course, if you weren't looking at the XChat window and all the output was there. ;)
« Last Edit: October 16, 2011, 08:45:26 pm by alberthrocks »
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: urllib is not being nice :(
« Reply #4 on: October 16, 2011, 09:02:54 pm »
That is how you are supposed to intercept outgoing messages...  See the filters() function.  that does ALMOST the same thing.

edit: Somehow it was in my last two lines.  I switched the order of them (so the filter got hooked first, then the greenText) and it works... Idk why...
« Last Edit: October 16, 2011, 09:21:09 pm by jkag »

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: urllib is not being nice :(
« Reply #5 on: October 16, 2011, 10:06:03 pm »
That is how you are supposed to intercept outgoing messages...  See the filters() function.  that does ALMOST the same thing.

edit: Somehow it was in my last two lines.  I switched the order of them (so the filter got hooked first, then the greenText) and it works... Idk why...
I think I might know why... again, this is all theory, but I have an idea now: since you have the same first argument, the 2nd line will replace the hook established in the first. To prove it, add a print statement to the filters function, and if I'm right, you won't see that get printed at all! ;)

Assuming I'm right about the issue... it seems that you're simply playing around with the code, since I found your code snipplet online, which is probably a plugin example. However, in the future, if you are going to parse like this (with two functions), you should either:
1) Merge the two functions into one; or
2) If you prefer having two functions handing the hook, call the 2nd function from the first. That is...
Code: [Select]
__module_name__ = "IrmageBoard"
__module_version__ = "0.2.0"
__module_description__ = "Liek a real imageboard guies (without the images!!!1)"

import xchat, string, re, random, yaml
from urllib import urlopen

global myFilter
ymlFilter = urlopen("http://xvicario.us/irmageboard/filters.yml")
myFilter = yaml.load(ymlFilter)
ymlFilter.close()

print "\0034",__module_name__, __module_version__, "has been loaded\003"

def greenText(word, word_eol, userdata):
    print "EXECUTING greenText()"
    if word_eol[0].find('>'):
        return xchat.EAT_NONE
        print "FOUND NOTHING!"
    else:
        xchat.command(" ".join(["msg", xchat.get_info("channel"), ''.join(["3",word_eol[0]])]))
        print "FOUND SOMETIHNG!"
    return xchat.EAT_ALL

def filters(word, word_eol, userdata):
    newString = word_eol[0]
    for k, v in myFilter.iteritems():
        if k in newString:
            tempColor = random.randrange(0,16,1)
            tempBack = random.randrange(0,16,1)
            tempReplace = re.compile(k, re.IGNORECASE)
            newString = tempReplace.sub(v, newString)
            newString = "".join(["\003",str(tempColor),",",str(tempBack),newString,"\003"])
    xchat.command(" ".join(["msg", xchat.get_info("channel"), newString]))
    greenText(word, word_eol, userdata) # Call that 2nd function!
    return xchat.EAT_ALL # May not be necessary, since the greenText function also returns this too... although...
    # Maybe I'm a bit wrong in how it returns. If the 2nd function doesn't kick out of the plugin, simply do
    # ret = greenText(word, word_eol, userdata)
    # return ret

xchat.hook_command("", filters)
Be sure to read my included code comments for more info.

Have fun learning how to write XChat plugins! :D (They're really, really fun when they work! :))
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: urllib is not being nice :(
« Reply #6 on: October 16, 2011, 10:46:49 pm »
Its kinda funny because just switching them fixed it.  Both things work now.  This is my third script for XChat.  Its really fun doing this lol.

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: urllib is not being nice :(
« Reply #7 on: October 16, 2011, 10:59:27 pm »
Its kinda funny because just switching them fixed it.  Both things work now.  This is my third script for XChat.  Its really fun doing this lol.
Oh actually... I might know why, but then I don't know why it occurs. A lovely programming term called race condition.

If they are both being called, the first function returns xchat.EAT_ALL... which tells XChat to withhold any of the output from the user and any other plugins that might want to parse the data. So in a sense, your code still has some errors, it's just that the switch doesn't "EAT_ALL". ;) (My code above fixes that!)

It might do a stack like system - filters is first, greenText is last when added in the opposite way. When you switched it, greenText is first, filters is last in order of execution. Again, this is speculation, since XChat-WDK (Windows version) doesn't particularly do Python scripting unless you install some obscure version of it... :/
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline XVicarious

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +45/-28
  • I F**king Love Twisty Puzzles
    • View Profile
    • XVicarious
Re: urllib is not being nice :(
« Reply #8 on: October 16, 2011, 11:02:15 pm »
Actually that isn't true about installing an obscure version.  I have 2.7.  IDK if its Active Python or just vanilla but they operate almost the same.
The problem with your code is that filters() will be optional in the future (the function to change this is commented out in hte full script) so disabling filters() would disable the whole thing.  I can just use another return.