Live NBA Text Widget Based on Python Command Line

The NBA playoff is in progress and has no choice but to go to work and watch live video.And the text live page has too many advertisements and so on, so it takes half a day to make a command line-based text live broadcast with Python 3, which is refreshing to watch but not easy for leaders to discover.The effect is as shown in the diagram:

Figure 1: When the program starts, all current competitions are listed


Figure 2: After entering the contest ID, the text is broadcast live


Find a circle of live NBA text websites and find Live on Mobile There is an existing interface that directly returns data in json format.That's it. Listen to me slowly.

Open in the computer browser first Live on Mobile I'm using the chrome browser, which you can see in the Network that it keeps requesting http://bifen4m.qiumibao.com/json/list.htm by GET. This address returns the basics of all types of games currently in progress and filters out non-NBA matches based on the type field.The most important of these is the ID field, which is needed for all subsequent operations.The data returned is as follows:

{
    "code": "2760624",
    "second": "10",
    "list": [
        {
            "id": "96233",
            "sdate": "2017-04-20",
            "time": "10:30",
            "url": "/zhibo/nba/2017/042096233.htm",
            "type": "basketball",
            "start": "2017-04-20 10:30",
            "home_team": "Warrior",
            "visit_team": "Pioneer",
            "home_score": "106",
            "visit_score": "81",
            "period_cn": "Section 4\n01:30",
            "from": "dc.live",
            "code": "373",
            "update": "13:13:37",
            "big_score_1": "",
            "big_score_2": ""
        },
        ... # Information for other contests was omitted
    ]
}

Once you have the IDs of all the Games in progress, click on a game to enter the text live page.First request the http://dingshi4pc.qiumibao.com/livetext/data/cache/max_sid/XXXX/0.htm page, where XXXX is the ID obtained in the previous step and returns a number, max_sid.Then determine if the max_sid is greater than the last value obtained, and if it is greater, it means there is new live text, otherwise it means there is No.

If the max_sid is greater than the last value, return to the basics of the game, such as the score, the sections and so on, by requesting http://bifen4pc2.qiumibao.com/json/XXXX/YYYY.htm (where XXXX is today's date in the format 2017-04-20 and YYY is the id you got in the first step):

{
    "id": "96233",
    "home_team": "Warrior",
    "visit_team": "Pioneer",
    "home_score": "110",
    "visit_score": "81",
    "period_cn": "End of Section 4",
    ...
}

Finally, you can get the live text.Request http://dingshi4pc.qiumibao.com/livetext/data/cache/livetext/XXXX/0/lit_page_2/YYYY.htm (where XXXX is the competition id and YYYY is max_sid), which returns the latest live text, including one or more live text, as follows:

[
    {
        "live_id": "8769977",
        "live_text": "@Fairy Cua-: The maximum positive and negative value of Curie is 32 I will go!!!!",
        "home_score": "110",
        "visit_score": "81",
        "pid_text": "End of Competition",
        ...
    },
    ... # There may be more than one live text
]

You can see that the request returns no information about the remaining time of the game, the home team and the guest team, so you need to request more than once to get the basic information of the game before you can get the live text.

This is the basic process, which is very simple, returning four strings of json s for four GET requests, using the requests library requests, and parsing them.

Define a Match class to represent each game currently in progress.

# match.py

class Match:
    def __init__(self, **kwargs):
        self.id = kwargs['id']
        self.home_team = kwargs['home_team']
        self.visit_team = kwargs['visit_team']
        self.home_score = kwargs['home_score']
        self.visit_score = kwargs['visit_score']
        self.period_cn = kwargs['period_cn'].replace('\n', ' ')

    def __repr__(self):
        return '{self.id} {self.home_team} {self.home_score} - {self.visit_score} {self.visit_team} {self.period_cn}'.format(self=self)

Define a TextLiving class to represent each text broadcast you get.

# text_living.py

class TextLiving:
    def __init__(self, match_info, **kwargs):
        self.home_team = match_info['home_team']
        self.visit_team = match_info['visit_team']
        self.period_cn = match_info['period_cn']
        self.live_text = kwargs['live_text']
        self.home_score = kwargs['home_score']
        self.visit_score = kwargs['visit_score']

    def __repr__(self):
        return '{self.home_team} {self.home_score} - {self.visit_score} {self.visit_team} {self.period_cn}\n{self.live_text}\n{sep}'.format(self=self, sep='*'*60)

Next, create the zhibo8_api.py module to get the relevant data.

# Competition currently in progress
Living_Matches_Url = 'http://bifen4m.qiumibao.com/json/list.htm'

# Current max_sid for a game
Match_Max_Sid_Url = 'http://dingshi4pc.qiumibao.com/livetext/data/cache/max_sid/%s/0.htm'
# Live broadcast of the latest text in a game
Match_Living_Text_Url = 'http://dingshi4pc.qiumibao.com/livetext/data/cache/livetext/%s/0/lit_page_2/%d.htm'
# Current basics of a game
Match_Info_Url = 'http://bifen4pc2.qiumibao.com/json/%s/%s.htm'


def get_living_matches():
    response = requests.get(Living_Matches_Url)
    result = json.loads(response.text)
    matches = [Match(**match) for match in result['list'] if match['type'] == 'basketball' and match['period_cn'] != 'Finish the race']
    return matches


def get_match_max_sid(match_id):
    response = requests.get(Match_Max_Sid_Url % match_id)
    if response.status_code == requests.codes.ok:
        return int(response.text)


def get_match_living(match_id, max_sid):
    # Get the current state of the game before getting the latest live text
    match_info = get_match_info(match_id)

    response = requests.get(Match_Living_Text_Url % (match_id, max_sid))

    texts = []
    if response.status_code == requests.codes.ok:
        result = json.loads(response.text)
        texts = [TextLiving(match_info, **living) for living in result]
    return texts


def get_match_info(match_id):
    today = datetime.now().strftime('%Y-%m-%d')
    response = requests.get(Match_Info_Url % (today, match_id))
    match_info = json.loads(response.text)
    return match_info

Finally, start the program in the main.py module and start live broadcasting!

def get_living_matches():
    matches = zhibo8_api.get_living_matches()
    for match in matches:
        print(match)
    return matches

def get_watch_match(matches):
    match_id = input('Please enter the contest ID: ')
    for match in matches:
        if match.id == match_id:
            return match
    else:
        print('Input ID Incorrect')
        return None

def main_loop():
    matches = get_living_matches()
    if len(matches) == 0:
        print('There is no competition now!!!')
        return

    match = get_watch_match(matches)
    if not match:
        print('Didn't find the match')
        return

    current_match_max_sid = -1
    while True:
        match_max_sid = zhibo8_api.get_match_max_sid(match.id)
        if not match_max_sid:
            print('No live data')
            return

        if current_match_max_sid == match_max_sid:
            continue

        current_match_max_sid = match_max_sid
        text_livings = zhibo8_api.get_match_living(match.id, current_match_max_sid)
        for text in text_livings:
            print(text)

if __name__ == '__main__':
    main_loop()

There are basically no exception handling in the program, and there are many areas that need to be improved. You are welcome to teach.If my friend needs it, I'll put the code on GitHub.

Keywords: JSON Mobile Python network

Added by Deivas on Sun, 07 Jul 2019 23:23:48 +0300