Exemplo n.º 1
0
def checktable(dbpath: str, ownername: str):
    """
    检查数据表是否存在并根据情况创建
    """
    if not (ifcreated := getcfpoptionvalue('everwebchat', 'wcdb', ownername)):
        print(ifcreated)
        dbnameinner = getdbname(dbpath, ownername)

        tablename = "wcheadimg"
        if istableindb(tablename, dbnameinner):
            # 删表操作,危险,谨慎操作
            droptablefromdb(dbnameinner, tablename, confirm=True)
        csql = f"create table if not exists {tablename} (himgid INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT not null, himguuid TEXT NOT NULL UNIQUE ON CONFLICT IGNORE, headimg BLOB NOT NULL)"
        ifnotcreate(tablename, csql, dbnameinner)
        logstr = f"数据表{tablename}于{dbnameinner}中被删除并完成重构"
        log.critical(logstr)

        tablename_cc = "wccontact"
        if istableindb(tablename_cc, dbnameinner):
            # 删表操作,危险,谨慎操作
            droptablefromdb(dbnameinner, tablename_cc, confirm=True)
        csql = f"create table if not exists {tablename_cc} (id INTEGER PRIMARY KEY AUTOINCREMENT, contactuuid TEXT NOT NULL UNIQUE ON CONFLICT IGNORE, nickname TEXT, contactflag int, remarkname TEXT, sex int, signature TEXT, starfriend int, attrstatus int, province TEXT, city TEXT, snsflag int, keyword TEXT, imguuid text, appendtime datatime)"
        ifnotcreate(tablename_cc, csql, dbnameinner)
        logstr = f"数据表{tablename_cc}于{dbnameinner}中被删除并完成重构"
        log.critical(logstr)

        setcfpoptionvalue('everwebchat', 'wcdb', ownername, str(True))
Exemplo n.º 2
0
def items2df(fl):
    """
    读取txt记录文件,格式化拆分并存储至DataFrame返回
    """
    try:
        content = open(fl, 'r').read()
    except Exception as e:
        log.critical(f"文件{fl}读取时出现错误,返回空的pd.DataFrame")
        return pd.DataFrame()
    ptn = re.compile(
        "(^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\t(True|False)\t([^\t]+)\t(\w+)\t",
        re.M)
    itemlst = re.split(ptn, content)
    itemlst = [im.strip() for im in itemlst if len(im) > 0]
    step = 5
    itemlst4pd1 = [itemlst[i:i + step] for i in range(0, len(itemlst), step)]
    df2 = pd.DataFrame(itemlst4pd1,
                       columns=['time', 'send', 'sender', 'type', 'content'])
    df2['time'] = pd.to_datetime(df2['time'])
    df2['send'] = df2['send'].apply(lambda x: True if x == 'True' else False)
    df2['content'] = df2['content'].apply(
        lambda x: re.sub("(\[\w+前\])?", "", x))
    dfout = df2.drop_duplicates().sort_values('time', ascending=False)

    return dfout
Exemplo n.º 3
0
def isitchat(pklabpath):
    """
    判断itchat是否已经运行,没有则热启动之。
    如果成功则返回True,否则直接退出运行。
    """

    inputpklpath = os.path.abspath(pklabpath)
    #     print(inputpklpath)

    if itchat.originInstance.alive:
        # 转换成绝对路径方便对比

        loginpklpath = os.path.abspath(itchat.originInstance.hotReloadDir)
        if inputpklpath == loginpklpath:
            log.info(f"微信处于正常登录状态,pkl路径为:\t{loginpklpath}……")
        else:
            logstr = f"当前登录的pkl路径为{loginpklpath},不同于传入的参数路径:\t{inputpklpath}"
            log.critical(logstr)
            sys.exit(1)
    else:
        itchat.auto_login(hotReload=True, statusStorageDir=pklabpath)  #热启动你的微信
        if not itchat.originInstance.alive:
            log.critical("微信未能热启动,仍处于未登陆状态,退出!")
            sys.exit(1)
        else:
            loginpklpath = os.path.abspath(itchat.originInstance.hotReloadDir)
            logstr = f"微信热启动成功\t{loginpklpath}"
            log.info(logstr)

    return True
