Advertisement
Azelphur

Untitled

Jul 5th, 2011
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. import urllib2, urllib, re
  2. from BeautifulSoup import BeautifulSoup
  3. from BeautifulSoup import BeautifulStoneSoup
  4.  
  5. class BattleNet():
  6.     """ This class interfaces with battle.net to scrape data """
  7.     def __init__(self):
  8.         self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
  9.         urllib2.install_opener( self.opener )
  10.         self.loggedIn = False
  11.         self.getEventDetailsColorRegex = re.compile('^name color\-c\d+$')
  12.         self.getEventDetailsResponse = re.compile('^response [a-zA-Z]+$')
  13.  
  14.     def login(self, account, password):
  15.         """ Login to battle.net, account is your email address, password is your password. Don't know about authenticator support. """
  16.  
  17.         # Bail out if we're already logged in
  18.         if self.loggedIn:
  19.             return False
  20.  
  21.         # Build the login URL
  22.         p = urllib.urlencode( { 'ref' : '', 'app' : '', 'accountName': account, 'password' : password } )
  23.  
  24.         # Login
  25.         f = self.opener.open( 'https://eu.battle.net/login/en/',  p )
  26.  
  27.         # Grab the data from the login page
  28.         data = f.read()
  29.         f.close()
  30.  
  31.         # Check to see if we logged in successfully, not much error handling here.
  32.         if data.find('<h3 class="section-title">Account Details</h3>') != -1:
  33.             self.loggedIn = True
  34.             return True
  35.         else:
  36.             print data
  37.             return False
  38.  
  39.     def getPending(self):
  40.         if not self.loggedIn:
  41.             #self.login('USER', 'PASS')
  42.             pass
  43.  
  44.         # Grab the calendar page
  45.         #f = self.opener.open('https://eu.battle.net/wow/en/vault/character/event')
  46.         f = open('output.html', 'r')
  47.         document = f.read()
  48.    
  49.         # Time to break out the HTML parser
  50.         soup = BeautifulSoup(document)
  51.         soupupcoming = soup.findAll('li', 'event-category')
  52.         soupevents = soupupcoming[0].findAll('li', "event-summary")
  53.        
  54.         events = []
  55.  
  56.         for soupevent in soupevents:
  57.             event = {}
  58.            
  59.             # Get the event id
  60.             event["event-id"] = soupevent['data-id']
  61.  
  62.             # Get the event title
  63.             event["title"] = BeautifulStoneSoup(soupevent.findAll('h4', 'subheader name')[0].text, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
  64.  
  65.             # Get the event description
  66.             event["description"] = BeautifulStoneSoup(soupevent.findAll('p', 'description')[0].text, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
  67.  
  68.             # Get the date of the event
  69.             event["date"] = soupevent.findAll('span', 'datetime')[0].text
  70.  
  71.             # Are we invited?
  72.             if soupevent.findAll('span', 'status invited'):
  73.                 event["invited"] = True
  74.             else:
  75.                 event["invited"] = False
  76.  
  77.             events.append(event)
  78.         return events
  79.  
  80.     def getEventDetails(self, eventid):
  81.         p = urllib.urlencode( { 'eventId' : eventid } )
  82.         #f = self.opener.open( 'https://eu.battle.net/wow/en/vault/character/event/details',  p )
  83.         f = open('list.html', 'r')
  84.         document = f.read()
  85.  
  86.         soup = BeautifulSoup(document)
  87.         soupplayers = soup.findAll('li')
  88.  
  89.         players = {}
  90.  
  91.         for soupplayer in soupplayers:
  92.             player = soupplayer.findAll('a', { "class" : self.getEventDetailsColorRegex} )[0].text
  93.             response = soupplayer.findAll('span', { "class" : self.getEventDetailsResponse} )[0].text
  94.             if soupplayer.has_key("class") and soupplayer["class"] == 'leader':
  95.                 leader = player
  96.             else:
  97.                 players[player] = response
  98.            
  99.         return leader, players
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement