Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 May, 2013, 06:51:19 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: urllib is not being nice :( -  (Read 509 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
XVicarious
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 15 April, 2013, 12:25:41
Date Registered: 04 January, 2011, 04:04:27
Location: 127.0.0.1
Posts: 456


Topic starter
Total Post Ratings: +17

View Profile WWW
« on: 16 October, 2011, 22:36:44 »
0

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 Sad?

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
__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 Undecided
« Last Edit: 16 October, 2011, 23:08:09 by jkag » Logged







Spoiler for Countries:


Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 07 May, 2013, 22:49:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Total Post Ratings: +174

View Profile
« Reply #1 on: 16 October, 2011, 22:44:35 »
0

Have you tried using urllib2? You can find information on it here.

Can you try this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__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: 16 October, 2011, 22:57:30 by ephan » Logged
XVicarious
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 15 April, 2013, 12:25:41
Date Registered: 04 January, 2011, 04:04:27
Location: 127.0.0.1
Posts: 456


Topic starter
Total Post Ratings: +17

View Profile WWW
« Reply #2 on: 16 October, 2011, 22:57:17 »
0

It does the same thing Sad

edit: you can actually see my debugging code Tongue "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: 16 October, 2011, 22:58:34 by jkag » Logged







Spoiler for Countries:


alberthrocks
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Last Login: 11 May, 2013, 04:49:27
Date Registered: 01 May, 2010, 16:51:27
Posts: 743

Total Post Ratings: +88

View Profile
« Reply #3 on: 17 October, 2011, 02:42:20 »
0

It does the same thing Sad

edit: you can actually see my debugging code Tongue "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! Sad)

EDIT: Oh wait, I think you're doing it wrong... Tongue
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. Wink
« Last Edit: 17 October, 2011, 02:45:26 by alberthrocks » Logged

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! Sad Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. Smiley

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 Tongue)
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 Tongue)
wxWabbitemu: 40% done (NEED MOAR FEATURES Tongue)

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 Tongue)
XVicarious
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 15 April, 2013, 12:25:41
Date Registered: 04 January, 2011, 04:04:27
Location: 127.0.0.1
Posts: 456


Topic starter
Total Post Ratings: +17

View Profile WWW
« Reply #4 on: 17 October, 2011, 03:02:54 »
0

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: 17 October, 2011, 03:21:09 by jkag » Logged







Spoiler for Countries:


alberthrocks
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Last Login: 11 May, 2013, 04:49:27
Date Registered: 01 May, 2010, 16:51:27
Posts: 743

Total Post Ratings: +88

View Profile
« Reply #5 on: 17 October, 2011, 04:06:03 »
0

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! Wink

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
__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! Cheesy (They're really, really fun when they work! Smiley)
Logged

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! Sad Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. Smiley

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 Tongue)
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 Tongue)
wxWabbitemu: 40% done (NEED MOAR FEATURES Tongue)

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 Tongue)
XVicarious
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 15 April, 2013, 12:25:41
Date Registered: 04 January, 2011, 04:04:27
Location: 127.0.0.1
Posts: 456


Topic starter
Total Post Ratings: +17

View Profile WWW
« Reply #6 on: 17 October, 2011, 04:46:49 »
0

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.
Logged







Spoiler for Countries:


alberthrocks
Coder Of Tomorrow
LV8 Addict (Next: 1000)
*
Offline Offline

Last Login: 11 May, 2013, 04:49:27
Date Registered: 01 May, 2010, 16:51:27
Posts: 743

Total Post Ratings: +88

View Profile
« Reply #7 on: 17 October, 2011, 04:59:27 »
0

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". Wink (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... Undecided
Logged

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! Sad Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. Smiley

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 Tongue)
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 Tongue)
wxWabbitemu: 40% done (NEED MOAR FEATURES Tongue)

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 Tongue)
XVicarious
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 15 April, 2013, 12:25:41
Date Registered: 04 January, 2011, 04:04:27
Location: 127.0.0.1
Posts: 456


Topic starter
Total Post Ratings: +17

View Profile WWW
« Reply #8 on: 17 October, 2011, 05:02:15 »
0

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.
Logged







Spoiler for Countries:


Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.273 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.