Crawling epidemic data and previewing with Markdown

Keywords: Mobile JSON Python Lambda

Friday is not sleepy. I wrote a Python script with simple functions: get Sina's epidemic data about each country, write it into md file and preview it, get the data regularly, and generate new markdown content at the end of the file if there is new data.

1, Code

Because the function and code are very simple, go directly to the code

# -*-coding:utf8-*-
import re,requests,json,pprint,time
import os

pattern=re.compile(r'^try{sinajp_15844213244528328543098388435\((.*?)\);}catch\(e\){};')
lasttimes='00:00:00'

while True:
    res=requests.get('https://gwpre.sina.cn/ncp/foreign?_=1584421324452&callback=sinajp_15844213244528328543098388435')
    match=pattern.search(res.text)

    if match:
        obj=json.loads(match.group(1))
        resultObj=obj['result']
        times=resultObj['times']  # Deadline
        timesMatch=re.search(r'By(\d{2})month(\d{2})day(\d{2})Time(\d{2})branch',times)
        if timesMatch:
            times=timesMatch.group(1)+'month'+timesMatch.group(2)+'day '+timesMatch.group(3)+':'+timesMatch.group(4)
        
        if times==lasttimes:
            continue
        else:
            lasttimes=times
            totalObj=resultObj['total']
            certain=totalObj['certain'] # Cumulative diagnosis
            die=totalObj['die']   # death
            recure=totalObj['recure'] # Cure
            certain_inc=totalObj['certain_inc'] # Diagnosed increase
            die_inc=totalObj['die_inc'] # Death increase
            recure_inc=totalObj['recure_inc'] # Cure increase
            # Country data list
            worldlistArr=resultObj['worldlist']
            worldlistArr.sort(key=lambda x: int(x.get('conNum','0')),reverse=True)
            
            fo=open('./coronavirus.md','a')
            fo.writelines('\n# '+times+'\n')
            fo.writelines('Total number of countries infected:'+str(len(worldlistArr))+'\n')
            fo.writelines('```\n Cumulative diagnosis:'+certain.rjust(10,' ')+' Yesterday:'+certain_inc+'\n'+'Cumulative death:'+die.rjust(10,' ')+' Yesterday:'+die_inc+'\n'+'Cumulative cure:'+recure.rjust(10,' ')+' Yesterday:'+recure_inc+'\n```\n')

            fo.writelines('|Country|Newly diagnosed|Cumulative diagnosis|New death|Cumulative death|Cumulative cure|'+'\n')
            fo.writelines('|:--:|---:|---:|---:|---:|---:|'+'\n')

            top15=worldlistArr[:15]
            pattient_countrys=['Australia','Canada','Brazil','India','Denmark','Vietnam?','Singapore','Russia','Serbia','Pakistan',]
            pattient=[c for c in worldlistArr if c['name'] in pattient_countrys]

            for countryObj in top15:
                name=countryObj['name'] # Country
                if name=='China':
                    continue
                conadd=countryObj['conadd'] # Newly diagnosed
                conNum=countryObj['conNum'] # Cumulative diagnosis
                deathadd=countryObj['deathadd'] # New death
                deathNum=countryObj['deathNum'] # Cumulative death
                cureNum=countryObj['cureNum'] # Cumulative cure
                fo.writelines('|'+name+'|'+conadd+'|'+conNum+'|'+deathadd+'|'+deathNum+'|'+cureNum+'|\n')

            fo.writelines('\n Special care'+'\n')
            fo.writelines('|Country|Newly diagnosed|Cumulative diagnosis|New death|Cumulative death|Cumulative cure|'+'\n')
            fo.writelines('|:--:|---:|---:|---:|---:|---:|'+'\n')
            for countryObj in pattient:
                name=countryObj['name'] # Country
                conadd=countryObj['conadd'] # Newly diagnosed
                conNum=countryObj['conNum'] # Cumulative diagnosis
                deathadd=countryObj['deathadd'] # New death
                deathNum=countryObj['deathNum'] # Cumulative death
                cureNum=countryObj['cureNum'] # Cumulative cure
                fo.writelines('|'+name+'|'+conadd+'|'+conNum+'|'+deathadd+'|'+deathNum+'|'+cureNum+'|\n')
            fo.close()
    
    # Open. md file with Markdown IDE for preview
    os.system('open -a "/Applications/Typora.app" ./coronavirus.md')

    for i in range(1,61):
        time.sleep(10)
        print(i*10)2

2, How to use

  1. Install requests
pip3 install requests
  1. Modify the opening mode of Markdown. Since the Marodown editor installed on my computer is Typora, the script is open - a "/ applications / Typora. app". / coronavirus.md. Modify the * * *. app here as your own ide

  2. Terminal operation

python3 coronavirus.py

Posted by vornn on Sat, 11 Apr 2020 07:45:44 -0700