示例#1
0
def pak_pwb(book_name, pwb_input):
    # 连接数据库
    db_name = "db/" + book_name + ".db"
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    # 获取书籍、文章数目
    num_book = ""
    num_article = ""
    book = cur.execute("select count(*) from Folder")
    for i in book:
        num_book = str(i)[1:-2]
    article = cur.execute("select count(*) from Article")
    for i in article:
        num_article = str(i)[1:-2]
    conn.close()
    # 打包为pwb
    if os.path.isdir("tmp"):
        shutil.rmtree("tmp")
    os.makedirs("tmp")
    pwb_name = "PureWriterBackup-" + num_book + "本书-" + num_article + "篇文章-" + pwb_input
    pwb_newest = helper_main.findnewestfile("pwb")
    os.system("7z.exe x pwb/" + pwb_newest + " -y -aos -otmp -x!*.db")
    # os.rename(db_name,"tmp/"+pwb_name+".db")
    os.system("copy .\\db\\" + book_name + ".db .\\tmp\\" + pwb_name + ".db")
    os.system("7z.exe a -tzip tmp.pwb .\\tmp/*")
    os.rename("tmp.pwb", "pwb/" + pwb_name + ".pwb")
    os.system("rd/s/q tmp")
def make():
    db_name = "db\\" + helper_main.findnewestfile("db")
    # 连接数据库
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    # 读取书籍
    book = cur.execute("select name from Folder")
    print("====================\n请输入分章导出的书籍编号:")
    book_id = int(input())
    tmp = 0
    for i in book:
        if tmp == book_id:
            book_name = str(i)[2:-3]
            break
        tmp += 1

    folder = cur.execute("select * from Folder")
    for i in folder:
        if i[1] == book_name:
            folder_id = i[0]
            break
    # 读取分卷
    categories = cur.execute("select * from Category order by rank")
    category_selected_name = []
    category_selected_rank = []
    for i in categories:
        if i[1] == folder_id:
            category_selected_name.append(i[2])
            category_selected_rank.append(i[5])

    # 创建文件夹
    if os.path.isdir("output/" + book_name):
        shutil.rmtree("output/" + book_name)
    os.makedirs("output/" + book_name)
    for i in category_selected_name:
        if not os.path.isdir("output/" + book_name + "/" + i):
            os.makedirs("output/" + book_name + "/" + i)

    # 写入文章内容
    articles = cur.execute("select * from Article where folderId=\'" +
                           folder_id + "\' order by rank asc")
    for i in articles:
        if i[9] == 0:
            with open("output/" + book_name + "/" + i[1] + "." + i[3],
                      "w",
                      encoding="utf-8") as f:
                f.write(i[2])
        index = between(category_selected_rank, i[9])
        if index == None:
            folder = category_selected_name[len(category_selected_rank) - 1]
        else:
            folder = category_selected_name[index]
        with open("output/" + book_name + "/" + folder + "/" + i[1] + "." +
                  i[3],
                  "w",
                  encoding="utf-8") as f:
            f.write(i[2])

    cur.close()
    print("默认导出至output目录下。")
def gui():
    os.system("cls")
    # 连接数据库
    db_name = "db\\" + helper_main.findnewestfile("db")
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    # 读取书籍
    book = cur.execute("select name from Folder")
    print("书架上有以下书籍:")
    book_ser = 0
    for i in book:
        print(str(book_ser) + ":" + str(i)[2:-3])
        book_ser += 1

    cur.close()
    print("====================\n0:返回\n1:分章导出\n2:合并导出")
    ctrl()
