def loginTest(cred, browser="PhantomJS"): logfilename = datetime.strftime(datetime.now(), '%Y-%m-%d %H_%M_%S') + '.log' with open('../tmp/' + logfilename, 'w+') as f: sys.stdout = f driver = getDriver(browser) i, t0 = 1, time.time() for login_pair in cred: t = time.time() print("# {0} ID: {1}".format(i, login_pair[0])) i += 1 try: driver.get(amex_url) amexLogIn(driver, login_pair[0], login_pair[1], 'eliloUserID', 'eliloPassword') except: print("Something is wrong with login\n") continue time.sleep(2) closeFeedback(driver) offer_names = collectOfferNames(driver) print("Available offers are: {}".format(offer_names)) if offer_names: clickOnOffers(driver) while not driver.find_elements_by_id('eliloUserID'): try: amexLogOut(driver) except: pass print("Time used: %0.1f seconds\n" % (time.time() - t)) driver.quit() print("** Summary **\nTotal time used: %0.1f seconds" % (time.time() - t0))
def loginTest(cred, browser="PhantomJS"): logfilename = datetime.strftime(datetime.now(), '%Y-%m-%d %H_%M_%S') + '.log' with open('../tmp/' + logfilename, 'w+') as f: sys.stdout = f driver = getDriver(browser) i, t0 = 1, time.time() for login_pair in cred: t = time.time() print("# {0} ID: {1}".format(i, login_pair[0])) i += 1 try: driver.get(amex_url) time.sleep(2) amexLogIn(driver, login_pair[0], login_pair[1], 'eliloUserID', 'eliloPassword') except: print("Something is wrong with login\n") continue time.sleep(5) closeFeedback(driver) time.sleep(3) offer_names = collectOfferNames(driver) print("Available offers are: {}".format(offer_names)) if offer_names: clickOnOffers(driver) while not driver.find_elements_by_id('eliloUserID'): try: amexLogOut(driver) except: pass print("Time used: %0.1f seconds\n" % (time.time() - t)) driver.quit() print("** Summary **\nTotal time used: %0.1f seconds" % (time.time() - t0))
def getAddedOffers(cred, browser = "Chrome"): offer_map = {} offer_key_desc = {} username = [x[0] for x in cred] driver = getDriver(browser) for login_pair in cred: try: driver.get(amex_url) time.sleep(3) amexLogIn(driver, login_pair[0], login_pair[1], 'eliloUserID', 'eliloPassword') except: print("login error") continue time.sleep(5) attempt = 0 while not driver.find_elements_by_class_name("offer-expires") and attempt < 5: attempt += 1 time.sleep(1) closeFeedback(driver) offer_expires = [o.text.lower() for o in driver.find_elements_by_class_name("offer-expires")] offer_expires = [get_date(o) for o in offer_expires] offer_info = [o.text.lower().encode('ascii', 'ignore').decode('utf-8', 'ignore') for o in driver.find_elements_by_class_name("offer-info")] offer_info = [o.replace(', get', ' and get').replace(',', '') for o in offer_info] # filter out things don't start with 'Spend' offer_expires = [offer_expires[i] for i in range(len(offer_expires)) if offer_info[i].startswith('spend')] offer_info = [o for o in offer_info if o.startswith('spend')] offer_info = [o.split('\n') for o in offer_info] offer_desc = [o[0] for o in offer_info] offer_info = [','.join([re.findall(r'spend \$(\d+)', o[0])[0], o[1]]) for o in offer_info] offer_key = [','.join(x) for x in zip(offer_expires, offer_info)] for k in offer_key: if k in offer_map: offer_map[k].append(login_pair[0]) else: offer_map[k] = [login_pair[0]] for i in range(len(offer_key)): if offer_key[i] in offer_key_desc: offer_key_desc[offer_key[i]].update([offer_desc[i]]) else: offer_key_desc[offer_key[i]] = set([offer_desc[i]]) while not driver.find_elements_by_id('eliloUserID'): try: amexLogOut(driver) except: pass driver.quit() offer_key_desc = {k: ' OR '.join(v) for k, v in offer_key_desc.items()} offer_df = {k: ['+' if x in v else '' for x in username] for k, v in offer_map.items()} offer_df = pd.DataFrame(offer_df) cols = offer_df.columns.tolist() cols.sort(key=lambda x: (datetime.strptime(x.split(',')[0].strip(), '%m/%d/%Y'), x.split(',')[2].strip())) offer_df = offer_df[cols] offer_df.index = username info_rows = pd.DataFrame([[offer_key_desc[x], x.split(',')[0].strip()] for x in cols]).T info_rows.index = ['_description', '_expiration'] colnames = [x.split(',')[2].strip() for x in cols] info_rows.columns = offer_df.columns = colnames res = pd.concat([info_rows, offer_df]) file_name = 'offers_' + datetime.strftime(datetime.now(), '%Y-%m-%d_%H-%M-%S') + '.csv' res.to_csv('../tmp/' + file_name, sep=',', encoding='utf-8')
def getAddedOffers(cred, browser = "Chrome"): offer_map = {} offer_key_desc = {} username = [x[0] for x in cred] driver = getDriver(browser) for login_pair in cred: try: driver.get(amex_url) amexLogIn(driver, login_pair[0], login_pair[1], 'eliloUserID', 'eliloPassword') except: print("login error") continue time.sleep(1) while not driver.find_elements_by_class_name("offer-expires"): time.sleep(1) closeFeedback(driver) offer_expires = [o.text.lower() for o in driver.find_elements_by_class_name("offer-expires")] offer_expires = [get_date(o) for o in offer_expires] offer_info = [o.text.lower() for o in driver.find_elements_by_class_name("offer-info")] offer_info = [o.replace(', get', ' and get').replace(',', '') for o in offer_info] # filter out things don't start with 'Spend' offer_expires = [offer_expires[i] for i in range(len(offer_expires)) if offer_info[i].startswith('spend')] offer_info = [o for o in offer_info if o.startswith('spend')] offer_info = [o.split('\n') for o in offer_info] offer_desc = [o[0] for o in offer_info] offer_info = [','.join([re.findall(r'spend \$(\d+)', o[0])[0], o[1]]) for o in offer_info] offer_key = [','.join(x) for x in zip(offer_expires, offer_info)] for k in offer_key: if k in offer_map: offer_map[k].append(login_pair[0]) else: offer_map[k] = [login_pair[0]] for i in range(len(offer_key)): if offer_key[i] in offer_key_desc: offer_key_desc[offer_key[i]].update([offer_desc[i]]) else: offer_key_desc[offer_key[i]] = set([offer_desc[i]]) while not driver.find_elements_by_id('eliloUserID'): try: amexLogOut(driver) except: pass driver.quit() offer_key_desc = {k: ' OR '.join(v) for k, v in offer_key_desc.items()} offer_df = {k: ['+' if x in v else '' for x in username] for k, v in offer_map.items()} offer_df = pd.DataFrame(offer_df) cols = offer_df.columns.tolist() cols.sort(key=lambda x: (datetime.strptime(x.split(',')[0].strip(), '%m/%d/%Y'), x.split(',')[2].strip())) offer_df = offer_df[cols] offer_df.index = username info_rows = pd.DataFrame([[offer_key_desc[x], x.split(',')[0].strip()] for x in cols]).T info_rows.index = ['_description', '_expiration'] colnames = [x.split(',')[2].strip() for x in cols] info_rows.columns = offer_df.columns = colnames res = pd.concat([info_rows, offer_df]) file_name = 'offers_' + datetime.strftime(datetime.now(), '%Y-%m-%d_%H-%M-%S') + '.csv' res.to_csv('../tmp/' + file_name, sep=',', encoding='utf-8')
def loginTest(cred, browser="PhantomJS"): logfilename = 'balance_' + datetime.strftime(datetime.now(), '%Y-%m-%d %H_%M_%S') + '.log' with open('../tmp/' + logfilename, 'w+') as f: sys.stdout = f driver = getDriver(browser) i, t0 = 1, time.time() for login_pair in cred: try: driver.get(amex_url) time.sleep(2) amexLogIn(driver, login_pair[0], login_pair[1], 'eliloUserID', 'eliloPassword') except: print("# {0} ID: {1} Something is wrong with login".format(i, login_pair[0])) continue time.sleep(5) closeFeedback(driver) bal = getBalance(driver) print("# {0} ID: {1} Balance: {2}".format(i, login_pair[0], bal)) i += 1 while not driver.find_elements_by_id('eliloUserID'): try: amexLogOut(driver) except: pass driver.quit() print("** Summary **\nTotal time used: %0.1f seconds" % (time.time() - t0))
def getAddedOffers(username, password, outputlog = True): # re-route output orig_stdout = sys.stdout logfile = None if outputlog: # use current time as log file logfilename = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') logfilename = "offers " + logfilename.replace(':', '_') + ".csv" logfile = open('../tmp/' + logfilename, 'w+') sys.stdout = logfile # input error handle if username == [] or password == [] or len(username) != len(password): print "username array does not have the same length as password array..." # close log file if outputlog: sys.stdout = orig_stdout logfile.close() return # use phantom JS / Firefox # driver = webdriver.PhantomJS() # driver = webdriver.Firefox() driver = webdriver.Chrome('./chromedriver') driver.maximize_window() majorOfferDescMap = dict() majorOfferDateMap = dict() majorDateOfferPair = set() userOffersList = [] # loop through all username/password combinations for idx in range(len(username)): eachbegintime = time.time() try: driver.get(amexWebsite) except: print "website is not available..." # close log file if outputlog: sys.stdout = orig_stdout logfile.close() return # fill and submit login form try: amexLogIn(driver, username[idx], password[idx]) except: print "username/password combination is incorrect..." continue time.sleep(2) closeFeedback(driver) clickOnAddedToCard(driver) clickOnLoadMore(driver) # main program offers = driver.find_elements_by_class_name("ah-card-offer") offerstext = [offer.text.encode('utf-8') for offer in offers] offersplit = [text.split('\n') for text in offerstext] offerDescMap = dict() offerDateMap = dict() dateOfferPair = set() offersSet = set() # discard expired offers for sp in offersplit: expiration = dt.strptime(sp[3], '%m/%d/%Y') if expiration > dt.now(): offerDescMap[sp[0]] = sp[1] offerDateMap[sp[0]] = sp[3] dateOfferPair.add((expiration, sp[0])) offersSet.add(sp[0]) majorOfferDescMap.update(offerDescMap) majorOfferDateMap.update(offerDateMap) majorDateOfferPair.update(dateOfferPair) userOffersList.append(offersSet) time.sleep(1) # logout try: amexLogOut(driver) except: pass time.sleep(1) majorDateOfferList = list(majorDateOfferPair) majorDateOfferList.sort(key=lambda tup:tup[0]) # write 1st line for dateOffer in majorDateOfferList: offer = dateOffer[1].replace(',', '.') sys.stdout.write(','+offer) sys.stdout.write('\n') # write 2nd line for dateOffer in majorDateOfferList: desc = majorOfferDescMap[dateOffer[1]].replace(',', ' and') sys.stdout.write(','+desc) sys.stdout.write('\n') # write 3rd line for dateOffer in majorDateOfferList: date = majorOfferDateMap[dateOffer[1]] sys.stdout.write(','+date) sys.stdout.write('\n') # write the rest for i in range(len(username)): sys.stdout.write(username[i]) for dateOffer in majorDateOfferList: if dateOffer[1] in userOffersList[i]: sys.stdout.write(',+') else: sys.stdout.write(',') sys.stdout.write('\n') # close log file if outputlog: sys.stdout = orig_stdout logfile.close() # close browser driver.quit()
def loginTest(username, password, outputlog = True, browser = "PhantomJS"): orig_stdout = sys.stdout # re-route output logfile = None if outputlog: # use current time in log file name logfilename = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') logfilename = logfilename.replace(':', '_') + ".log" logfile = open('../tmp/' + logfilename, 'w+') sys.stdout = logfile # input error handle if username == [] or password == [] or len(username) != len(password): print "username array does not have the same length as password array..." # close log file if outputlog: sys.stdout = orig_stdout logfile.close() return driver = getDriver(browser) begintime = time.time() # loop through all username/password combinations for idx in range(len(username)): eachbegintime = time.time() print "--------------------------------------------------------------" print "#", idx+1, "ID:", username[idx] # just in case network connection is broken try: driver.get(amexWebsite) except: print "website is not available..." if outputlog: sys.stdout = orig_stdout logfile.close() # close log file return # fill and submit login form try: amexLogIn(driver, username[idx], password[idx]) except: print "Something is wrong with login" continue # end current loop closeFeedback(driver) # just in case the feedback banner appears clickOnLoadMore(driver) # scroll down and click load more # store offer names and click on offers offernames = collectOfferNames(driver) print "Available offers are:", offernames if not offernames == '': clickOnOffers(driver) time.sleep(1) # logout try: amexLogOut(driver) except: pass # pass would be fine, since the launch of the url is the log-in page no matter what time.sleep(1) eachendtime = time.time() print "Time used: %0.2f seconds" % (eachendtime - eachbegintime) print "--------------------------------------------------------------" endtime = time.time() # print summary print "--------------------------------------------------------------" print "** Summary **" print "Total time used: %0.2f seconds" % (endtime - begintime) print "--------------------------------------------------------------" # close log file if outputlog: sys.stdout = orig_stdout logfile.close() # close browser driver.quit()
def loginTest(username, password, outputlog=True): orig_stdout = sys.stdout # re-route output logfile = None if outputlog: # use current time in log file name logfilename = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') logfilename = logfilename.replace(':', '_') + ".log" logfile = open('../tmp/' + logfilename, 'w+') sys.stdout = logfile # input error handle if username == [] or password == [] or len(username) != len(password): print "username array does not have the same length as password array..." # close log file if outputlog: sys.stdout = orig_stdout logfile.close() return # use phantom JS driver = webdriver.PhantomJS() # driver = webdriver.Firefox() # driver = webdriver.Chrome('./chromedriver') driver.maximize_window() begintime = time.time() # loop through all username/password combinations for idx in range(len(username)): eachbegintime = time.time() print "--------------------------------------------------------------" print "#", idx + 1, "ID:", username[idx] # just in case network connection is broken try: driver.get(amexWebsite) except: print "website is not available..." if outputlog: sys.stdout = orig_stdout logfile.close() # close log file return # fill and submit login form try: amexLogIn(driver, username[idx], password[idx]) except: print "Something is wrong with login" continue # end current loop closeFeedback(driver) # just in case the feedback banner appears clickOnLoadMore(driver) # scroll down and click load more # store offer names and click on offers tmpoffernames = driver.find_elements_by_class_name( "ah-card-offer-name") + driver.find_elements_by_class_name( "ah-offer-name") tmpnames = [n.text.encode('utf-8') for n in tmpoffernames] tmpnames = filter(None, tmpnames) print "Available offers are:", ', '.join(tmpnames) clickOnOffers(driver) # logout try: amexLogOut(driver) except: pass # pass would be fine, since the launch of the url is the log-in page no matter what time.sleep(1) eachendtime = time.time() print "Time used: %0.2f seconds" % (eachendtime - eachbegintime) print "--------------------------------------------------------------" endtime = time.time() # print summary print "--------------------------------------------------------------" print "** Summary **" print "Total time used: %0.2f seconds" % (endtime - begintime) print "--------------------------------------------------------------" # close log file if outputlog: sys.stdout = orig_stdout logfile.close() # close browser driver.quit()