import os
import json
import requests
import etc
import re
import time
def check_dir(dir):
    if (not os.path.exists(dir)) or (not os.path.isdir(dir)):
    #     print dir
    #     os.mkdir(unicode(dir,"utf8"))
        os.mkdir(dir)
def save_img(path,url):
    if os.path.exists(path):
        print "had down the page"
        return
    req = requests.get(url)
    time.sleep(3)
    with open(path,'wb') as f:
        f.write(req.content)
data_path = etc.data_path

img_check_name = 'img_check'

def check_file(file):
    print "checkfile"
    print os.path.exists(file)
    if not os.path.exists(file):
        print "not existfile",file
        dict = {}
        dict['-1'] = 1
        with open(file,'w')as f:
            json.dump(dict,f)
def check_down(t):
    with open(os.path.join(data_path,img_check_name),'r')as f:
        dict = json.load(f)
    if t in dict:
        return True
    return False
def get_down(t):
    with open(os.path.join(data_path,img_check_name),'r')as f:
        dict = json.load(f)
    dict[t] = 1
    with open(os.path.join(data_path,img_check_name),'w') as f:
        json.dump(dict,f)

if __name__ == '__main__':
    check_file(os.path.join(data_path,img_check_name))
    for file in os.listdir(data_path):
        print file
        pat = re.compile('(\d*?)_img_url_json')
        zhang_id_list =  pat.findall(file)
        if len(zhang_id_list) == 1:
            i = zhang_id_list[0]
            path = os.path.join(data_path,str(i))
            if check_down(str(i)):
                print 'had_down'
                continue
            check_dir(path)
            with open(path+'_img_url_json','r')as f:
                img_dict = json.load(f)
            for j in img_dict:
                print j
                print img_dict[j]
                save_img(os.path.join(path,str(j)),img_dict[j])
            get_down(str(i))
