def run(configFile, userId, name, shareFile, index, savefile, logFile): OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name, logFile) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work try: db = None qwd = None db = Database(configFile, 'register') db.initialize() qwd = QWD(db, userId) viewer = Viewer(configFile, qwd) data = viewer.get(shareFile, index) if savefile is not None: with open(savefile, 'w') as fp: fp.write(reprDict(data)) else: print reprDict(data) except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
def run(configfile, name, filename): OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name) thread.start() #sys.stdout = thread #sys.errout = thread # XXX: Actually, it does NOT work try: looper = CurrencyLooper(configFile) #looper.run(filename) looper.runWithVoice(filename) #CurrencyLooper.autorun() except KeyboardInterrupt: pass except Exception as e: print('Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) traceback.print_exc(file=sys.stdout) thread.quit() thread.join()
def run(configFile, userId, name, logFile): OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name, logFile) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work try: db = None qwd = None db = Database(configFile, 'register') db.initialize() qwd = QWD(db, userId) qwd.updateDb(force=True) except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
def generate(self, tts, content, silencePath): text = self.getValue(content, 'text') self.name = self.getValue(content, 'name') name = self.name if name: try: OutputPath.createDataPath(name) except UnicodeEncodeError as e: name = None print(e) if not name: m = hashlib.md5() m.update(text.encode('utf-8')) name = m.hexdigest()[:8] OutputPath.createDataPath(name) self.path = OutputPath.getDataPath(name) self.prepare() self.saveImages(content['image-urls-list']) self.generateTts(tts, text, silencePath) self.createSlider() self.merge()
def run(configfile, name, content, savefile): OutputPath.init(configFile) try: db = Database(configFile, 'specials') db.initialize() evaluation = Evaluation(configFile, db) data = evaluation.search(content) if savefile is not None: with open(savefile, 'w') as fp: fp.write(reprDict(data)) else: print reprDict(data) return 0 except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
def run(configfile, name): OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work try: db = Database(configFile, 'specials') db.initialize() evaluation = Evaluation(configFile, db) evaluation.updateOverdue() path = OutputPath.getSharePath() sharePath = getProperty(configFile, 'output-share-file') cmd = '/bin/rm -f {1} && /bin/ln -s {0} {1}'.format(path, sharePath) runCommand(cmd) data = evaluation.output() with open(path, 'w') as fp: fp.write(reprDict(data)) except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
def run(configFile, name, uuid): wx = None @itchat.msg_register(itchat.content.TEXT) def text(msg): wx.text(msg) @itchat.msg_register(itchat.content.TEXT, isGroupChat=True) def text(msg): wx.textGroup(msg) def quit(): try: wx.quit() except: pass OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work try: wx = WX(configFile) wx.login(quit, uuid) except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
def getVoiceText(self): # XXX record from micphone path = OutputPath.getDataPath('input', 'pcm') record(path) ''' cmd = 'aplay -r 16000 -f S16_LE -c 1 {}'.format(path) runCommand(cmd) ''' with open(path, 'rb') as fp: obj = self.aipSpeech.asr(fp.read()) error = obj.pop('err_no') if error is 0: res = obj.pop('result') if isinstance(res, list) and len(res) > 0: return (res[0]) else: print(obj.pop('err_msg')) return None
def getSlogan(skuid): path = OutputPath.getDataPath(skuid, 'html') SKU_MAIN_URL_TEMPLATE = 'http://item.m.jd.com/product/{}.html' url = SKU_MAIN_URL_TEMPLATE.format(skuid) ret = Network.saveGetUrl(path, url) #print 'Update', path, ':', ret if ret < 0: return None #PATTERN = r'<div class="prod-act">(.*?)</div>' PATTERN = r'<div class="prod-act">(.*?)<' with open(path) as fp: content = fp.read() slogan = getMatchString(content, PATTERN) if slogan is not None and not isinstance(slogan, unicode): slogan = unicode(slogan, errors='ignore') return slogan return None
def run(configFile, userId, name, screenshotPath, inputPath, outputPath, loginConfigFile, logFile): OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name, logFile) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work result = JsonResult.error() try: db = Database(configFile, 'register') db.initialize() account = Account(db, userId) result = account.login(screenshotPath, inputPath, outputPath, loginConfigFile) except KeyboardInterrupt: pass except Exception as e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout) finally: try: if db is not None: db.quit() except: pass try: if outputPath is not None: with open(outputPath, 'w') as fp: fp.write(reprDict(result)) except KeyboardInterrupt: pass except Exception as e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout) thread.quit() thread.join()
def run(configFile, name): OutputPath.init(configFile) OutputPath.clear() thread = ThreadWritableObject(configFile, name) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work try: db = Database(configFile, 'specials') db.initialize() skuManager = SkuManager(configFile, db) couponManager = CouponManager(configFile, db) discountManager = DiscountManager(configFile, db) seckillManager = SeckillManager(db) priceHistoryManager = PriceHistoryManager(db) evaluation = Evaluation(configFile, db) couponManager.update() discountManager.update() seckillManager.update() skuManager.update() evaluation.update() priceHistoryManager.update() evaluation.evaluate() except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
def run(configFile, userId, name, content, savefile, logFile): OutputPath.init(configFile) thread = ThreadWritableObject(configFile, name, logFile) thread.start() sys.stdout = thread sys.errout = thread # XXX: Actually, it does NOT work try: key = SearchingKeyRegex.parse(content) if key is None: key = content print 'Searching "', key, '" from user', userId db = None qwd = None db = Database(configFile, 'register') db.initialize() qwd = QWD(db, userId) searcher = Searcher(configFile, qwd) data = searcher.explore(key) if savefile is not None: with open(savefile, 'w') as fp: fp.write(reprDict(data)) else: print reprDict(data) except KeyboardInterrupt: pass except Exception, e: print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S') traceback.print_exc(file=sys.stdout)
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 initUserConfig(self): sql = ''' SELECT config FROM `configs` WHERE userId = {} '''.format(self.userId) result = self.db.query(sql) config = None for row in result: config = row['config'] if config is None: raise Exception('Config is invalid for user {}'.format( self.userId)) try: configObj = json.loads(config.decode('utf-8', 'ignore')) except ValueError as e: raise Exception('Config is invalid for user {}'.format( self.userId)) ## Login obj = configObj.pop('login') self.username = obj.pop('username') password = obj.pop('password') self.password = base64.b64decode(password) self.image = OutputPath.getAuthPath('user_{}'.format(self.userId)) self.originalImage = OutputPath.getAuthPath('user_{}_original'.format( self.userId)) self.source = OutputPath.getAuthPath('user_{}'.format(self.userId), 'html')
def getPlateImage(self): if self.skuimgurl is None: return None path = OutputPath.getDataPath('sku-{}-plate'.format(self.skuid), 'jpeg') ret = Network.saveGetUrl(path, self.skuimgurl) if ret < 0: return None return path
def run(name, configFile, ttsConfigFile, contentFile, videoFile, logFile): OutputPath.init(configFile) #thread = ThreadWritableObject(configFile, name, logFile) #thread.start() #sys.stdout = thread #sys.errout = thread # XXX: Actually, it does NOT work try: print('Now: ', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) Network.setIsEnabled(True) tts = Tts(ttsConfigFile) combiner = Combiner(configFile) combiner.combine(tts, contentFile, videoFile) except KeyboardInterrupt: pass except Exception as e: print('Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) traceback.print_exc(file=sys.stdout) finally: pass
def update(self, page=1, force=False): path = OutputPath.getDataPath('seckill-{}-{}'.format(self.gid, page), 'json') if page is 1: url = 'https://ms.m.jd.com/seckill/seckillList.json?gid={}'.format(self.gid) else: url = 'https://ms.m.jd.com/seckill/seckillListPage.json?isPagination=true&gid={}&page={}'.format(self.gid, page) ret = Network.saveHttpData(path, url, force=force) if ret < 0: return False return self.parse(path)
def voiceOut(self, stringContent): result = self.aipSpeech.synthesis(stringContent, 'zh', 1, { 'vol': 15, 'per': 0 }) if not isinstance(result, dict): path = OutputPath.getDataPath('voice', 'wav') with open(path, 'wb') as fp: fp.write(result) cmd = 'mplayer {}'.format(path) runCommand(cmd)
def getComments(self): url = Infor.COMMENT_URL_TEMPLATE.format(self.skuid) path = OutputPath.getDataPath(self.skuid, 'json') ret = Network.saveGetUrl(path, url) if ret < 0: return None with open(path) as fp: content = fp.read() obj = json.loads(content) return obj.pop('wareDetailComment') return None
def getSlogan(self): path = OutputPath.getDataPath(self.skuid, 'html') url = Infor.SKU_MAIN_URL_TEMPLATE.format(self.skuid) ret = Network.saveGetUrl(path, url) if ret < 0: return None with open(path) as fp: content = fp.read() slogan = getMatchString(content, Infor.SLOGAN_PATTERN) if slogan is not None and not isinstance(slogan, unicode): slogan = unicode(slogan, errors='ignore') return slogan return None
def getComments(skuid): COMMENT_URL_TEMPLATE = 'http://item.m.jd.com/ware/getDetailCommentList.json?wareId={}' url = COMMENT_URL_TEMPLATE.format(skuid) path = OutputPath.getDataPath(skuid, 'json') ret = Network.saveGetUrl(path, url) #print 'Update', path, ':', ret if ret < 0: return None with open(path) as fp: content = fp.read() obj = json.loads(content) return obj.pop('wareDetailComment') return None
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 getImage(self): path = OutputPath.getDataPath(self.skuid, 'html') url = Infor.SKU_MAIN_URL_TEMPLATE.format(self.skuid) ret = Network.saveGetUrl(path, url) if ret < 0: return None with open(path) as fp: content = fp.read() m = re.search(Infor.IMAGE_PATTERN, content) if m is None: return None url = getMatchString(m.group(0), Infor.MARK_PATTERN) return url return None
def getPriceHistoryData(executor, data): skuid = data['skuid'] title = data['title'] url = 'http://item.jd.com/{}.html'.format(skuid) # For history searching # Get URL for price history url = executor.context.requestPriceInfo(title, url) # Get price histories path = OutputPath.getDataPath(skuid, 'js') ret = Network.saveHttpData(path, url) #print 'Update', path, ':', ret, ':', title if ret < 0: return None obj = PriceHistoryManager.parse(path) if obj is None: return None return PriceHistoryManager.generatePriceHistoryData(data, obj)
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 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