last day (438 days later) » 

user435118
3:22 PM
Does anyone here know how I can listen to messages in a specific room with Python? e.g. like Smokey listens to messages here?
 
@Daniil Looks like there is a websocket you can listen to
 
user435118
@thesecretmaster I saw that and read the docs but have no idea how to use it.
 
user435118
3:37 PM
@user12986714 What websocket would that be?
 
4:04 PM
@Daniil it isn't stable
you have to get the url
 
user435118
So do I just get the room URL and scrape the response for the latest messages?
 
@Daniil that doesn't sound like a good idea to me.
 
@Daniil It's complex. Is there a reason you don't want to use the ChatExchange package, like SmokeDetector does?
 
user435118
@Makyen I don't mind, it's just I don't know how to use it (and yes, I've read the docs).
 
@Daniil No, if you just want to get the latest existing messages, then there's an API route that can get the existing messages in a room, but that route isn't a notification of new activity.
 
user435118
4:18 PM
@double-beep Probably not, I just got a response with everyone's email_hashes :/
 
user435118
@Makyen I would prefer that, but again, I need someone to tell me how to use it :)
 
@Daniil Using that API route would be polling, which is a very poor substitute for being notified of new activity. If you just want existing events, and don't care about new messages, then use the events route. If you want notifications of new messages, which is what you asked about, then listen to the WebSocket, which is what ChatExchange does. For listening for new messages, you're much better off using ChatExchange, which does the heavy lifting for you wrt. SE chat.
 
user435118
@Makyen I would prefer to use ChatExchange but I don't know how to use it.
 
user435118
And importing ChatExchange doesn't work even when I've installed it.
 
user435118
>>> from ChatExchange.chatexchange.client import Client
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    from ChatExchange.chatexchange.client import Client
ModuleNotFoundError: No module named 'ChatExchange'
 
user435118
Requirement already satisfied: ChatExchange in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (0.0.3)
 
from chatexchange.client import Client
 
user435118
That worked
 
user435118
So how do I get the contents of any new messages posted in a specific room, say here?
 
user435118
4:30 PM
e.g. it prints all new messages posted
 
@Daniil wasn't the example in the file I linked you earlier enough?
 
@Daniil I don’t have time to make a whole tutorial, but the ChatRoom class has the functionality you need
 
user435118
@double-beep It's not working.
 
in order to make a ChatRoom you’ll need to make a Client and maybe another intermediate object or two
Just look at the constructors and see what you need to give them
 
user435118
>>> c = Client("stackoverflow.com")
>>> c.login("REDACTED", "REDACTED")
RESPONSE REDACTED
>>> room = c.get_room(ROOM)
>>> room.join()
 
user435118
4:42 PM
It didn't join.
 
@Daniil any error? Also have you tried it outside of the REPL?
 
user435118
@NobodyNada No error. What's REPL?
 
Interactive Python en.m.wikipedia.org/wiki/Read–eval–print_loop
Also you probably have to keep going with the example github.com/thomas-daniels/SO-EditMonitor/blob/master/…
 
user435118
Now I get this
 
user435118
chatexchange.browser.LoginError: failed to get acct` cookie from Stack Exchange OpenID, check credentials provided for accuracy`
 
user435118
4:54 PM
They are working
 
@Daniil When first trying new credentials with SD, I often need to try many times prior to it starting to work. I'm not sure of the cause, but once it starts working (without any changes to SD's config file), it seems to keep going, even if re-loging in using credentials.
 
user435118
See, I just logged in with them (Daniil-SD).
 
user435118
@Makyen Ok, I'll do a loop.
 
Sometimes, you get captcha’d, to fix that have to open the login page yourself
I usually just log in with my own SE account in a private window
 
@Daniil I'd wait a substantial amount of time prior to retrying. In fact, if I was doing this, I'd spin up an SD instance which used those credentials, so that I know the code was working and the only issue was getting the credentials to work. Once the SD instance was able to log-in consistently using just the credentials (i.e. removing pickles/cookies.p), then I'd try my own code.
 
