def main(): #this is where the magick happens logFIleName = 'logs/femmanLog-{:%Y-%m-%d}'.format(datetime.now()) logging.basicConfig(filename=logFIleName, level=logging.DEBUG) #start logging logging.debug('') logging.debug('') logging.debug('{:%Y-%m-%d %H:%M:%S} Generating HTML output'.format( datetime.now())) logging.debug('{:%Y-%m-%d %H:%M:%S} Create outputs'.format(datetime.now())) database = "femman.db" logging.debug('{:%Y-%m-%d %H:%M:%S} Create connection to db'.format( datetime.now())) conn = femmanDb.createConnection(database) logging.debug('{:%Y-%m-%d %H:%M:%S} Fetch all members from db'.format( datetime.now())) members = femmanDb.getAllMembers(conn) logging.debug( '{:%Y-%m-%d %H:%M:%S} Create html table output for arr'.format( datetime.now())) femmanMisc.rowsToHtml(members) logging.debug( '{:%Y-%m-%d %H:%M:%S} Create html table output for admin'.format( datetime.now())) femmanMisc.rowsToHtmlAdmin(members) print(members)
def main(): #this is where the magick happens # logFIleName = 'logs/femmanLog-{:%Y-%m-%d}'.format(datetime.now()) # logging.basicConfig(filename=logFIleName,level=logging.DEBUG) # logging.debug('') # logging.debug('') # logging.debug('') # logging.debug('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') # logging.debug('') # logging.debug('') # logging.debug('') # logging.debug('\n{:%Y-%m-%d %H:%M:%S} Summary script'.format(datetime.now())) database = "femman.db" # logging.debug('{:%Y-%m-%d %H:%M:%S} Create connection to db'.format(datetime.now())) conn = femmanDb.createConnection(database) # logging.debug('{:%Y-%m-%d %H:%M:%S} Fetch all members from db'.format(datetime.now())) members = femmanDb.getAllMembers(conn) # logging.debug('{:%Y-%m-%d %H:%M:%S} Create html table output for arr'.format(datetime.now())) # rowsToHtml(members) # logging.debug('{:%Y-%m-%d %H:%M:%S} Create html table output for admin'.format(datetime.now())) # rowsToHtmlAdmin(members) newMembers = [] for member in members: member = list(member) memberDate = member[4] memberDate = datetime.strptime(memberDate,'%Y-%m-%d %H:%M:%S') compareDate = datetime.now() - timedelta(days=1) if memberDate >= compareDate: outputString = "" for item in member: outputString += str(item) + "," print outputString[:-2]
def main(): logFIleName = 'logs/femmanLog-{:%Y-%m-%d}'.format(datetime.now()) logging.basicConfig(filename=logFIleName, level=logging.DEBUG) logging.debug('') logging.debug( '{:%Y-%m-%d %H:%M:%S} Manually adding member via commandline'.format( datetime.now())) name = input("name: ").encode('utf-8') #assume prefName == Name email = input("email: ") email = email.lower() email = email.strip() now = datetime.now() memberDate = now memberDate = now.strftime("%Y-%m-%d %H:%M:00") memberDate = datetime.strptime(memberDate, "%Y-%m-%d %H:%M:00") member = [name, name, email, memberDate] print(member) database = "femman.db" # create a database connection logging.debug('{:%Y-%m-%d %H:%M:%S} Create connection to db'.format( datetime.now())) conn = femmanDb.createConnection(database) conn.text_factory = str now = datetime.now() sqlCreateMembersTable = """ CREATE TABLE IF NOT EXISTS members%s( id integer PRIMARY KEY, fullName text NOT NULL, prefName text NOT NULL, email text NOT NULL, [MemberDate] timestamp NOT NULL ); """ % str(now.year) sqlCreateMembersTableNextYear = """ CREATE TABLE IF NOT EXISTS members%s( id integer PRIMARY KEY, fullName text NOT NULL, prefName text NOT NULL, email text NOT NULL, [MemberDate] timestamp NOT NULL ); """ % str(now.year + 1) with conn: # create members table table if it doesn't exist, i.e. first # time running program logging.debug('{:%Y-%m-%d %H:%M:%S}'.format(datetime.now()) + str(member)) okCheck = False now = datetime.now() if now.month >= 11: femmanDb.createTable(conn, sqlCreateMembersTableNextYear) if not femmanDb.isMemberNextYear(conn, member): print(" Adding member to next year's db...") memberNumber = femmanDb.insertMemberNextYear( conn, tuple(member)) print(" " * 8 + "Nr: " + str(memberNumber) + " (" + str(member[3]) + ")") print(" " * 8 + member[0].decode('utf-8')) print(" " * 8 + member[1].decode('utf-8')) print(" " * 8 + str(member[2])) # email need not be decoded if memberNumber: okCheck = True moosendResult2 = femmanEmail.moosend(member, "nextYear") if moosendResult2 != 0: print("See error above. Add manually. Important!") else: print(" ok!") else: print("Already member in next year's db") femmanDb.createTable(conn, sqlCreateMembersTable) if not femmanDb.isMember(conn, member): #if not member print(" Adding member to database...") logging.debug('{:%Y-%m-%d %H:%M:%S} Adding member to db'.format( datetime.now())) memberNumber = femmanDb.insertMember(conn, tuple(member)) print(" " * 8 + "Nr: " + str(memberNumber) + " (" + str(member[3]) + ")") print(" " * 8 + member[0].decode('utf-8')) print(" " * 8 + member[1].decode('utf-8')) print(" " * 8 + str(member[2])) # email need not be decoded logging.debug('{:%Y-%m-%d %H:%M:%S} added to db with nr'.format( datetime.now()) + str(memberNumber)) if memberNumber: okCheck = True print(" Adding member to moosend") moosendResult = femmanEmail.moosend(member, "thisYear") if moosendResult != 0: print("See error above. Add manually. Important!") else: print(" ok!") else: print("already a in member current db") if okCheck: femmanEmail.sendEmail([memberNumber] + member)
def extractInfo(body): """ extract info (name, pref name, email, dateString) from body = msg.get_payload(decode=True). returns this info as list. :param body: msg.get_payload(decode=True) :return member: list """ cost = 100 logging.debug("extractinfo(body)") #Split the body into a list of lines for easier parsing b = body.decode("utf-8") body = b body = body.replace('\r', '\n') bodyLines = body.split('\n') bodyLines[:] = [x for x in bodyLines if x] ind = bodyLines.index(" SUBTOTAL") + 2 if bodyLines[ind] != "Membership": logging.debug("Product check failed") return [] fullName = "" preferredName = "" emailAdr = "" membershipDate = "" ind = bodyLines.index(" TOTAL") + 2 totalCost = bodyLines[ind] totalCost = totalCost[1:-6].strip() totalCost = int(totalCost.replace(",", "")) numberOfRuns = totalCost / cost if totalCost > cost: logging.debug("Multiple memberships bought!!" + str(numberOfRuns)) counter = 0 members = [] # product could potentially be something other than 'membership' # e.g. t-shirt so productCheck should insure that we are dealing # with a new member and not someone trying to buy something. productCheck = False # go through all the body lines and when the info is found stop and # return the list (given that productCheck is equal to true. lineCount = 0 for line in bodyLines: i = lineCount if "Order" in line and "Placed on" in line: logging.debug("'Order' and 'Placed on' found in line") membershipDate = parseDate(line) elif "Full legal name:" in line or "Full given name:" in line: fullName = bodyLines[i + 1].strip() logging.debug("Full name: " + fullName) fullName = fullName.encode('utf-8') elif "Preferred name:" in line: preferredName = bodyLines[i + 1].strip() logging.debug("Prefered name: " + preferredName) preferredName = preferredName.encode('utf-8') elif "Email:" in line: emailAdr = bodyLines[i + 1].strip() logging.debug("email converted to lower case: " + emailAdr) emailAdr = emailAdr.lower() counter += 1 member = [fullName, preferredName, emailAdr, membershipDate] members.append(member) if counter == numberOfRuns: break lineCount += 1 if totalCost > cost: #if date reasonable compareDate = datetime.now() - timedelta(days=1) firstDate = members[0][3] if firstDate >= compareDate: print("\nWarning: totalCost > cost maybe check it out") emailList = [] for member in members: s = member[0].decode('utf-8') + ", " + member[1].decode( 'utf-8') s += ", " + member[2] + ", " + str(member[3]) s = " " * 4 + s print(s) emailList.append(member[2]) emailCounter = len(set(emailList)) if emailCounter < len(members): cand = members[0] database = "femman.db" conn = femmanDb.createConnection(database) if not femmanDb.isMemberNextYear(conn, cand): print("trying to send warning") sendEmailWarning(cand) else: print("is already member. should be sorted out") return members
def main(): logFIleName = 'logs/femmanLog-{:%Y-%m-%d}'.format(datetime.now()) logging.basicConfig(filename=logFIleName, level=logging.DEBUG) logging.debug('') logging.debug('{:%Y-%m-%d %H:%M:%S} Starting Script'.format( datetime.now())) if len(sys.argv) < 2 or str(sys.argv[1]) not in ["all", "unread"]: print("must be executed as either") print("'./femman all' or './femman unread'") sys.exit() howMany = str(sys.argv[1]) # check all emails in inbox or just unread print("Checking email...") logging.debug('{:%Y-%m-%d %H:%M:%S} Checking Email'.format(datetime.now())) newMembers = femmanEmail.readEmailFromGmail(howMany) if newMembers: logging.debug('{:%Y-%m-%d %H:%M:%S} Member candidates found'.format( datetime.now())) #if there exists new potential members count them counter = 0 database = "femman.db" #make a db connection logging.debug('{:%Y-%m-%d %H:%M:%S} Create connection to db'.format( datetime.now())) conn = femmanDb.createConnection(database) now = datetime.now() sqlCreateMembersTable = """ CREATE TABLE IF NOT EXISTS members%s( id integer PRIMARY KEY, fullName text NOT NULL, prefName text NOT NULL, email text NOT NULL, [MemberDate] timestamp NOT NULL ); """ % str(now.year) sqlCreateMembersTableNextYear = """ CREATE TABLE IF NOT EXISTS members%s( id integer PRIMARY KEY, fullName text NOT NULL, prefName text NOT NULL, email text NOT NULL, [MemberDate] timestamp NOT NULL ); """ % str(now.year + 1) with conn: # create members table table if it doesn't exist, i.e. first # time running program logging.debug('{:%Y-%m-%d %H:%M:%S} Creating table'.format( datetime.now())) femmanDb.createTable(conn, sqlCreateMembersTable) memberCandsStr = "\n" + str(len(newMembers)) + " candidates" print(memberCandsStr) logging.debug('{:%Y-%m-%d %H:%M:%S}'.format(datetime.now()) + memberCandsStr) print("\nStart processing...\n") logging.debug( '{:%Y-%m-%d %H:%M:%S} Start processing members'.format( datetime.now())) maxLen = 0 outputString = "" for member in newMembers: logging.debug('{:%Y-%m-%d %H:%M:%S}'.format(datetime.now()) + str(member)) #if already a member go to next candidate memberInfo = femmanDb.isMember(conn, member) if memberInfo: #is member logging.debug('{:%Y-%m-%d %H:%M:%S} Already member'.format( datetime.now())) ouputString = member[0].decode('utf-8') + "," ouputString += member[1].decode('utf-8') + "," ouputString += member[2] + "," ouputString += str(member[3]) + " already a member" # outputString = str(member)+ " already a member" # print(outputString) now = datetime.now() if now.month >= 11: newDate = member[-1] oldDate = memberInfo[4] oldDate = datetime.strptime(oldDate, '%Y-%m-%d %H:%M:%S') tmpStr = str(now.year) + "/12/1" firstDec = datetime.strptime(tmpStr, '%Y/%m/%d') if newDate > oldDate and newDate >= firstDec: #make sure table exists femmanDb.createTable( conn, sqlCreateMembersTableNextYear) logging.debug( '{:%Y-%m-%d %H:%M:%S} Adding member to db'. format(datetime.now())) if not femmanDb.isMemberNextYear(conn, member): print(" Adding member to next year's db...") memberNumber = femmanDb.insertMemberNextYear( conn, tuple(member)) print(" " * 8 + "Nr: " + str(memberNumber) + " (" + str(member[3]) + ")") print(" " * 8 + member[0].decode('utf-8')) print(" " * 8 + member[1].decode('utf-8')) print(" " * 8 + str( member[2])) # email need not be decoded moosendResult2 = femmanEmail.moosend( member, "nextYear") if moosendResult2 != 0: print( "See error above. Add manually. Important!" ) else: print(" ok!") print(" Emailing welcome msg...") femmanEmail.sendEmail([memberNumber] + member) counter += 1 if len(outputString) > maxLen: maxLen = len(outputString) padding = " " * (maxLen - len(outputString) + 1) #check if script is run from cron or console if os.isatty(sys.stdin.fileno()): sys.stdout.write("\r" + outputString + padding) sys.stdout.flush() else: pass continue else: print() print("New member!") logging.debug('{:%Y-%m-%d %H:%M:%S} New member'.format( datetime.now())) counter += 1 print(" Adding member to database...") logging.debug( '{:%Y-%m-%d %H:%M:%S} Adding member to db'.format( datetime.now())) memberNumber = femmanDb.insertMember(conn, tuple(member)) print(" " * 8 + "Nr: " + str(memberNumber) + " (" + str(member[3]) + ")") print(" " * 8 + member[0].decode('utf-8')) print(" " * 8 + member[1].decode('utf-8')) print(" " * 8 + str(member[2])) # email need not be decoded logging.debug('{:%Y-%m-%d %H:%M:%S} added to db with nr'. format(datetime.now()) + str(memberNumber)) now = datetime.now() if now.month >= 11: # add to next year's db femmanDb.createTable(conn, sqlCreateMembersTableNextYear) if not femmanDb.isMemberNextYear(conn, member): print(" Adding member to next year's db...") memberNumber = femmanDb.insertMemberNextYear( conn, tuple(member)) print(" " * 8 + "Nr: " + str(memberNumber) + " (" + str(member[3]) + ")") print(" " * 8 + member[0].decode('utf-8')) print(" " * 8 + member[1].decode('utf-8')) print(" " * 8 + str(member[2])) # email need not be decoded print(" Adding member to next year's moosend...") moosendResult2 = femmanEmail.moosend( member, "nextYear") if moosendResult2 != 0: print("See error above. Add manually. Important!") else: print(" ok!") logging.debug('{:%Y-%m-%d %H:%M:%S} Sleep 2s'.format( datetime.now())) print(" Sleep 2s") # sleep(2) print(" Adding member to moosend") moosendResult = femmanEmail.moosend(member, "thisYear") moosendResult2 = femmanEmail.moosend(member, "nextYear") if moosendResult != 0 or moosendResult2 != 0: print("See error above. Add manually. Important!") else: print(" ok!") print(" Emailing welcome msg...") logging.debug('{:%Y-%m-%d %H:%M:%S} Email welcome msg'.format( datetime.now())) femmanEmail.sendEmail([memberNumber] + member) #if there is more than one new members and we have more #members to process, sleep a little to make google happy if len(newMembers) > 1 and newMembers.index( member) < len(newMembers) - 1: sleepTime = randint(1, 5) sleepStr = "Sleep for " + str( sleepTime) + "s to not make google angry" print(sleepStr) logging.debug( '{:%Y-%m-%d %H:%M:%S} '.format(datetime.now()) + sleepStr) sleep(sleepTime) CURSOR_UP = '\033[F' ERASE_LINE = '\033[K' print(CURSOR_UP + ERASE_LINE) padding = " " * (maxLen - 25) sys.stdout.write("\rdone checking candidates " + padding) sys.stdout.flush() print() if counter > 0: addStr = "Added " + str(counter) + " new members to db" print(addStr) logging.debug('{:%Y-%m-%d %H:%M:%S} '.format(datetime.now()) + addStr) else: addStr = "No new members added to db." print(addStr) logging.debug('{:%Y-%m-%d %H:%M:%S} '.format(datetime.now()) + addStr) else: if howMany == "all": elseMsg = "No member candidates found. Probably indicates some error." logging.debug('{:%Y-%m-%d %H:%M:%S} '.format(datetime.now()) + elseMsg) print(elseMsg) else: print("no new candidates to process") logging.debug('{:%Y-%m-%d %H:%M:%S} Create HTML outputs'.format( datetime.now())) database = "femman.db" conn = femmanDb.createConnection(database) print() members = femmanDb.getAllMembers(conn) print("creating (arr) list") femmanMisc.rowsToHtml(members) print("creating (admin) list") femmanMisc.rowsToHtmlAdmin(members)