Exemplo n.º 4
0
def showiprecords():
    """
    综合输出ip记录
    """
    namestr = 'everip'
    ip, wifi, wifiid, tun, device_id = iprecord()
    if ip is None:
        logstr = '无效ip,可能是没有处于联网状态'
        log.critical(logstr)
        sys.exit(1)
    print(f'{ip}\t{wifi}\t{wifiid}\t{tun}\t{device_id}')
    if not (guid := getcfpoptionvalue(namestr, device_id, 'guid')):
        token = getcfpoptionvalue('everwork', 'evernote', 'token')
        note_store = get_notestore()
        parentnotebook = note_store.getNotebook(
            '4524187f-c131-4d7d-b6cc-a1af20474a7f')
        evernoteapijiayi()
        note = ttypes.Note()
        note.title = f'服务器_{device_id}_ip更新记录'
        # note.title = "hengchu"
        print(note.title)
        note = makenote(token,
                        note_store,
                        note.title,
                        notebody='',
                        parentnotebook=parentnotebook)
        guid = note.guid
        setcfpoptionvalue(namestr, device_id, 'guid', guid)
Exemplo n.º 5
0
def inserttimeitem2db(dbname: str, timestampinput: int):
    '''
    insert timestamp to wcdelay db whose table name is wcdelay
    '''
    tablename = "wcdelaynew"
    checkwcdelaytable(dbname, tablename)

    # timetup = time.strptime(timestr, "%Y-%m-%d %H:%M:%S")
    # timest = time.mktime(timetup)
    elsmin = (int(time.time()) - timestampinput) // 60
    conn = False
    try:
        conn = lite.connect(dbname)
        cursor = conn.cursor()
        cursor.execute(
            f"insert into {tablename} (msgtime, delay) values(?, ?)",
            (timestampinput, elsmin))
        #         print(f"数据成功写入{dbname}\t{(timestampinput, elsmin)}")
        conn.commit()
    except Exception as e:
        logstr = f"数据库文件{dbname}存取错误!{e}"
        log.critical(logstr)
    finally:
        if conn:
            conn.close()
Exemplo n.º 6
0
def after_logout():
    men_wc = getcfpoptionvalue('everwebchat', get_host_uuid(), 'host_nickname')
    try:
        termux_sms_send(f"微信({men_wc})登录已退出,如有必要请重新启动")
    except Exception as e:
        log.critical(f"尝试发送退出提醒短信失败。{e}")
    log.critical(f'退出微信({men_wc})登录')
Exemplo n.º 7
0
def insertbattinfoitem2db(dbname: str, percentage: int, temperature: float):
    '''
    插入电池信息(电量百分比、温度)到数据表battinfo中
    '''
    tablename = "battinfo"
    checkbatteryinfotable(dbname, tablename)

    # timetup = time.strptime(timestr, "%Y-%m-%d %H:%M:%S")
    # timest = time.mktime(timetup)
#     elsmin = (int(time.time()) - timestampinput) // 60
    conn = False
    try:
        conn = lite.connect(dbname)
        cursor = conn.cursor()
        cursor.execute(
            f"insert into {tablename} values(?, ?, ?)", (time.time(
            ), percentage, temperature)
        )
#         print(f"数据成功写入{dbname}\t{(timestampinput, elsmin)}")
        conn.commit()
    except lite.IntegrityError as lie:
        logstr = f"键值重复错误\t{lie}"
        log.critical(logstr)
    finally:
        if conn:
            conn.close()
Exemplo n.º 8
0
def chuliweixinzhifuzhangdan(dmpath):
    """
    处理“微信支付账单”文件,生成DataFrame输出
    """

    file_list = os.listdir(dmpath)
    #     print(file_list)
    fnlst = [
        x for x in file_list if (x.startswith('微信支付账单') and (
            x.endswith('csv') or x.endswith('xlsx')))
    ]
    fnlst.sort(key=lambda fn: os.path.getmtime(dmpath / fn))

    rstdf = pd.DataFrame()
    for fn in fnlst:
        if not (filerecorded := getcfpoptionvalue('everwebchat', 'wczhifufile',
                                                  str(fn))):
            if fn.endswith('.csv'):
                singledf = pd.read_csv(dmpath / fn,
                                       header=16,
                                       parse_dates=True)
            elif fn.endswith('.xlsx'):
                content = xlrd.open_workbook(filename=dmpath / fn)
                singledf = pd.read_excel(content,
                                         header=16,
                                         parse_dates=True,
                                         engine='xlrd')
            else:
                logstr = f"读取《微信支付账单》文件时失败:\t{fn}"
                log.critical(logstr)
                continue

            rstdf = rstdf.append(singledf, ignore_index=True)
            print(rstdf.shape[0], end='\t')
            setcfpoptionvalue('everwebchat', 'wczhifufile', str(fn), str(True))
