import os
import json
import requests
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):
    req = requests.get(url)
    with open(path,'wb') as f:
        f.write(req.content)
data_path = '/home/zyh/Data/manhua'
def check_down(t):
    with open(os.path.join(data_path,'img_check'),'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'),'r')as f:
        dict = json.load(f)
    dict[t] = 1
    with open(os.path.join(data_path,'img_check'),'w') as f:
        json.dump(dict,f)

if __name__ == '__main__':
    for i in range(1,2):
        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))
