#!/usr/bin/env python3.8
#!coding=utf-8
# 爬取数据并保存
import re
import os
import urllib.request as urllib2
from urllib import error
import http.client
import ssl
#import json
########
#from time import time, asctime, localtime
#import matplotlib.pyplot as plt
#import numpy as np
#from sys import argv
#
print('-'*10,'import end','-'*10)


def saveDataOneDay(filename, newdatadict):
    # 保存单日数据
#    print(newdatadict)
#    filename = os.path.join(datapath, filename)
    ff = open(filename, 'w', encoding='utf-8')
    print(newdatadict, file=ff)
    ff.close()
#    print('savein--->', newtimekey)
    print('datasave')
   #^^^^^^^END



def request_content(url,contentpattern):
    ######## url+orbitcontent to items
    print('ininin')
    ssl._create_default_https_context = ssl._create_unverified_context
    ########
    try:
        response = urllib2.urlopen(url)#, headers=headers)
    except error.HTTPError as e:
        print(e.reason)
        print(e.code)
        print(e.headers)
    except error.URLError as e:
        print(e.reason)
    ########
    try:
        print('try2')
        content = response.read().decode('utf-8')
    except http.client.IncompleteRead as e:
        print('except3')
        content = e.partial.decode('utf-8', 'ignore')
    ########
    pattern=re.compile(contentpattern,re.S)
    items=re.findall(pattern,content)
    return items
   #^^^^^^^END


def newDXYweb(url, pattern1):
    ######################## 读取当日数据
    ######## 正文
    items_url= request_content(url, pattern1)
#    print(len(items_url))
    print('#'*20, 'read DXYweb end', '#'*20)
    return items_url[0].rstrip('catch(e){}')
   #^^^^^^^END


###############################################################################
if __name__ == '__main__':
#    url = 'https://3g.dxy.cn/newh5/view/pneumonia'
    url = 'https://ncov.dxy.cn/ncovh5/view/pneumonia'
    ######## 读入丁香园数据
    #newdatadict = newDXYweb(url)
    ######## 保存数据
#    saveDataOneDay('/Users/shzy/2019nCov-dataset', 'shiyan.txt', newdatadict)
#    savedatalst('savedata.txt', newdatadict)