user435118
5:00 PM
I'm not using SD code at all.
 
user435118
Huh, no error but the user isn't showing in the user list.
 
SD uses ChatExchange
@Daniil yeah, that might not happen until you either post a message or tell CE to listen (as opposed to just join), not sure
 
test
 
user435118
Oh wow.
 
user435118
Can't believe that worked.
 
5:02 PM
If you’re going to do that a bunch, maybe do it in a test room?
 
user435118
@NobodyNada I'm not but if I do I will.
 
@Daniil Yes, but you're having problems which may be your code and may be the credentials. You have an SD instance. Spinning one up splits the problem into two things, both of which are more easily managed. That's a basic methodology for problem solving.
 
user435118
How can I make a continuous web socket? i.e. it listens and then the script stops after 5 seconds or so.
 
In which language, Python?
 
@Daniil The CE listen method is async, you need to keep the script running somehow
 
user435118
5:07 PM
@NobodyNada Ok
 
user435118
It works, not as intended but still.
 
user435118
:55520385 Ok
:55520385 Ok:55520385 Ok

:55520385 Ok:55520385 Ok

:55520385 Ok
:55520385 Ok
:55520385 Ok:55520385 Ok:55520385 Ok


:55520385 Ok
:55520385 Ok
:55520385 Ok
:55520385 Ok
:55520385 Ok
:55520385 Ok
:55520385 Ok
 
user435118
It's a mess now.
 
user435118
1600 lines
 
How do you get events?
 
user435118
5:08 PM
from chatexchange.client import Client
from chatexchange.events import MessagePosted

def on_event(event, _):
    if not isinstance(event, MessagePosted):
        return
    msg = event.message.content_source
    print(msg)

c = Client("stackexchange.com")
c.login("REDACTED", "REDACTED")
room = c.get_room(11540)
room.join()

while True:
    room.watch_socket(on_event)
 
Oh, that's going to trigger an awful lot, isn't it?
 
user435118
Yeah, need to think of how to keep it running.
 
@Daniil ... you’re spawning an infinite number of socket watchers
 
@Daniil, I'm sure people are interested in helping you, but you need to take this to a different room, as it's really not about SD/MS/Charcoal (at this point). Which room would you like me to move this conversation to?
@Daniil Basically, this has turned into a help/debug session, which has grown beyond what should be in this room when it's for your own project. A quick question is fine, but it's now dominating the transcript, which isn't OK.
 
user435118
@Makyen Sorry.
 
user435118
5:14 PM

XneroBot's Room

