All pastes #1456996 Raw Edit

SitePlugin.py

public python v1 · immutable
#1456996 ·published 2009-06-11 19:56 UTC
rendered paste body
# -*- coding: cp1252 -*-##########################################################################################################   This is a Plugin to get the registered members from a site for Clan Checker for Soul Wars Unleashed.#       written by Aesus1##   Note that this script uses the mechanize browser###########################################################################################################%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#       Site Funtions#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%## Function:  Login =====================================================================================    #   syntax: Login (arg1)#   success: returns an 1#   failure: returns a -2 for a navigation error and -1 for any other error + deletes the bad entry   #========================================================================================================def Site_Login(arg1):#arg2):    br = arg1[0] # mechanize browser object    username = arg1[1]        password = arg1[2]    try:        debug = arg1[3]    except:        debug = 0    import urllib #needed for url encoding of login values    login_url = 'http://s1.zetaboards.com/swunleashed/login/log_in'    login_values = {'uname' : username, 'pw' : password, 'submit' : '1', 'cookie_on' : '1'}    login_data = urllib.urlencode(login_values)        try:        response1 = br.open(login_url, login_data)        HTMLSource1 = response1.read()         response1.close() #not really needed    except:        print "Error opening page"        return -2        else:        # this stores a copy of the login result page when debugging is set to 1 in the ini file        # Its handy when troubleshooting errors        if str(debug) == "1":            f = file('SWU_login.html', 'w')            f.write(HTMLSource1)            f.close()        # Verify Login Attempt Successful               success_string = "Welcome <strong><a href='http://s1.zetaboards.com/swunleashed/profile"        success = HTMLSource1.find(success_string)        if success >= 1:                      return 1        # Verify Login Attempt Successful         bad_login_str = "<strong>Guest</strong>"        bad_login = HTMLSource1.find(bad_login_str)        if bad_login >= 1:            print "loggin failed"            return -1        ## Function: GetSite ====================================================================================   #   syntax: GetSite(arg2)#   success: returns the an array with the number of pages as the first element and all the names,# join date, and last seen date, on that page#   failure: returns a -1 for a navigation error and -1 for any other error + deletes the bad entry#========================================================================================================def GetSite(arg2):#site_page = "1"):    br = arg2[0]    site_page = arg2[1]    membertypes = arg2[2]    try:        debug = arg2[3]    except:        debug = 0           from BeautifulSoup import BeautifulSoup    import os    import types    base_url = "http://s1.zetaboards.com/swunleashed/members/" #the base address    url_part2 = "/?name=&search_type=&group=0&order=a&sort=name&joinable=0"    url = base_url + site_page# + url_part2        try:        response = br.open(url)    except:        return - 1    else:        HTMLSource1 = response.read()        # save page if debugging is on        if str(debug) == "1":                    f = file('SWU_login.html', 'w')            f.write(HTMLSource1)            f.close()        # replace these strings for compatability            HTMLSource1 = HTMLSource1.replace('<font color=', '<color=')        HTMLSource1 = HTMLSource1.replace('</font color>', '</color>')        HTMLSource1 = HTMLSource1.replace("for(i=0;i<links.length;i++){", "")        if str(debug) == "1":            f = file('SWU_login2.htm', 'w')            f.write(HTMLSource1)            f.close()        soup = BeautifulSoup(HTMLSource1)        if site_page == "1":                        try:                li = soup.findAll('li')                lp_line = li[22].find('a')            except:                numpages = "1"            else:                lp_line =str(lp_line)                pos1 = lp_line.find('<a href="http://s1.zetaboards.com/swunleashed/members/')                pos2 = lp_line.find('/?name=&amp;search_type=&amp;group=0&amp;order=a&amp;sort=name&amp;joinable=0">', pos1)                numpages = lp_line[pos1 + len('<a href="http://s1.zetaboards.com/swunleashed/members/'):pos2]                member_list = [numpages]        else:            member_list = [site_page]        table = soup.find('table', cellspacing="0")        for row in table.findAll('tr')[3:-1]:            col = row.findAll('td')            # pull out the member type            membertypecell = col[1]            for i in membertypecell: # same issue as numpages                membertype = i            # only proccess the member if the member is in a member group in the list                        if membertype in membertypes:                # pull out the member name                membercell = col[0]                for j in membercell:                    for k in j:                        for l in k:                            if type(l) != types.InstanceType: #anything other than a member creates another parse lvl                                membername = l                            else:                                for m in l:                                    membername = m                # pull out the last activity date                    last_seen_cell = col[2] # <-- last active date "May 22, 2009" or "One minute ago" or "57 minutes ago" or "Today, 7:44 PM"                for date in last_seen_cell:                    for thingy in last_seen_cell:                        last_seen = thingy                # pull out the join date                join_date_cell = col[3] # <-- join date May 22, 2009                for another_date in join_date_cell:                    join_date = another_date                          another_thingy = (membername, membertype, join_date, last_seen)                member_list.append(another_thingy)              return member_list