Page 1 of 1

Python script

Posted: Wed Jun 02, 2010 12:09 pm
by AD Epoch
I have written a short python script that intermittantly reads some webpages of your choice to see if your chosen callsigns are on it. It's meant to be used to monitor the BZ Flag stats web pages for various chosen servers to see if anyone I know is on (reads every 5 mins or so, but you could change that).

If anyone thinks it might be useful to them I'd be happy to post it.

AD Epoch

Re: Python script

Posted: Wed Jun 02, 2010 10:34 pm
by darkid
I'd feel like it's useful... is there a certain App to use to run python scripts?

Re: Python script

Posted: Wed Jun 02, 2010 11:09 pm
by joevano
Yes... python

Re: Python script

Posted: Thu Jun 03, 2010 8:32 am
by AD Epoch
Yes. Python is like another "virtual machine" sort of like Java. Except Python apps are just plain text files with a .py extension. You just need to install Python 2.6, which is free and open source and can be found at . They call Python executable suedo code because it resembles plain English in many ways.

I'll try to tidy it up a bit then I'll post it as an attachment of a reply in this thread.

Re: Python script

Posted: Thu Jun 03, 2010 11:06 am
by AD Epoch
I've tried to post the script but it says the extension pyw is not allowed (and presumably py). However since python files are just text files you can downlaod this text file and perform a simple rename of the file extension to get it working.

I use .pyw which tells Python on windows to not display a DOS box in the background, I don't know if this extension works on other Operating Systems but it's worth a try. If it doesn't work then use .py as that WILL work on any system.

It imports a module called winsound to play a sound file, but if you are using non Windows OS you may need to comment or remove this line near the top of the file:

import winsound

it can be commented out by placing a hash at the front of the line

#import winsound

It will need two file created in the same directory

Callsign list.TXT
URL list.TXT

The "Callsign list.TXT" file needs to contain callsigns exactly as you see them on screen, one on each line.

The "URL list.TXT" file needs to contain URL's like the one below: ... com%3A5154

Hope it is useful to you.

It's always good to look at the code before you run something that someone gives you just to make sure it's not malicious. A text editor is good, Python comes with one called IDLE.

Re: Python script

Posted: Thu Jun 03, 2010 12:57 pm
by strayer

Please check and consider using the RSS feed instead of parsing the HTML code. Even when the stats site isn't known for changing its HTML code that often, the advantage of using the RSS feed is the reliable tag structure.
(Furthermore, there already exist Python packages which parse RSS feed structure. This will make it easier for you too.)

Re: Python script

Posted: Fri Jun 04, 2010 10:00 am
by AD Epoch
I've heard a saying before like: You can't pleased all people all the time :). Not sure the omg was warranted :(, but another saying is hindsight is 20 20 vision. It's a great idea strayer. I didn't know that BZstats did RSS feeds, and that would be better, but as I have never hooked into one before it will be a project for a night with bad TV I think. I'll also have to experiment with what the stats can give me.

What I have now provides what I want, "who is on that I know on the servers I like.", so no rush for me.

But still good idea strayer.

Re: Python script

Posted: Fri Jun 04, 2010 7:10 pm
by strayer
Well, beside the certainty of having a design-independent content structure, a RSS feed (usually) allows to transfer the same data like an HTML page with less traffic and less server resources (for creation).
In this special case it seems that at least the amount of transferred bytes is higher when using the feed. Funny ... isn't it? :)

However, when you find the time (or the night with a boring TV program) you can indeed have a look at it - since it would/could broaden your horizon. ;)

Re: Python script

Posted: Sun Jun 06, 2010 6:05 am
by AD Epoch
Ok here's the RSS feed version. It was a great idea, and enabled me to remove one of the files needed. No more URL file, it's now 1 URL hardcoded into the constructor of the application's class. You still need to create a file called "Callsign list.TXT" that contains the callsign you are looking for, one per line, and it's case sensitive. Once again all this is just the default settings, as it's python it's just a text file, and if you are willing to wade through my code you can edit the names and settings, and make sure I'm not doing anything malicious.

Incidentaly I've decided that the xml.sax module is much more complicated than it should be. I've got half a mind to make my own module that delivers a list of tags and values and points to their parent tag. I know there are better modules out their but I wanted to use something that people didn't have to install separately, or any other files to keep in the same directory. It just get's too complicated for those who don't know about how Python works.

Re: Python script

Posted: Fri Jun 25, 2010 8:43 am
by cj
Sounds good AD.. Sadly I am running Macs.. I use Python for my Dropbox apps. I did what you suggest on the winsound but it comes up with this:

Traceback (most recent call last):
File "/Users/Darkroom/Dropbox/bzflag/scan BZStats v2.txt", line 33, in <module>
import winsound
ImportError: No module named winsound

I put a # in front of the line import winsound

Is there anybody that was successful in running this script with Macs?


Re: Python script

Posted: Wed Jun 30, 2010 9:24 am
by AD Epoch
Hey CJ!

Glad someone has had a go at it. Sorry to hear you are having trouble though. Unfortunately sound on Python is very OS dependent. I'm not sure how to do it with a Mac unfortunately, but I wou8ld suggest running a search as I'm sure there is an easy way. I ran a quick search and apparently there is a module called PyAudio that is on the Mac version. I tried it on my Win version but it wasn't there. I'm sure just removing the import statement (or commenting it out with a hash) should make the rest of the program work. The calls to the playign sound routines are wrapped in try-except statements which means it should simply display a message saying it can't play the sounds. If anybody else has any idea and would like to, share feel free to post the code for cj.

Also cj did you rename the file extension to .py? This makes it officially a "Python" file as opposed to some wierd textfile with Python like language. The only reason I posted it as a txt file is that the forum wouldn't let me (presumably because it is an executable file).

The more i use Python the more I like it, but realistically it's not as portable as Java, but much easier to use.

If anyone else has a comment on the application feel free to post it. I'd like to hear if it has been useful, (or not :().

Re: Python script

Posted: Wed Jun 30, 2010 11:05 pm
by cj
Thanks AD.

I didn't rename the file, but i used open with and used the python app to open but I will rename it. If I have a positive on that I will repost.

Thanks for replying

Re: Python script

Posted: Wed Jun 30, 2010 11:46 pm
by cj
I renamed the file to have the .py and it seems to work when I put an # in front of import winsound. I am now working on importing pyaudio in the script.

I have a question in the URL list.txt how do you go about find the url of the server? the main one is missilewars that Blast host.



Re: Python script

Posted: Thu Jul 01, 2010 11:14 am
by AD Epoch
Hi cj,

I think you might be using the wrong version. I posted 2, the first was done poorly, and the second is the one to use. However I have made a small change, now it will log the windows to a file. Use the one with THIS post. You WILL need to make a text file called "Callsign list.TXT" and place it in the same directory as the application. DO NOT FORGET to rename the file to a .py extension.

I am including a version of the callsign file.


I hope you get it working Okay cj. I use it whenever I want to check if someone I know is on, and that way I can find them easily.

All the best cj :)