예제 #1
0
def get_game_info(url_path, match_date):
    url_path = 'http://www.nba.com' + url_path
    print url_path
    html = get_response(url_path)
    get_box_score(html, url_path, match_date)
    pass


    # get_game_info('http://www.nba.com/games/20111220/DETCLE/gameinfo.html', '2011-12-30')
예제 #2
0
def print_url(match_date, format_match_date):
    url_path = 'http://www.nba.com/gameline/' + match_date + '/'
    print url_path
    html = get_response(url_path)
    if html:
        soup = BeautifulSoup(html)
        game_set = set()
        for tag in soup.find_all(href=re.compile("^/games/" + match_date + "/")):
            if tag.string == 'recap':
                game_set.add(tag['href'])
        for tag in game_set:
            get_game_info(tag, format_match_date)
            pass
예제 #3
0
__author__ = 'chenghao'

from my_urllib import get_response
import json
import time
import datetime
from mysql_connector import get_connector
import mysql.connector

conn = get_connector()
while True:
    url_path = 'http://china.nba.com/wap/static/data/season/schedule_7.json'
    html = get_response(url_path)
    if html:
        dates = json.loads(html)['payload']['dates']
        for date in dates:
            utcMillis = date['utcMillis']
            utcMillis = utcMillis.strip()
            utcMillis = int(utcMillis)
            game_date = datetime.date.fromtimestamp(utcMillis / 1000).isoformat()
            games = date['games']
            for game in games:
                status = game['boxscore']['status']
                status = status.strip()
                status = int(status)
                if status != 1:
                    sql = 'update next_match set status = %s'
                    cursor = conn.cursor()
                    cursor.execute(sql, (status,))
                    cursor.close()
                    conn.commit()
예제 #4
0
def game_live(conn,gameid):
    request_time = 0
    clean_flag = True
    init = False
    sql = 'update next_match set status = 2 where gameid = %s'
    cursor = conn.cursor()
    cursor.execute(sql,(gameid,))
    cursor.close()
    conn.commit()
    conn.close()
    while True:
        url_path = 'http://china.nba.com/wap/static/data/game/snapshot_'+str(gameid)+'.json'
        html = get_response(url_path)
        if not html :
            request_time += 1
            clean_flag = True
            if request_time < 30:
                continue
            else:
                break
        html = html.decode("utf-8-sig")
        payload = json.loads(html)
        payload = payload['payload']
        status = payload['boxscore']['status']
        status = int(status)
        if not init:
            if clean_flag:
                sql = 'delete from live where gameid = %s'
                cursor = conn.cursor()
                cursor.execute(sql,(gameid,))
                cursor.close()
                conn.commit()
                clean_flag = False
            period = payload['boxscore']['period']
            period = int(period)
            for i in range(1,period,1):
                data = []
                url_path = 'http://china.nba.com/wap/static/data/game/playbyplay_'+gameid+'_'+unicode(i)+'.json'
                html = get_response(url_path)
                if not html:
                    request_time += 1
                    clean_flag = True
                    if request_time < 10:
                        continue
                    else:
                        break
                html = html.decode("utf-8-sig")
                playbyplay = json.loads(html)
                payload = playbyplay['payload']
                events = payload['playByPlays'][0]['events']
                for j in range(len(events) - 1,-1,-1):
                    event = events[j]
                    awayScore = unicode(event['awayScore'])
                    homeScore = unicode(event['homeScore'])
                    gameClock = unicode(event['gameClock'])
                    description = unicode(event['description'])
                    playerId = unicode(event['playerId'])
                    teamId = unicode(event['teamId'])
                    m = (gameid,i,gameClock,awayScore,homeScore,description,teamId,playerId,len(events) - j)
                    print m
                    data.append(m)
                cursor = conn.cursor()
                sql = 'insert into live VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)'
                cursor.executemany(sql,data)
                cursor.close()
                conn.commit()
            if clean_flag:
                continue
            init = True

        loop_time404 = 0
        while True:
            data = []
            url_path = 'http://china.nba.com/wap/static/data/game/playbyplay_'+gameid+'_'+unicode(period)+'.json'
            html = get_response(url_path)
            if not html:
                loop_time404 += 1
                if loop_time404 < 1000:
                    continue
                else:
                    break
            html = html.decode("utf-8-sig")
            playbyplay = json.loads(html)
            payload = playbyplay['payload']
            events = payload['playByPlays'][0]['events']
            sql = 'select * from live where gameid = %s and period = %s'
            cursor = conn.cursor()
            cursor.execute(sql,(gameid,period,))
            values = cursor.fetchall()
            for i in range(len(events) - len(values) -1,-1,-1):
                event = events[i]
                awayScore = unicode(event['awayScore'])
                homeScore = unicode(event['homeScore'])
                gameClock = unicode(event['gameClock'])
                description = unicode(event['description'])
                playerId = unicode(event['playerId'])
                teamId = unicode(event['teamId'])
                m = (gameid,period,gameClock,awayScore,homeScore,description,teamId,playerId,len(events)-i)
                print m
                data.append(m)
            cursor = conn.cursor()
            sql = 'insert into live VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)'
            cursor.executemany(sql,data)
            cursor.close()
            conn.commit()
            now_period = payload['boxscore']['period']
            now_period = int(now_period)
            if now_period > period:
                period = now_period
                continue
            if status == 3:
                sql = 'update next_match set status = 3 where gameid = %s'
                cursor = conn.cursor()
                cursor.execute(sql,(gameid,))
                cursor.close()
                conn.commit()
                conn.close()
                return
    conn.close()