Exemplo n.º 9
0
def fetch_dutyondata2lite():
    zhutis = [['放假', 'holiday'], ['请假', 'leave'], ['打卡', 'checkin'],
              ['入职', 'dutyon'], ['高温', 'hot'], ['下雨', 'rain']]
    try:
        for zhuti in zhutis:
            dfresult = chuliholidayleave_note(zhuti)
            if zhuti[0] in ['高温', '下雨']:
                if zhuti[0] == '高温':
                    dffromgd, dfallfromgd = getgaowenfromgoogledrive()
                    dfresult = dfresult.append(dffromgd)
                    dfresult.drop_duplicates(['hottime'], inplace=True)
                else:
                    dffromgd, dfallfromgd = getrainfromgoogledrive()
                    dfresult = dfresult.append(dffromgd)
                    dfresult.drop_duplicates(['raintime'], inplace=True)
            countfromini = getcfpoptionvalue('everworkplan', '行政管理',
                                             f'{zhuti[0]}count')
            if not countfromini:
                countfromini = 0
            if countfromini == dfresult.shape[0]:
                log.info(f"本轮查询没有发现新的《{zhuti[0]}》相关数据,跳过!")
                continue
            cnxp = lite.connect(dbpathworkplan)
            # index, ['mingmu', 'xingzhi', 'tianshu', 'date']
            dfresult.to_sql(zhuti[1], cnxp, if_exists='replace', index=None)
            cnxp.close()
            log.info(f'{zhuti[0]}数据表更新了{dfresult.shape[0]}条记录。')
            setcfpoptionvalue('everworkplan', '行政管理', f'{zhuti[0]}count',
                              f"{dfresult.shape[0]}")
    except OSError as exp:
        topic = [x for [x, *y] in zhutis]
        log.critical(f'从evernote获取{topic}笔记信息时出现未名错误。{exp}')
Exemplo n.º 10
0
def zipdata2one_timer(jiangemiao):
    try:
        zipdir2one()
    except ValueError as wve:
        log.critical(f'自动备份至OneDrive目录时出现错误。{wve}')
    global timer_zip2one
    timer_zip2one = Timer(jiangemiao, zipdata2one_timer, [jiangemiao])
    timer_zip2one.start()
Exemplo n.º 11
0
    def parseheader(message):
        headermsg = []
        """ 解析邮件首部 """
        # 发件人
        # mailfrom = email.utils.parseaddr(message.get('from'))[1]
        # print('From:', mailfrom)

        # 时间
        datestr = message.get('date')
        if datestr is None:
            log.error('从邮件头部提取时间失败,只好从邮件内容中寻找时间信息。')
            pattern = re.compile(
                r'(?:X-smssync-backup-time: )(?P<date>\d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2})',
                re.I)
            items = re.split(pattern, str(message))
            if len(items) > 1:
                print(items[1])
                datemail = datetime.datetime.strptime(items[1],
                                                      '%d %b %Y %H:%M:%S')
            else:
                log.critical("从邮件内容中也没有找到有效的时间信息。")
                datemail = None
                # print(message)
        else:
            datemail = email.utils.parsedate_to_datetime(message.get('date'))

        localdate = datemail.astimezone(
            datetime.timezone(datetime.timedelta(hours=8)))
        # print('Date:', localdate)
        headermsg.append(localdate)
        # if mailfrom.startswith('*****@*****.**'):
        #     headermsg.append(str(localdate) + '\t发出\t')
        # else:
        #     headermsg.append(str(localdate) + '\t收到\t')

        # 主题
        subject = message.get('subject')
        # print(subject)
        subjectdecoded = str(
            email.header.make_header(email.header.decode_header(subject)))
        # print(subjectdecoded)
        headermsg.append(subjectdecoded)

        # 发件人
        mailfrom = email.utils.parseaddr(message.get('from'))[1]
        # print('From:', mailfrom)
        headermsg.append(mailfrom)

        # 收件人
        # print(message.get('to'))
        mailto = email.utils.parseaddr(message.get('to'))[1]
        # print('To:', mailto)
        headermsg.append(mailto)
        # print('To:', message.get('to'))
        # 抄送人
        # print('Cc:', email.utils.parseaddr(message.get_all('cc'))[1])

        return headermsg
Exemplo n.º 12
0
def execcmd(cmd):
    try:
        r = os.popen(cmd)
        text = r.read()
        r.close()
        return text.strip("\n")
    except Exception as e:
        log.critical(f"执行命令 {cmd} 时出现错误,返回空字符串。{e}")
        return ""
Exemplo n.º 13
0
def removesection(cfpfilename: str, sectionname: str):
    """
    删除指定section,默认清除其下面的所有option
    """
    cfpin, cfpinpath = getcfp(cfpfilename)
    if cfpin.has_section(sectionname):
        cfpin.remove_section(sectionname)
        cfpin.write(open(cfpinpath, 'w', encoding='utf-8'))
        log.critical(f"成功清除{sectionname}下的所有option!!!")
