def CreateFile(fullDir, filename, mode): path = fullDir + os.sep + filename ChkDir(fullDir) file = open(path, mode) print("[%s%s][%s]" % \ (GVAL.EN2CN("Create"), GVAL.EN2CN("File"), path)) return file
def LogStart(file, key): if file == None: return 0 strfTime = time.strftime("%Y.%m.%d %H:%M:%S") file.write("[%s%s][%s][%sID]\n" % \ (GVAL.EN2CN("Start"), GVAL.EN2CN("Time"), GVAL.GetV("keyType"), GVAL.EN2CN("Img"))) file.write("[%s][%s]\n" %(strfTime, key)) file.flush()
def LogInfo(file, key, action, imgID): if file == None: return 0 strfTime = time.strftime("%Y.%m.%d %H:%M:%S") file.write("[%s][%s][%s%s][%s][%sID]\n" % \ (strfTime, action, GVAL.GetV("keyType"), key, GVAL.EN2CN("ImgID"), imgID)) file.flush()
def PrintExt(reason): print("[%s][%s][%s]" % \ (GVAL.EN2CN("Exit"), GVAL.EN2CN("Reason"), GVAL.EN2CN(reason)))
def PrintErrType(): print("[%s%s][%d]" % (GVAL.EN2CN("Error"), (GVAL.EN2CN("Type")), GVAL.GetVInt("poolType")))
def ParsePageOne(urlPage, key, page): download = GVAL.GetVInt("download") #获取html信息 htmlPage = urllib.request.urlopen(urlPage).read().decode("utf-8", "ignore") print("[%s%s][%s]" % (GVAL.EN2CN("Start"), GVAL.EN2CN("Url"), urlPage)) count = 0 findIdx = re.findall(GVAL.GetImgRegex(), htmlPage) #print(findIdx) for img in findIdx: imgID = img[5:] try: startTime = time.time() count += 1 action = "" if download: action = GVAL.EN2CN("Download") else: action = GVAL.EN2CN("GetAddress") print("[%s][%s %s][%s %s][%s %.3d][%s %.3d][%s]" % \ (action, GVAL.EN2CN(GVAL.GetV("keyType")), key, GVAL.EN2CN("ImgID"), imgID, GVAL.EN2CN("Page"), page, GVAL.EN2CN("Count"), count, GVAL.EN2CN("Start"))) urlImage = GVAL.GetV("urlImgPre") + imgID htmlImage = urllib.request.urlopen(urlImage).read().decode( "utf-8", "ignore") photosFind = CFUN.DelRepeat( re.findall(GVAL.GetAddrRegex(), htmlImage)) if (len(photosFind) == 0): continue urlPhoto = GVAL.GetV("urlFilePre") + photosFind[0] namePhoto = CFUN.NameReplace(photosFind[0]) if download: ret = DowloadImg(urlPhoto, namePhoto, key) else: ret = ParseAddress(urlPhoto, namePhoto, key) endTime = time.time() print("[%s][%s %s][%s %s][%s %.3d][%s %.3d][%s %s][%s %3d][%s]" % \ (action, GVAL.EN2CN(GVAL.GetV("keyType")), key, GVAL.EN2CN("ImgID"), imgID, GVAL.EN2CN("Page"), page, GVAL.EN2CN("Count"), count, GVAL.EN2CN("Ret"), ret, GVAL.EN2CN("Cost"), endTime - startTime, GVAL.EN2CN("Sec"))) if ret == 0: CFUN.LogInfo(GVAL.GetSuccFile(), key, action, imgID) elif ret < 0: CFUN.LogInfo(GVAL.GetFailFile(), key, action, imgID) except urllib.error.URLError or socket.gaierror or NameError or ConnectionAbortedError as e: LogInfo(GVAL.GetFailFile(), pool, action, imgID) continue print("[%s%s][%s][%s][%s %d]" % (GVAL.EN2CN("End"), GVAL.EN2CN("Url"), urlPage, GVAL.EN2CN("Success"), count)) if (len(findIdx) == 0): return -1