def make_one():
    db_name = "db\\" + helper_main.findnewestfile("db")
    # 连接数据库
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    # 读取书籍
    book = cur.execute("select name from Folder")
    print("====================\n请输入合并导出的书籍编号:")
    book_id = int(input())
    tmp = 0
    for i in book:
        if tmp == book_id:
            book_name = str(i)[2:-3]
            break
        tmp += 1

    folder = cur.execute("select * from Folder")
    for i in folder:
        if i[1] == book_name:
            folder_id = i[0]
            break
    # 读取分卷
    categories = cur.execute("select * from Category order by rank")
    category_selected_name = []
    category_selected_rank = []
    for i in categories:
        if i[1] == folder_id:
            category_selected_name.append(i[2])
            category_selected_rank.append(i[5])
    # 创建文件夹
    if not os.path.isdir("output"):
        os.makedirs("output")
    # 写入文章内容
    articles = cur.execute("select * from Article where folderId=\'" +
                           folder_id + "\' order by rank asc")
    all_articles = ""
    for i in articles:
        all_articles += "\n====================\n" + i[
            1] + "\n====================\n" + i[2] + "\n"

    with open("output/" + book_name + ".txt", "w", encoding="utf-8") as f:
        f.write(all_articles)

    cur.close()
    print("默认导出至output目录下。")
示例#5
0
def pak_db(pak_path, new_book_name):
    now_time = int(time.time() * 1000)
    # 遍历文件夹
    file_lst, sum_lst = lst_dir(pak_path, new_book_name)
    # 连接数据库
    # helper_main.unzip()
    db_newest = helper_main.findnewestfile("db")
    try:
        f = open("db/" + new_book_name + ".db", 'r')
        f.close()
    except IOError:
        os.rename("db/" + db_newest, "db/" + new_book_name + ".db")
    db_name = "db/" + new_book_name + ".db"
    conn = sqlite3.connect(db_name)
    cur = conn.cursor()
    # 得到Folder下最大rank值
    book_rank = cur.execute("select rank from Folder order by rank")
    for i in book_rank:
        new_book_rank = str(i[0] + 1)
    # 生成16位16进制id
    book_id = cur.execute("select id from Folder")
    ran_folderId = gen_16hex()
    id_same = True
    while id_same:
        for i in book_id:
            if ran_folderId == str(i)[2:-3]:
                ran_folderId = gen_16hex()
        id_same = False
    # 插入书名
    cur.execute(
        "insert into Folder (id,name,createdTime,rank,deleted,deletedTime) values (\""
        + ran_folderId + "\",\"" + new_book_name + "\"," + str(now_time) +
        "," + new_book_rank + ",0,0)")

    rank_num = 0
    sum_num = 0
    cafe_rank = [2]
    for i in file_lst[:-1]:
        sum_num += len(i)
        cafe_rank.append(2 * len(i))
    # 获取分卷rank放入数组
    a = 0
    b = 0
    for j in cafe_rank:
        if b < len(cafe_rank) - 1:
            b += 1
        cafe_rank[b] += cafe_rank[b - 1]
    cafe_rank = cafe_rank[:-1]
    # 插入数据
    while (rank_num < 2 * (sum_num) + 1):
        rank = 0
        if rank_num != 0:
            rank = sum_lst[int(rank_num / 2) - 1]
        # 插入无分卷的文章
        if rank_num == 0:
            for i in file_lst[len(file_lst) - 1]:
                if i != file_lst[len(file_lst) - 1][0]:
                    pre, suf = sep_str(i)
                    article_str = file_str(pak_path + "/" + new_book_name +
                                           "/" + i)
                    insert_article(cur, pre, article_str, suf, str(now_time),
                                   ran_folderId, str(rank_num))
        # 插入分卷
        elif rank_num in cafe_rank:
            insert_category(cur, ran_folderId, rank, str(now_time),
                            str(rank_num))
        # 插入分卷下文章
        else:
            pre, suf = sep_str(rank)
            n = 0
            for i in reversed(cafe_rank):
                n += 1
                if rank_num > i:
                    for j in file_lst[len(cafe_rank) - n][1:]:
                        article_str = file_str(pak_path + "/" + new_book_name +
                                               "/" + file_lst[len(cafe_rank) -
                                                              n][0] + "/" + j)
                        insert_article(cur, pre, article_str, suf,
                                       str(now_time), ran_folderId,
                                       str(rank_num))
        rank_num += 2

    conn.commit()
    conn.close()