Exemplo n.º 14
0
def showorderstat2note(jiangemiao):
    try:
        showorderstat()
        # jiaoyanchanpinkehu()
    except NameError as nee:
        log.critical(f'处理订单核对统计笔记时出现错误。{nee}')

    global timer_showorderstat
    timer_showorderstat = Timer(jiangemiao, showorderstat2note, [jiangemiao])
    timer_showorderstat.start()
Exemplo n.º 15
0
def chulixls_orderdetails(orderfile: Path):
    try:
        content = xlrd.open_workbook( filename=orderfile, encoding_override='gb18030')
        df = pd.read_excel(content, index_col=0, parse_dates=True, engine='xlrd')
        log.info(f'读取{orderfile}')
        # print(list(df.columns))
    except UnicodeDecodeError as ude:
        log.critical(f'读取{orderfile}时出现解码错误。{ude}')
        return
    # ['日期', '单据编号', '摘要', '单位全名', '仓库全名', '商品编号', '商品全名', '规格', '型号', '产地', '单位', '数量', '单价', '金额', '数量1', '单价1',
    # '金额1', '数量2', '单价2', '金额2']
    totalin = ['%.2f' % df.loc[df.index.max()]['数量'], '%.2f' %
               df.loc[df.index.max()]['金额']]  # 从最后一行获取数量合计和金额合计,以备比较
    print(df['日期'].iloc[1], end='\t')
    print(totalin, end='\t')
    # df[xiangmu[0]] = None
    # df = df.loc[:, ['日期', '单据编号', '单据类型', xiangmu[0], '摘要', '备注', '商品备注', xiangmu[1],
    #                 '单价', '单位', '数量', '金额', '单位全名', '仓库全名', '部门全名']]
    df = df.loc[:, df.columns[:-6]]
    df['日期'] = pd.to_datetime(df['日期'])
    # df['备注'] = df['备注'].astype(object)
    dfdel = df[
        (df.单位全名.isnull().values == True) & ((df.单据编号.isnull().values == True) | (df.单据编号 == '小计') | (df.单据编号 == '合计'))]
    hangdel = list(dfdel.index)
    # print(hangdel)
    df1 = df.drop(hangdel)  # 丢掉小计和合计行,另起DataFrame
    dfzhiyuan = df1[df1.单位全名.isnull().values == True]  # 提取出项目名称行号
    zyhang = list(dfzhiyuan.index)
    zyming = list(dfzhiyuan['单据编号'])  # 项目名称

    # 每次填充df到最后一行,依次滚动更新之
    df['员工名称'] = None
    for i in range(len(zyhang)):
        df.loc[zyhang[i]:, '员工名称'] = zyming[i]

    # 丢掉项目名称行,留下纯数据
    dfdel = df[df.单位全名.isnull().values == True]
    # print(dfdel[['日期', '单据编号', '数量', '金额']])
    hangdel = list(dfdel.index)
    # print(hangdel)
    dfout = df.drop(hangdel)
    dfout.index = range(len(dfout))
    dfout = pd.DataFrame(dfout)
    # print(dfout)
    # print(dfout.head(10))
    log.info('共有%d条有效记录' % len(dfout))
    # print(list(dfout.columns))
    if (totalin[0] == '%.2f' % dfout.sum()['数量']) & (totalin[1] == '%.2f' % dfout.sum()['金额']):
        dfgrp = dfout.groupby(['员工名称']).sum()[['数量', '金额']]
        dfgrp.loc['汇总'] = dfgrp.sum()
        print(dfgrp.loc['汇总'].values)
        return dfout
    else:
        log.warning(f'对读入文件《{orderfile}》的数据整理有误!总数量和总金额对不上!')
        return
Exemplo n.º 16
0
def showorderstat2note(jiangemiao):
    global workplannotebookguid
    workplannotebookguid = '2c8e97b5-421f-461c-8e35-0f0b1a33e91c'
    try:
        orderdetails_check4product_customer()
    except Exception as ee:
        log.critical('处理订单核对统计笔记时出现错误。%s' % str(ee))
        raise ee

    global timer_showorderstat
    timer_showorderstat = Timer(jiangemiao, showorderstat2note, [jiangemiao])
    timer_showorderstat.start()
