def inputAuthCode(self, driver, inputter): divAuthCodeId = 'input-code' divImgVerfiyName = '//div[@class="txt-imgverify"]' imgAutoCodeId = 'imgCode' inputAuthCodeId = 'code' loginButtonId = 'loginBtn' msg = None element = findElement(driver, divAuthCodeId) if element is None or not element.is_displayed( ) or not element.is_enabled(): return False print 'Auth code is needed.' element = findElement(driver, divImgVerfiyName) if element is not None: msg = element.text element = findElement(driver, imgAutoCodeId) if element is None: return True ImageKit.saveCapture(driver, element, self.image) element = findElement(driver, inputAuthCodeId) if element is None: return False prompt = element.get_attribute('placeholder') content = inputter.getInput(None, msg, self.image, prompt, 4) if content is None: print 'Auth code is not inputted.' return True element.clear() element.send_keys(content) print 'Auth code is sent.' time.sleep(1) element = findElement(driver, loginButtonId) element.click() time.sleep(3) # Sleep a little longer return True
def saveScreenshot(self, driver, screenshotPath): if screenshotPath is None: return with open(self.source, 'w') as fp: page = driver.page_source fp.write(page) driver.save_screenshot(self.originalImage) ImageKit.resize(screenshotPath, self.originalImage, newSize=(400, 300))
def getComplexImage(self): content = self.getHtml() path = OutputPath.getDataPath('sku-{}-complex'.format(self.skuid), 'html') with open(path, 'w') as fp: fp.write(content) chmod(path) return ImageKit.fromHtml(path, pageSize=(80, 150))
def search(self, content): r = Network.post(self.url, data={'content': content}) if r is None: print 'No result for', content return False try: obj = json.loads(r.content.decode('utf-8', 'ignore')) except ValueError as e: print 'Error (', e, ') of json: "', r.content, '"' return False num = obj['num'] if num is 0: print 'Error content: "', r.content, '"' return False print 'Found', num, 'SKU with "', content, '"' datas = obj['list'] plates = list() urls = list() for data in datas: formatter = SpecialFormatter.create(data) plate = formatter.getPlate(self.qwd) url = data['skuimgurl'] plates.append(plate) urls.append(url) now = datetime.now().strftime('%Y_%m_%d_%H_%M_%S') path = OutputPath.getDataPath('search_{}'.format(now), 'jpeg') self.plate = '\n----------------------------\n'.join(plates) self.image = ImageKit.concatUrlsTo(path, urls) return True
def plogin(self): def isValidAuthCode(code): if code is None or len(code) != 4: return False for c in code: if c.isdigit(): continue if c.isalpha(): continue # Wrong word return False return True if self.pCookies is not None: return True # https://github.com/mozilla/geckodriver/releases # browser = webdriver.Firefox() # https://chromedriver.storage.googleapis.com/index.html browser = webdriver.Chrome() try: # Plogin browser.get(self.ploginUrl) # Login by username and password # Username and password randomSleep(1, 2) inputElement(browser.find_element_by_id('username'), self.pin) randomSleep(1, 2) inputElement(browser.find_element_by_id('password'), self.password) # Submit buttonElement = browser.find_element_by_id('loginBtn') # Code codeElement = browser.find_element_by_id('code') imageElement = browser.find_element_by_id('imgCode') times = 0 if codeElement.is_displayed(): while codeElement.is_displayed() and times < 50: times += 1 # Image to text path = OutputPath.getAuthPath(self.pin) ImageKit.saveCapture(browser, imageElement, path) code = ImageKit.getText(path) codeElement.send_keys(code) if not isValidAuthCode(code): # Refresh auth code randomSleep(0.5, 1) imageElement.click() # Wait for updating auth code randomSleep(1, 2) codeElement.clear() continue # Submit randomSleep(1, 2) buttonElement.click() error = self.getBrowserError(browser) if error is None: print 'Succeed after', times, 'tries.' break if u'验证码' not in error: raise Exception('Unable to login for "{}": {}'.format( self.pin, error)) randomSleep(1, 2) codeElement.clear() randomSleep(1, 2) else: raise Exception('Unable to login for "{}"'.format( self.pin)) else: # Submit randomSleep(1, 2) buttonElement.click() wait = WebDriverWait(browser, 3) error = self.getBrowserError(browser) if error is not None: raise Exception('Unable to login for "{}": {}'.format( self.pin, error)) print 'Loginned for', self.pin # Redirect to wqs qwsUrl = getProperty(self.configFile, 'cps-qwd-wqs-url') browser.get(qwsUrl) time.sleep(10) # Save as type of cookie for requests self.pCookies = dict() for cookie in browser.get_cookies(): k = cookie['name'] v = cookie['value'] self.pCookies[k] = v except Exception as e: print e finally: browser.quit()
def verify(self, driver, inputter): verifyBodyName = '//div[@class="verify-body"]' verifyMsgName = '//p[@class="verify-msg"]' verifyNoticeName = '//div[@class="verify-notice"]' verifyInputName = '//input[@class="verify-input"]' verifyContinueName = '//a[@class="verify-continue"]' time.sleep(1) # Verification element = findElement(driver, verifyBodyName) if element is None or not element.is_displayed( ) or not element.is_enabled(): return False print 'Verification is needed ...' ImageKit.saveCapture(driver, element, self.image) element = findElement(driver, verifyNoticeName) if element is not None: notice = element.text else: notice = None element = findElement(driver, verifyMsgName) if element is not None: msg = element.text else: msg = None element = findElement(driver, verifyInputName) if element is not None: prompt = element.get_attribute('placeholder') content = inputter.getInput(notice, msg, self.image, prompt) if content is None: print 'Verification code is not inputted.' return element.send_keys(content) print 'Verification code is sent.' time.sleep(2) element = findElement(driver, verifyContinueName) if element is not None: element.click() time.sleep(3) # Sleep a little longer return True
def plogin(self, retries=0, force=False): def isValidAuthCode(code): if code is None or len(code) != 4: return False for c in code: if c.isdigit(): continue if c.isalpha(): continue # Wrong word return False return True if self.pCookies is not None: return True if force: self.entryCookies = None self.keyCookies = None if retries is 0: pass elif retries is 1: self.keyCookies = None elif retries is 2: self.entryCookies = None else: return False cookies = None if self.entryCookies is not None: try: cookies = json.loads(self.entryCookies.decode('utf-8', 'ignore')) except ValueError as e: pass if cookies is None: self.dbUpdated = True display = Display(visible=0, size=(800, 600)) display.start() if 'firefox' == self.ploginType: # https://github.com/mozilla/geckodriver/releases browser = webdriver.Firefox() else: # Chrome # https://chromedriver.storage.googleapis.com/index.html browser = webdriver.Chrome() try: # Plogin browser.get(self.ploginUrl) # Login by username and password # Username and password randomSleep(1, 2) inputElement(browser.find_element_by_id('username'), self.username) randomSleep(1, 2) inputElement(browser.find_element_by_id('password'), self.password) # Submit buttonElement = browser.find_element_by_id('loginBtn') # Code codeElement = browser.find_element_by_id('code') imageElement = browser.find_element_by_id('imgCode') times = 0 if codeElement.is_displayed(): print 'Auth code is needed ...' while codeElement.is_displayed() and times < 50: times += 1 # Image to text path = OutputPath.getAuthPath(self.username) ImageKit.saveCapture(browser, imageElement, path) code = ImageKit.getText(path) codeElement.send_keys(code) if not isValidAuthCode(code): # Refresh auth code randomSleep(0.5, 1) imageElement.click() # Wait for updating auth code randomSleep(1, 2) codeElement.clear() continue # Submit randomSleep(1, 2) buttonElement.click() error = self.getBrowserError(browser) if error is None: print 'Succeed after', times, 'tries.' break if u'验证码' not in error: raise Exception('Unable to login for "{}": {}'.format(self.userId, error)) randomSleep(1, 2) codeElement.clear() randomSleep(1, 2) else: raise Exception('Unable to login for "{}"'.format(self.userId)) else: # Submit randomSleep(1, 2) buttonElement.click() wait = WebDriverWait(browser, 3) error = self.getBrowserError(browser) if error is not None: raise Exception('Unable to login for "{}": {}'.format(self.userId, error)) print 'Loginned for user', self.userId, 'with type', self.loginType # Redirect to wqs time.sleep(1) # Save as type of cookie for requests cookies = dict() for cookie in browser.get_cookies(): k = cookie['name'] v = cookie['value'] cookies[k] = v self.entryCookies = reprDict(cookies) except Exception as e: print 'Unable to get entry cookie with an error:\n', e return False finally: browser.quit() if display is not None: display.stop() # Update pCookies self.pCookies = cookies cookies = None if self.keyCookies is not None: try: cookies = json.loads(self.keyCookies.decode('utf-8', 'ignore')) except ValueError as e: pass if cookies is None: self.dbUpdated = True try: # Headers headers = {'User-Agent': self.userAgent} params = {'rurl': self.wqsUrl} r = requests.get(self.wqUrl, params=params, cookies=self.pCookies, headers=headers, allow_redirects=False) except Exception as e: print 'Unable to get key cookie with an error:\n', e return False cookies = dict() for cookie in r.cookies: cookies[cookie.name] = cookie.value self.keyCookies = reprDict(cookies) # Update pCookies self.pCookies.update(cookies) return True
def prepare(self): self.width = int(self.contentConfig['width']) self.height = int(self.contentConfig['height']) # Logo: logo = self.contentConfig['logo'] if logo: prefix = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'logo.original') logo = Network.saveUrl(prefix, logo) else: logo = getProperty(self.configFile, 'logo-path') if logo: self.logo = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'logo.jpg') logoWidth = int(self.contentConfig['logo-width']) logoHeight = int(self.contentConfig['logo-height']) print('Create logo', self.logo, 'from', logo) ''' cmd = 'ffmpeg -y -i {} -vf scale="{}:{}" {}'.format(logo, logoWidth, logoHeight, self.logo) runCommand(cmd) ''' ImageKit.stretch(self.logo, logo, (logoWidth, logoHeight)) else: self.logo = None # Background: background = self.contentConfig['background'] if background: prefix = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'background.original') background = Network.saveUrl(prefix, background) else: background = getProperty(self.configFile, 'background-path') if background: self.background = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'background.jpg') scalePath = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'bg-scale.jpg') print('Create background', self.background, 'from', background) ''' cmd = 'ffmpeg -y -i {} -vf scale="{}:{}" {}'.format(background, self.width, self.height, self.background) runCommand(cmd) ''' ImageKit.crop(scalePath, background, (self.width, self.height)) ImageKit.blurdim(self.background, scalePath) else: self.background = None # Create silence self.silencePath = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'silence.mp3') print('Create silence in', self.silencePath) cmd = 'ffmpeg -y -f lavfi -i anullsrc=r=22050:cl=mono -t 1 -q:a 9 -acodec libmp3lame {}'.format(self.silencePath) runCommand(cmd) # To m4a audioPath = '{}m4a'.format(self.silencePath[:-3]) print('Translate', self.silencePath, 'to', audioPath) cmd = 'ffmpeg -y -i {} -vn -acodec aac -strict -2 \'-bsf:a\' aac_adtstoasc {}'.format(self.silencePath, audioPath) runCommand(cmd) # Create separator between videos separatorPath = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'image.mp4') print('Create separator in', separatorPath) cmd = 'ffmpeg -y -loop 1 -i {} -c:v libx264 -t 1 -pix_fmt yuv420p {}'.format(self.background, separatorPath) runCommand(cmd) # Merge image and audio self.separatorPath = os.path.join(OutputPath.DATA_OUTPUT_PATH, 'separator.mp4') print('Merge', separatorPath, 'and', audioPath, 'to', self.separatorPath) cmd = 'ffmpeg -y -i {} -i {} -c copy -map \'0:v:0\' -map \'1:a:0\' {}'.format(separatorPath, audioPath, self.separatorPath) runCommand(cmd)
def saveImages(self, urls): def saveImage(path, index, url): prefix = os.path.join(path, '{}.original'.format(index)) # TODO: if it already exists pathname = '{}.original.png'.format(prefix) if os.path.exists(pathname): return pathname pathname = '{}.original.jpg'.format(prefix) if os.path.exists(pathname): return pathname pathname = '{}.original.gif'.format(prefix) if os.path.exists(pathname): return pathname return Network.saveUrl(prefix, url) index = 0 for url in urls: if not url: continue imagePath = saveImage(self.path, index, url) if imagePath is not None: # To jpg if not imagePath.endswith('.jpg'): oldPath = imagePath imagePath = os.path.join(self.path, '{}.original.jpg'.format(index)) print('Translate', oldPath, 'to', imagePath) cmd = 'ffmpeg -y -i {} {}'.format(oldPath, imagePath) runCommand(cmd) # Backgroud image cropPath = os.path.join(self.path, '{}.crop.jpg'.format(index)) bgPath = os.path.join(self.path, '{}.bg.jpg'.format(index)) print('Create background to', bgPath) ImageKit.crop(cropPath, imagePath, (self.width, self.height)) ImageKit.blurdim(bgPath, cropPath) # Scale image scalePath = os.path.join(self.path, '{}.scale.jpg'.format(index)) print('Scale', imagePath, 'to', scalePath) cmd = 'ffmpeg -y -i {0} -vf scale="\'if(gt(a,{1}/{2}),{1},-1)\':\'if(gt(a,{1}/{2}),-1,{2})\'" {3}'.format(imagePath, self.width, self.height, scalePath) runCommand(cmd) # Overlay background overlayPath = os.path.join(self.path, '{}.jpg'.format(index)) print('Overlay', bgPath, 'to', overlayPath) cmd = 'ffmpeg -y -i {} -i {} -filter_complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" {}'.format(bgPath, scalePath, overlayPath) runCommand(cmd) index += 1 self.imageCount = index