A room for using XneroBot (a bot which automates some flagging...
 
@Daniil np. That doesn't mean that people don't want to help, just that we shouldn't impose on all the other people who are only interested in Charcoal related things. Thanks.
 
user435118
@NobodyNada Yeah, I am. How can I keep the script running without doing that?
 
@Daniil well, that depends on when you want your bot to shut down
 
Keep it running just like the SD instances keep themselves running.
Time-out after X hours for reboot.
 
user435118
75 messages moved from Charcoal HQ
 
user435118
5:19 PM
@NobodyNada I can just kill the script manually. I just want the bot/script to be running until I shut it down.
 
user435118
So what I want is for it to print every new message in a specific room, but not 1000 times, only once and for it to be running until I kill it.
 
Just sleep forever then
 
user435118
What do you mean?
 
or like Mast suggested, if you’re running with something like nocrash.py sleep for 8 hours or so for an auto reboot
 
user435118
test1
 
user435118
5:24 PM
test2
 
user435118
test3
 
user435118
@NobodyNada Ah, it works. Sounds good. Thanks.
 
33
A: How to run the Python program forever?

roarsneerHow about this one? import signal signal.pause() This will let your program sleep until it receives a signal from some other process (or itself, in another thread), letting it know it is time to do something.

 
user435118
@NobodyNada Thanks.
 
user435118
Thanks, everyone for the help and sorry again about the offtopic discussion. cc @NobodyNada @Mast @Makyen @double-beep @thesecretmaster @user12986714
 
5:44 PM
@Daniil np. It's not a big deal. Such things happen. What one thinks is an easy/quick question can easily grow to being something more significant. The important thing is being able to recognize when that's happened, that the conversation is off-topic for the original room, and change to a more appropriate venue, as was done here. Thanks for being willing to do so.
 
6:32 PM
Started: Running on Daniil/MacbookAir
 
user435118
test
 
user435118
@NobodyNada How can I get the room ID a message was posted in?
 
user435118
db reboot
 
Started: Running on Daniil/MacbookAir
 
@Daniil There should be some property on the message object
 
user435118
6:34 PM
db reboot
 
Rebooting...
 
message.room.id
if you need the host, you can get it with message._client.host
 
6:46 PM
Started: Running on Daniil/MacbookAir
 
user435118
db reboot
 
Rebooting...
 
user435118
@NobodyNada Thanks.
 
@Daniil Not a problem
 
user435118
db reboot
 
6:48 PM
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
user435118
@Makyen Is there a regex to match SD reports?
 
user435118
I got this: (?<=\(\d+\).*\/(questions|a)\/)(\d{2,}), doesn't work though.
 
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
6:55 PM
Started: Running on Daniil/MacbookAir
 
user435118
test
 
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
Yes
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
7:01 PM
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
@Daniil Yes
 
@Daniil Yes.
 
user435118
@Makyen ...
 
And no. There are many regexes which will match SD reports. Your question is not answerable, because you're not providing enough context. (e.g. match regex reports in what form? where? how discriminate are you wanting to be? for example . is sure to match SD reports).
 
user435118
7:10 PM
I want to extract a post ID from all SD reports.
 
user435118
I would like it to be VERY accurate.
 
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
@Daniil Yes
 
@Daniil Are you trying to extract an MS post ID, or the question/answer ID on the SE site where the question or answer is posted?
 
user435118
7:17 PM
@Makyen SE post ID.
 
@Daniil Are you wanting to extract it from the chat page source, a chat event, etc.? Where are you getting the text and what does it look like?
 
user435118
@Makyen Extract it from the following format: [ [SmokeDetector](//git.io/vyDZv) | [MS](//m.erwaysoftware.com/posts/uid/medicalsciences/24672) ] Potentially bad keyword in answer (1): [Opioids are addictive or cause dependency issues only? ✏️](//medicalsciences.stackexchange.com/a/24672) by [Chukwuyem Obiazi](//medicalsciences.stackexchange.com/users/20377) on medicalsciences.SE``
 
user435118
(see message source for the actual format, I added one "" and another "" at the end)
 
user435118
...
 
user435118
I think you get what I mean
 
7:21 PM
OK, so why not use the ones in either FIRE or AIM?
 
user435118
It's Python...
 
Started: Running on Daniil/MacbookAir
 
In general, the features available in JavaScript regular expressions are a subset of those in Python, particularly in the regex package which SD uses.
 
user435118
So I'll stick to Python.
 
user435118
db alive
 
7:25 PM
@Daniil Yes
 
user435118
@Makyen Can you help me with a regex please?
 
user435118
I came up with "r(?<=(questions|a)\/)(\d{2,})" but I realised that's not SD specific
 
user435118
i.e. someone could post a link
 
user435118
I have imported the regex (not re) libraary so you know I have additional features available
 
user435118
@Daniil And the recent SD report confirmed that didn't work
 
7:42 PM
Are you wanting just the SE post ID?
Is the regex supposed to do anything else?
 
user435118
Preferably the site name as well (without the .com/.net)
 
user435118
I don't mind 2 regex's
 
Started: Running on Daniil/MacbookAir
 
user435118
db alive
 
@Daniil Yes
 
7:53 PM
For the sitename part, use ([\w.]*+)`$
For the site id part, maybe \[ \[SmokeDetector\]\(\/\/git\.io\/vyDZv\)(?: | \[MS\]\(\[\w.\/]+\))? \] (?:\w )*+\(\d*+\): \[.*\✏️\]\(\/\/([\w.\/]*+)\)
Or you can just parse it using some python procedure rather than regex
 
@Daniil Do you want the site name or the site domain?
 
@user12986714 \[ \[SmokeDetector\]\(\/\/git\.io\/vyDZv\)(?: | \[MS\]\(\[\w.\/]+\))? \] (?:\w )*+\(\d*+\): \[.*\]\(\/\/([\w.\/]*+)\)
Well, I guess regex101.com/r/KDvAzj/1 is probably good enough
 
user435118
8:30 PM
@Makyen Sitename used in the SE API
 
user435118
@user12986714 I only want to match the post ID
 
@Daniil Well then regex101.com/r/KDvAzj/2
Well although it is technically regular grammar, I would rather have a DPDA counting braces
Which is much more simple
 
Started: Running on Daniil/MacbookAir
 
user435118
8:45 PM
@user12986714 Thanks
 
Started: Running on Daniil/MacbookAir
Started: Running on Daniil/MacbookAir
 
user435118
bp alive
 
Started: Running on Daniil/MacbookAir
 
user435118
bp alive
 
user435118
db alive
 
8:59 PM
@Daniil Yes
Started: Running on Daniil/MacbookAir
 
user435118
Test
 
user435118
@user12986714 Either I'm doing something wrong or it doesn't match
 
user435118
RegEx = regex.compile(r"^\[ \[SmokeDetector\]\(\/\/git\.io\/vyDZv\)(?: \| \[MS\]\([\w.\/]*+\))? \] (?:\w*+ )*+\(\**+\d++\**+\): \[.*\]\(\/\/[\w.\/]*\/(\d++)\) by \[.*\]\(\/\/[\w.\/]*+\) on `([\w.]*+)`$")
if regex.match(RegEx, msg) == msg:
     matches = regex.match(RegEx, msg).group(1)
 
Started: Running on Daniil/MacbookAir
Started: Running on Daniil/MacbookAir
 
@Daniil What is the purpose of regex.match(RegEx, msg) == msg ?
match(pattern, string, flags=0, pos=None, endpos=None, partial=False, concurrent=None, timeout=None, ignore_unused=False, **kwargs)
    Try to apply the pattern at the start of the string, returning a match
    object, or None if no match was found.
 
user435118
9:11 PM
@user12986714 If I try and group something with no matches it throws an error
 
Returning a match object
So that clause is never true unless msg is None
 
user435118
Oh
 
user435118
What can I do then?
 
user435118
I need to see if it matches, if so I need group 1 or I get an error?
 
You can just try catch it
 
user435118
9:12 PM
How?
 
try:
    matches = regex.match(RegEx, msg).group(1)
except Exception:  # TODO Replace "Exception" with a more specific one
    # No match found
    # do_things
    pass

# Match found
# do_things
@Daniil Kinda like that if it is what you mean (^)
 
user435118
@user12986714 Ok, thanks
 
user435118
For the record it's an AttributeError @user12986714
 
user435118
@user12986714 How do I get the sitename?
 
user435118
` ([\w.]*+)$ doesn't work
 
user435118
With no .SE?
 
user435118
Just medicalsciences
 
@Daniil Just strip it
Because there are things like stackoverflow.com
Which is problematic
 
user435118
@user12986714 How?
 
sitename = sitename[:-3] if sitename.endswith(".SE")
 
user435118
9:23 PM
But that won't work for stackoverflow.com
 
@Daniil So what do you want for stackoverflow.com? Just stackoverflow?
 
user435118
Yes
 
user435118
So basically the sitename I would parse to the SE API
 
user435118
@user12986714 Cheers
 
9:29 PM
Started: Running on Daniil/MacbookAir
Started: Running on Daniil/MacbookAir
Started: Running on Daniil/MacBookAir
 
user435118
db alive
 
@Daniil Yes
 
9:45 PM
Started: Running on Daniil/MacBookAir
Started: Running on Daniil/MacBookAir
 
user435118
db stop
 
@Daniil Shutting down...
 
user435118
db alive
 

  last day (438 days later) »