Exemplo n.º 17
0
def droptablefromdb(dbname: str, tablename: str, confirm=False):
    if not confirm:
        logstr = f"【警告】:数据表{tablename}将从{dbname}中删除,请确认!!!"
        log.critical(logstr)
    else:
        conn = lite.connect(dbname)
        cursor = conn.cursor()
        cursor.execute(f'drop table {tablename}')
        # cursor.execute(f'drop table cinfo')
        conn.commit()
        logstr = f"【警告】:数据表{tablename}已经从{dbname}中删除,谨以记!!!"
        log.critical(logstr)

        conn.close()
Exemplo n.º 18
0
def showiprecords():
    namestr = 'everip'
    ip, wifi, wifiid, tun, device_id = iprecord()
    if ip is None:
        log.critical('无效ip,可能是没有处于联网状态')
        exit(1)
    print(f'{ip}\t{wifi}\t{wifiid}\t{tun}\t{device_id}')
    if not (guid := getcfpoptionvalue(namestr, device_id, 'guid')):
        parentnotebookguid = '4524187f-c131-4d7d-b6cc-a1af20474a7f'
        note_title = f'服务器_{device_id}_ip更新记录'
        # note.title = "hengchu"
        print(note_title)
        note = makenote2(note_title, notebody='',
                        parentnotebook=parentnotebookguid)
        guid = note.guid
        setcfpoptionvalue(namestr, device_id, 'guid', guid)
Exemplo n.º 19
0
def notification2df(items):
    split_items = list()
    for itemstr in items:
        split_items.append(itemstr.strip().split('||| '))

    dfnoti = pd.DataFrame(split_items,
                          columns=('atime', 'shuxing', 'topic', 'content'))
    dfnoti['received'] = True
    # global log
    log.info('系统提醒记录有%d条。' % dfnoti.shape[0])
    # descdb(dfnoti)
    dfnoti.drop_duplicates(inplace=True)
    log.info('系统提醒记录去重后有%d条。' % dfnoti.shape[0])
    dfnoti.index = dfnoti['atime'].apply(lambda x: pd.to_datetime(
        datetime.datetime.strptime(x.strip(), '%B %d, %Y at %I:%M%p')) if len(
            x.split('at')) > 1 else pd.to_datetime(
                datetime.datetime.strptime(x.strip(), '%B %d, %Y')))
    # dfnoti.index = dfnoti['atime']
    del dfnoti['atime']
    dfnoti.sort_index(ascending=False, inplace=True)
    # descdb(dfnoti)
    dfout = dfnoti
    # b3a3e458-f05b-424d-8ec1-604a3e916724

    try:
        notestore = get_notestore()
        xiangmu = ['微信', '支付宝', 'QQ', '日历']
        cfplife, inilifepath = getcfp('everlife')
        for xm in xiangmu:
            biaoti = '系统提醒(%s)记录' % xm
            dfxm = dfnoti[dfnoti.shuxing == xm]
            if cfplife.has_option('lifenotecount', xm):
                ready2update = dfxm.shape[0] > cfplife.getint(
                    'lifenotecount', xm)
            else:
                ready2update = True
            print('%d\t%s\t%s' % (dfxm.shape[0], ready2update, biaoti))
            if ready2update:
                imglist2note(notestore, [], cfplife.get('notesguid', xm),
                             biaoti, tablehtml2evernote(dfxm[:1000], biaoti))
                cfplife.set('lifenotecount', xm, '%d' % dfxm.shape[0])
                cfplife.write(open(inilifepath, 'w', encoding='utf-8'))

    except Exception as ee:
        log.critical('更新系统提醒笔记时出现错误。%s' % str(ee))
    return dfout
Exemplo n.º 20
0
 def getnotestore():
     global note_store
     if note_store is not None:
         # log.info(f'note_store健壮存在:{note_store}')
         return note_store
     userstore = client.get_user_store()
     # evernoteapijiayi()
     version_ok = userstore.checkVersion("Evernote EDAMTest (Python)",
                                         EDAM_VERSION_MAJOR,
                                         EDAM_VERSION_MINOR)
     if not version_ok:
         log.critical('Evernote API版本过时,请更新之!程序终止并退出!!!')
         exit(1)
     # print("Is my Evernote API version up to date? ", str(version_ok))
     note_store = client.get_note_store()
     evernoteapijiayi()
     log.info(f'成功连接Evernote服务器!构建notestore:{note_store}')
     return note_store
Exemplo n.º 21
0
def istableindb(tablenin: str, dbname: str):
    result = False
    try:
        conn = lite.connect(dbname)
        cursor = conn.cursor()
        cursor.execute("select * from sqlite_master where type='table'")
        table = cursor.fetchall()
        # print(table)
        chali = [x for item in table for x in item[1:3]]
        # print(chali)
        result = tablenin in chali
    except Exception as eee:
        log.critical(f"查询数据表是否存在时出错。{eee}")
    finally:
        if 'conn' in locals():
            conn.close()

    return result
