Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib2, urllib, re
- from BeautifulSoup import BeautifulSoup
- from BeautifulSoup import BeautifulStoneSoup
- class BattleNet():
- """ This class interfaces with battle.net to scrape data """
- def __init__(self):
- self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
- urllib2.install_opener( self.opener )
- self.loggedIn = False
- self.getEventDetailsColorRegex = re.compile('^name color\-c\d+$')
- self.getEventDetailsResponse = re.compile('^response [a-zA-Z]+$')
- def login(self, account, password):
- """ Login to battle.net, account is your email address, password is your password. Don't know about authenticator support. """
- # Bail out if we're already logged in
- if self.loggedIn:
- return False
- # Build the login URL
- p = urllib.urlencode( { 'ref' : '', 'app' : '', 'accountName': account, 'password' : password } )
- # Login
- f = self.opener.open( 'https://eu.battle.net/login/en/', p )
- # Grab the data from the login page
- data = f.read()
- f.close()
- # Check to see if we logged in successfully, not much error handling here.
- if data.find('<h3 class="section-title">Account Details</h3>') != -1:
- self.loggedIn = True
- return True
- else:
- print data
- return False
- def getPending(self):
- if not self.loggedIn:
- #self.login('USER', 'PASS')
- pass
- # Grab the calendar page
- #f = self.opener.open('https://eu.battle.net/wow/en/vault/character/event')
- f = open('output.html', 'r')
- document = f.read()
- # Time to break out the HTML parser
- soup = BeautifulSoup(document)
- soupupcoming = soup.findAll('li', 'event-category')
- soupevents = soupupcoming[0].findAll('li', "event-summary")
- events = []
- for soupevent in soupevents:
- event = {}
- # Get the event id
- event["event-id"] = soupevent['data-id']
- # Get the event title
- event["title"] = BeautifulStoneSoup(soupevent.findAll('h4', 'subheader name')[0].text, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
- # Get the event description
- event["description"] = BeautifulStoneSoup(soupevent.findAll('p', 'description')[0].text, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
- # Get the date of the event
- event["date"] = soupevent.findAll('span', 'datetime')[0].text
- # Are we invited?
- if soupevent.findAll('span', 'status invited'):
- event["invited"] = True
- else:
- event["invited"] = False
- events.append(event)
- return events
- def getEventDetails(self, eventid):
- p = urllib.urlencode( { 'eventId' : eventid } )
- #f = self.opener.open( 'https://eu.battle.net/wow/en/vault/character/event/details', p )
- f = open('list.html', 'r')
- document = f.read()
- soup = BeautifulSoup(document)
- soupplayers = soup.findAll('li')
- players = {}
- for soupplayer in soupplayers:
- player = soupplayer.findAll('a', { "class" : self.getEventDetailsColorRegex} )[0].text
- response = soupplayer.findAll('span', { "class" : self.getEventDetailsResponse} )[0].text
- if soupplayer.has_key("class") and soupplayer["class"] == 'leader':
- leader = player
- else:
- players[player] = response
- return leader, players
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement