示例#1
0
文件: api.py 项目: alexwyler/LCS247
def get_game_info_from_lolnexus(account):
    if account[1] == 'KR':
        return None
    data = parse.urlencode({'name': account[0]}).encode('utf-8')
    req = request.Request('http://www.lolnexus.com/ajax/get-game-info/{0}.json?name={1}'.format(account[1], account[0]), data=data)
    response = get_json(req)
    if not response.get('successful'):
        return None
    
    html = response['html']
    
#     f1 = open('./testfile', 'w+', encoding='utf-8')
#     f1.write(html)
        
    match = QUEUE_RE.search(html)
    if not match:
        return None
    game_type = match.group(1)
    
    match = GAME_TIME_RE.search(html)
    if not match:
        return None
    start_time = int(match.group(1))
    
    match = SPECTATOR_DATA_RE.search(html)
    if not match:
        return None
    
    (server, port, key, game_id, region, version) = match.group(1, 2, 3, 4, 5, 6)
    
    blue_team = []
    purple_team = []
    
    player_matches = re.findall(PLAYER_RE, html)
    champion_matches = re.findall(CHAMPION_NAME_RE, html)
    
    for i in range(5):
        blue_team.append({
            'account': player_matches[i],
            'champion': champion_matches[i]
        })
        purple_team.append({
            'account': player_matches[i + 5],
            'champion': champion_matches[i + 5]
        })
    
    game = Game()
    game.server = server
    game.port = port
    game.key = key
    game.game_id = game_id
    game.region = region
    game.version = version
    game.start_time = start_time
    game.blue_team = blue_team
    game.purple_team = purple_team
    game.type = game_type
    
    return game
示例#2
0
    def FindGamesinPage_NotLoggedIn (name, isItYourTurn, page) :
        games = {}
        #
        #  Get Table Rows
        soup = BeautifulSoup(page)
        #
        #  Whose turn are we looking for?
        if isItYourTurn:
            Turn= "Turn " + name
        else:
            Turn= "Opponent's turn " + name
        #
        # Find the appropriate table
        txt = soup.find(text=Turn)
        #
        # Bomb out if no data found for user
        if (txt) :
            table = txt.findPrevious('table')
        else:
            return []
        #logging.debug( table.prettify() )
        for row in table.findAll('tr',recursive=True):
            #logging.debug( row )
            column = row.findAll('td',recursive=False)
            if (column[0]):
                #
                # Ignore the row if it doesn't have > 2 columns
                if (len(column) > 2):
                    this_game = Game()

                    opponent = column[0].contents[0][2:] # Name starts in 2nd character
                    if (column[1].a) :
                        type = unicode(column[1].contents[1] ).strip() # Skip the hyperlink if found
                    else:
                        type = unicode(column[1].contents[0] ).strip()  # or use contents
                    relpath = column[2].a['href']
                    number = relpath.split("gamenumber=")[1]
                    link = "http://www.yourturnmyturn.com/" + type + "/play.php?gamenumber=" + number
                    link = link.replace(" ", "+")

                    this_game.player=name
                    this_game.opponent=opponent
                    this_game.game = number
                    this_game.type = type
                    this_game.clicklink=link
                    if isItYourTurn:
                        this_game.whoseturn=name
                    else:
                        this_game.whoseturn=opponent
                    games[this_game.game] = this_game

        return games