Exemplo n.º 22
0
def txtfiles2dfdict(dpath, newfileonly=False):
    """
    读取传入目录下符合标准(固定格式文件名)所有文本文件并提取融合分账号的df,
    返回字典{name:dict}
    """

    fllst = [f for f in os.listdir(dpath) if f.startswith("chatitems")]
    names = list(set([getownerfromfilename(nm) for nm in fllst]))
    print(names)
    # 如果设置为new,则找到每个账号的最新文本文件处理,否则是全部文本文件
    if newfileonly:
        fl3lst = [[getownerfromfilename(fl), fl,
                   getfltime(dpath / fl)] for fl in fllst]
        fllstout = list()
        for nm in names:
            fllstinner = [item for item in fl3lst if item[0] == nm]
            fllstout4name = sorted(fllstinner, key=lambda x: x[2])
            fllstout.extend(fllstout4name[-2:])
        fllst = [item[1] for item in fllstout]

#     print(fllst)
    dfdict = dict()
    for fl in fllst[::-1]:
        rs1 = re.search("\((\w*)\)", fl)
        if rs1 is None:
            log.critical(f"记录文件《{fl}》的文件名不符合规范,跳过")
            continue
        account = getownerfromfilename(fl)
        dfin = items2df(dpath / fl)
        print(f"{fl}\t{getfltime(dpath / fl).strftime('%F %T')}\t " \
            f"{account}\t{dfin.shape[0]}", end="\t")
        if account in dfdict.keys():
            dfall = dfdict[account].append(dfin)
            dfall = dfall.drop_duplicates().sort_values(['time'],
                                                        ascending=False)
            print(f"{dfall.shape[0]}")
            dfdict.update({account: dfall})
        else:
            dfall = dfin.drop_duplicates().sort_values(['time'],
                                                       ascending=False)
            print(f"{dfall.shape[0]}")
            dfdict[account] = dfall

    return dfdict
Exemplo n.º 23
0
def checktableindb(ininame: str,
                   dbpath: str,
                   tablename: str,
                   creattablesql: str,
                   confirm=False):
    """
    检查数据表(ini登记,物理存储)是否存在并根据情况创建
    """
    absdbpath = os.path.abspath(dbpath)  # 取得绝对路径,用于作为section名称
    if not (ifcreated := getcfpoptionvalue(ininame, absdbpath, tablename)):
        print(ifcreated)
        if istableindb(tablename, dbpath) and confirm:
            # 删表操作,危险,谨慎操作
            droptablefromdb(dbpath, tablename, confirm=confirm)
            logstr = f"数据表{tablename}于{dbpath}中被删除"
            log.critical(logstr)
        ifnotcreate(tablename, creattablesql, dbpath)

        setcfpoptionvalue(ininame, absdbpath, tablename, str(True))
Exemplo n.º 24
0
def dropdup4option(opcontent: str):
    """
    manuplicate the content(str), which is such as 'option'='value',
    then return clean option/value contents in list object, drop the duplicated
    options, keep the first value
    """
    ptno = re.compile("(\w+)\s*=\s*(\w*)")  # = pairs pattern
    opdict = dict()  # result container in dict
    fdlst = re.findall(ptno, opcontent)
    for item in fdlst:
        if item[0] in opdict.keys():
            log.critical(f"出现option名称重复:\t{item[0]},取用最新的数据")
        opdict.update(dict({item[0]: item[1]}))
    # make = pairs list
    rstlst = [
        ' = '.join(list(x)) for x in list(zip(opdict.keys(), opdict.values()))
    ]
    # add two new lines at head and tail
    return '\n' + '\n'.join(rstlst) + '\n\n'
