Help - Search - Members - Calendar
Full Version: Example Of Using Pyqt (qt4) And Python
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > Python
Jeyrad
Here is a quick project I whipped up for retrieving the world status and highscore status from a MMORPG called "Runescape".

If you would like me to explain the script a bit more for your help, feel free to post any questions you have here.

CODE
#!/usr/bin/env python
"""
+-----------------------------------------------------------------------------+
| Title: Runescape Utilities
+-----------------------------------------------------------------------------+
| Author: Jeyrad Gerson (j3y.net)
| Date: 2007-09-17
+-----------------------------------------------------------------------------+
"""

import sys
from qt import *
import urllib

#Start HighScores class
class RSUtils(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)
        
        #Set Window Title
        self.setCaption("Runescape Utilities")
        self.layout = QGridLayout(self, 3, 3, 5, 5)
        
        #Set up the widgets
        self.WidgetStart()
        

    def WidgetStart(self):
        
        
        """
        +-------------------------------------+
        | HighScore Widgets
        +-------------------------------------+
        """
        #ListView for the Highscores
        self.hsdisp = QListView(self)
        self.hsdisp.setMinimumSize(300, 100)
        self.hsdisp.addColumn("Skill")
        self.hsdisp.addColumn("Level")
        self.hsdisp.addColumn("XP")
        self.hsdisp.addColumn("Rank")
        
        #Nickname Textbox
        self.nickTxt = QLineEdit(self, "Nickname")
        self.nickTxt.setMinimumWidth(150)
        
        # Get Highscores Button
        self.gethsBtn = QPushButton("Get Highscores", self)
        self.connect(self.gethsBtn, SIGNAL("clicked()"), self.get_highscores)
        
        """
        +-------------------------------------+
        | World List Widgets
        +-------------------------------------+
        """
        #ListView for the World List
        self.wldisp = QListView(self)
        self.wldisp.setMinimumSize(490, 100)
        self.wldisp.addColumn("World")
        self.wldisp.addColumn("Type")
        self.wldisp.addColumn("Players")
        self.wldisp.addColumn("Activity")
        self.wldisp.addColumn("Location")
        
        #Refresh World list
        self.refreshBtn = QPushButton("Refresh", self)
        self.connect(self.refreshBtn, SIGNAL("clicked()"), self.get_worldlist)
        
        #Setup the widget layout
        self.layout.addMultiCellWidget(self.hsdisp, 1, 1, 0, 1)
        self.layout.addWidget(self.wldisp, 1, 2)
        self.layout.addWidget(self.gethsBtn, 0, 1)
        self.layout.addWidget(self.nickTxt, 0, 0)
        self.layout.addWidget(self.refreshBtn, 0, 2)
        
        
    def get_highscores(self):
        
        # Clear the HighScore List
        self.hsdisp.clear()
        
        # Grab the web page source
        url = urllib.urlopen("http://hiscore.runescape.com/hiscorepersonal.ws?user1="+str(self.nickTxt.text()))
        
        #Open, read, close through the socket
        source = url.read()
        url.close()
        
        
        #Start extracting stuffs
        self.skill("Overall", source)
        self.skill("Attack", source)
        self.skill("Defence", source)
        self.skill("Strength", source)
        self.skill("Hitpoints", source)
        self.skill("Ranged", source)
        self.skill("Prayer", source)
        self.skill("Magic", source)
        self.skill("Cooking", source)
        self.skill("Woodcutting", source)
        self.skill("Fletching", source)
        self.skill("Fishing", source)
        self.skill("Firemaking", source)
        self.skill("Crafting", source)
        self.skill("Smithing", source)
        self.skill("Mining", source)
        self.skill("Herblore", source)
        self.skill("Agility", source)
        self.skill("Thieving", source)
        self.skill("Slayer", source)
        self.skill("Farming", source)
        self.skill("Runecraft", source)
        self.skill("Hunter", source)
        self.skill("Construction", source)
            
            
    def get_worldlist(self):
        
        #Clear the listview
        self.wldisp.clear()
        
        # Grab the web page source
        url = urllib.urlopen("http://www.runescape.com/sln.ws")
        
        #Open, read, close through the socket
        source = url.read()
        url.close()
        
        #Start extracting stuffs
        
        # Checking for the empty worlds
        for i in range(144):
            if i == 138:
                lul = "lol"
            elif i == 139:
                lul = "lol"
            elif i == 121:
                lul = "lol"
            elif i == 122:
                lul = "lol"
            elif i == 48:
                lul = "lol"
            elif i == 59:
                lul = "lol"
            elif i == 72:
                lul = "lol"
            else:
                #world is not empty, grab and place into listview
                self.world(str(i+1), source)
        

    def extract(self, text, sub1, sub2):
        # Used for on-the-go slicing simplicity
        return text.split(sub1)[-1].split(sub2)[0]
        
    def world(self, world, text):
        
        # Slicing through the source for the specified world
        text = text.replace("\n", "<")
        texts = self.extract(text, "World "+world+"<", "</tr>")
        texts1 = self.extract(texts, "<td>", "<")
        texts = texts.replace("<td>"+texts1+"</td>", "")
        texts2 = self.extract(texts, "\">", "</")
        texts = texts.replace("\">"+texts2+"</td>", "")
        texts3 = self.extract(texts, "<td>", "</")
        texts = texts.replace("<td>"+texts3+"</td>", "")
        texts4 = self.extract(texts, "\">", "<")
        

        QListViewItem(self.wldisp, world, texts2, texts3, texts1, texts4)
        
    def skill(self, skillname, text):

        texts = self.extract(text, skillname, "</tr>")
        texts1 = self.extract(texts, "\">", "<")
        texts = texts.replace(">"+texts1+"<", "")
        texts2 = self.extract(texts, "\">", "<")
        texts = texts.replace(">"+texts2+"<", "")
        texts3 = self.extract(texts, "\">", "<")
        
        lvi = QListViewItem(self.hsdisp, skillname, texts2, texts1, texts3)
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.connect(app, SIGNAL('lastWindowClosed()'), app,SLOT('quit()'))
    rsutils = RSUtils()
    rsutils.show()
    app.exec_loop()
Zytran
Nice one Jeyrad

I writen one awhile back in php, just havn't added a function to compare users at the moment.
Right now is just throws out an array, later it will be formatted.

http://runepost.com/hiscores.php

Also mine links to this page
http://hiscore.runescape.com/index_lite.ws...r=USERNAME_HERE
Jeyrad
Hehe, very nice, very nice.
MrCracker
Nice. I used to play. It's a pretty good game for java.. just got bored eventually.
Alex
Good example of how awesome and easy to learn Qt is. smile.gif
Julieth Gecko
Thanks for letting me buy levitra know about other good stuff ! cool.gif
topmakemoneyideas
I thing your wrote well as well as very great and very interesting and I got more detail because of your article.......
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.