Exemplo n.º 25
0
def splitjs(jsstr: str):
    """
    处理《计时工》原始打卡记录,规范成合规的时间间隔
    :param jsstr: str
    :return: str
    """
    zhongwuxiaban = getcfpoptionvalue('everinifromnote', 'xingzheng',
                                      'zhongwuxiaban')
    xiawushangban = getcfpoptionvalue('everinifromnote', 'xingzheng',
                                      'xiawushangban')
    xiawuxiaban = getcfpoptionvalue('everinifromnote', 'xingzheng',
                                    'xiawuxiaban')
    # print(zhongwuxiaban, xiawushangban, xiawuxiaban)
    ptn = re.compile('\\d{2}:\\d{2}')
    findlst = re.findall(ptn, jsstr)
    if len(findlst) == 2:
        if (findlst[0] <= zhongwuxiaban) & (findlst[1] >= xiawushangban):
            findlst.insert(1, xiawushangban)
            findlst.insert(1, zhongwuxiaban)
            log.info(f"计时工打卡记录条目({len(findlst)})增补中午休息间隔\t{findlst}")
        elif (findlst[0] >= zhongwuxiaban) & (findlst[1] >= xiawushangban):
            log.info(f"计时工打卡记录条目({len(findlst)})属于正常下午班")
        elif (findlst[0] <= zhongwuxiaban) & (findlst[1] <= xiawushangban):
            log.info(f"计时工打卡记录条目({len(findlst)})属于正常上午班")
        else:
            log.critical(f"出现不合规范的计时工打卡记录({len(findlst)})\t{findlst}")
            findlst = None
    elif len(findlst) == 3:
        if findlst[0] <= zhongwuxiaban:
            findlst.insert(1, zhongwuxiaban)
            log.info(f"计时工打卡记录条目({len(findlst)})增补中午休息间隔\t{findlst}")
        else:
            log.critical(f"出现不合规范的计时工打卡记录({len(findlst)})\t{findlst}")
            findlst = None
    elif len(findlst) == 1:
        if findlst[0] <= zhongwuxiaban:
            findlst.append(zhongwuxiaban)
            log.info(f"计时工打卡记录条目({len(findlst)})增补中午休止\t{findlst}")
        elif findlst[0] >= zhongwuxiaban:
            findlst.append(xiawuxiaban)
            log.info(f"计时工打卡记录条目({len(findlst)})增补下午休止\t{findlst}")
        else:
            log.critical(f"出现不合规范的计时工打卡记录({len(findlst)})\t{findlst}")
            findlst = None
    elif len(findlst) == 4:
        pass
    elif len(findlst) != 0:
        log.critical(f"出现不合规范的计时工打卡记录({len(findlst)})\t{findlst}")
        findlst = None
    resultlst = findlst

    return resultlst
Exemplo n.º 26
0
def dropdup4section(fcontent):
    ftn = re.compile(r"\[\w+\]")
    sectionlst = re.findall(ftn, fcontent)
    optionlst = re.split(ftn, fcontent)
    resultdict = dict()
    for i in range(len(sectionlst)):
        sname = sectionlst[i]
        if sname in resultdict.keys():
            thislen = len(resultdict[sname])
            thatlen = len(optionlst[i + 1])
            log.critical(f"存在重复的section:\t{sname}\t{thislen}\t{thatlen}")
            if thislen > thatlen:
                continue
        cleanopcontent = dropdup4option(optionlst[i + 1])
        resultdict.update({sname: cleanopcontent})
    rstlst = [
        x for y in list(zip(resultdict.keys(), resultdict.values())) for x in y
    ]
    correctcontent = ''.join(rstlst)

    return correctcontent
Exemplo n.º 27
0
def lststr2img(inputcontent,
               fontpath=dirmainpath / 'font' / 'msyh.ttf',
               title=None,
               showincell=False,
               fontsize=12,
               dpi=300,
               debug=False):
    if type(inputcontent) == str:
        dflines = inputcontent.split('\n')
    elif type(inputcontent) == list:
        dflines = inputcontent
    else:
        logstr = f"传入参数类型为:\t{type(inputcontent)},既不是str也不是list,暂不做处理返回None"
        log.critical(logstr)
        return

    rows = len(dflines)
    collenmax = max([len(x) for x in dflines])
    print(f"行数和行最长长度(字符):\t{(rows, collenmax)}")
    font = ImageFont.truetype(str(fontpath), fontsize)
    print(str(fontpath))
    colwidthmax = max([font.getsize(x)[0] for x in dflines])
    rowwidth = max([font.getsize(x)[1] for x in dflines])
    print(
        f"行高度、所有行总高度和所有列宽度(像素):\t{(rowwidth, rowwidth * len(dflines), colwidthmax)}"
    )

    print(f"画布宽高(像素):\t{(colwidthmax, rowwidth * len(dflines))}")
    im = Image.new("RGB", (colwidthmax, rowwidth * len(dflines)),
                   (255, 255, 255))
    dr = ImageDraw.Draw(im)

    i = 0
    for line in dflines:
        dr.text((0, 0 + rowwidth * i), line, font=font, fill="#000000")
        i += 1

    if not debug:
        if (notedpi := getinivaluefromnote('webchat', 'imgdpi')):
            dpi = notedpi
Exemplo n.º 28
0
def chulixls_zhifubao(orderfile):
    try:
        content = xlrd.open_workbook(filename=orderfile,
                                     encoding_override='gb18030')
        df = pd.read_excel(content,
                           index_col=0,
                           header=2,
                           parse_dates=True,
                           engine='xlrd')[:-1]
        log.info(f'读取{orderfile}, 共有{df.shape[0]}条有效记录')
        # print(df)
        df.columns = [
            '日期', '支付宝交易号', '支付宝流水号', '商户订单号', '账务类型', '收入(+元)', '支出(-元)',
            '账户余额(元)', '服务费(元)', '支付渠道', '签约产品', '对方账户', '对方名称', '银行订单号',
            '商品名称', '备注'
        ]
        df['日期'] = pd.to_datetime(df['日期'])
        print(df.columns)
        return df
    except UnicodeDecodeError as ude:
        log.critical(f'读取{orderfile}时出现解码错误。{ude}')
        return
Exemplo n.º 29
0
def log2notetimer(jiangemiao):
    print(getdirmain())
    pathlog = getdirmain() / 'log'
    files = os.listdir(str(pathlog))
    loglines = []
    for fname in files[::-1]:
        with open(pathlog / fname, 'r', encoding='utf-8') as flog:
            loglines = loglines + [
                line.strip() for line in flog if line.find('CRITICAL') >= 0
            ]

    print(f'日志共有{len(loglines)}条记录')
    # global cfp, inifilepath
    cfp, cfppath = getcfp('everwork')
    everlogc = cfp.getint('evernote', 'everlogc')
    if len(loglines) == everlogc:  # <=调整为==,用来应对log文件崩溃重建的情况
        log.info('暂无新记录,不更新everworklog笔记。')
    else:
        loglinestr = '\n'.join(loglines[::-1])
        loglinestr = loglinestr.replace('<', '《')
        loglinestr = loglinestr.replace('>', '》')
        loglinestr = loglinestr.replace('&', '并符')
        loglinestr = loglinestr.replace('=', '等于')
        loglinestr = '<pre>' + loglinestr + '</pre>'
        # print(loglinestr)
        noteguid_lognote = '4a940ff2-74a8-4584-be46-aa6d68a4fa53'
        try:
            nstore = get_notestore()
            imglist2note(nstore, [], noteguid_lognote, 'everwork日志严重错误信息',
                         loglinestr)
            cfp.set('evernote', 'everlogc', '%d' % len(loglines))
            cfp.write(open(cfppath, 'w', encoding='utf-8'))
            log.info('新的log错误信息成功更新入笔记,将于%d秒后再次自动检查并更新' % jiangemiao)
        except Exception as eeee:
            log.critical('处理新log错误信息到笔记时出现未名错误。%s' % (str(eeee)))

    global timer_log2note
    timer_log2note = Timer(jiangemiao, log2notetimer, [jiangemiao])
    timer_log2note.start()
Exemplo n.º 30
0
def geturlcontentwrite2excel(ownername, url):
    """
    处理url,提取网页内容,有效数据写入数据文件,return url valid, if not valid, [url]
    """
    excelpath, ownpy = makeexcelfileownpy(ownername)
    tdf = getsinglepage(url)
    print(tdf)
    if tdf.shape[0] > 0:
        roomid = tdf.iloc[0, 0]
        recorddf = pd.read_excel(excelpath)
        #vdrop dupliceres at first, I studun there for many times
        recorddf.drop_duplicates(['roomid', 'guestid'], inplace=True)
        oldsize = recorddf.shape[0]
        rstdf = recorddf.append(
            tdf, ignore_index=True)  # 使用ignore_index开关,避免追加数据因为索引问题导致错乱
        # 修正用户别名
        rstdf = fixnamebyguestid(rstdf)
        rstdf = rstdf.sort_values(by=['time', 'score'],
                                  ascending=[False, False])
        rstdf = rstdf.drop_duplicates(['roomid', 'guestid'])
        print(rstdf.iloc[:16, ])
        if rstdf.shape[0] == oldsize:
            descstr = f"room {roomid} is already recorded. till then the recordsize is {oldsize}"
            log.warning(descstr)
        else:
            excelwriter = pd.ExcelWriter(excelpath)
            rstdf.to_excel(excelwriter, index=False, encoding='utf-8')
            excelwriter.close()
            descstr = f"{rstdf.shape[0]}条记录写入文件\t{excelpath}, new roomid {roomid} record done now.\n{tdf[['guest', 'score']]}"
            log.info(descstr)
        outurl = url
    else:
        descstr = f"no valid content there, 这个网页貌似无效\t{url}"
        log.critical(descstr)
        outurl = f"[{url}]"

    return outurl, descstr