Initial commit
This commit is contained in:
Generated
+8
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
Generated
+19
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="gmzz" uuid="ce5a7cb8-0518-4b95-be83-9ab3ee2f6586">
|
||||
<driver-ref>sqlite.xerial</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||
<jdbc-url>jdbc:sqlite:E:\study\Projects\Python_Assignments2021\reader\gmzz.sqlite3</jdbc-url>
|
||||
<libraries>
|
||||
<library>
|
||||
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.25.1/license.txt</url>
|
||||
</library>
|
||||
<library>
|
||||
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.25.1/sqlite-jdbc-3.25.1.jar</url>
|
||||
</library>
|
||||
</libraries>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+14
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PublishConfigData">
|
||||
<serverData>
|
||||
<paths name="root@47.97.197.244:22">
|
||||
<serverdata>
|
||||
<mappings>
|
||||
<mapping local="$PROJECT_DIR$" web="/" />
|
||||
</mappings>
|
||||
</serverdata>
|
||||
</paths>
|
||||
</serverData>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/reader.iml" filepath="$PROJECT_DIR$/.idea/reader.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.7" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
import sqlite3
|
||||
import traceback
|
||||
|
||||
|
||||
class DbConfig:
|
||||
"""
|
||||
从数据库中获取企业微信通信的相关参数
|
||||
"""
|
||||
|
||||
def __init__(self, table, db_path):
|
||||
self.table = table
|
||||
self.db_path = db_path
|
||||
|
||||
def initial_table(self):
|
||||
"""
|
||||
初始化数据以及库表
|
||||
:return:
|
||||
"""
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
create table if not exists gmzz
|
||||
(
|
||||
chapter_name text,
|
||||
chapter_content text,
|
||||
last_chapter text,
|
||||
update_time bigint
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def get_params(self, *params):
|
||||
"""
|
||||
自定义获取参数
|
||||
:param params:list: access_token、crop_id、crop_secret、token、enconding_aes_key
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
sql = "select {} from {} ".format(",".join(list(params[0])), self.table)
|
||||
rtn = cursor.execute(sql)
|
||||
result = list(rtn)
|
||||
conn.close()
|
||||
return result
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
|
||||
def simple_query(self, sql):
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
rtn = cursor.execute(sql)
|
||||
conn.close()
|
||||
return list(rtn)
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
|
||||
def add_one(self,fields, values):
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
sql = "insert into {} ({}) values ('{}')".format(self.table, ",".join(fields), ",".join(values))
|
||||
print(sql)
|
||||
rtn = cursor.execute(sql)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return rtn
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
|
||||
def update_one(self, field_name, update_value):
|
||||
"""
|
||||
更新某个字段
|
||||
:param field_name: 字段名称
|
||||
:param update_value: 跟新值
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
sql = "update {table} set {field_name} = '{update_value}'".format(
|
||||
table=self.table,
|
||||
field_name=field_name,
|
||||
update_value=update_value
|
||||
)
|
||||
conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(sql)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
Binary file not shown.
@@ -0,0 +1,90 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import codecs
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from db_utils import DbConfig
|
||||
|
||||
|
||||
class Reader:
|
||||
|
||||
def __init__(self, book_name, txt_path):
|
||||
self.book_name = book_name
|
||||
self.txt_path = txt_path
|
||||
self.db = DbConfig(self.book_name, "./{}.sqlite3".format(self.book_name))
|
||||
self.db.initial_table() # 初始化数据库,用来存放阅读历史
|
||||
self.contents = []
|
||||
self.book = self.split_book_chapter()
|
||||
|
||||
def split_book_chapter(self):
|
||||
"""
|
||||
读取文本文件,并将文本按章节划分
|
||||
:param txt_path:
|
||||
:return:
|
||||
"""
|
||||
book_content = {}
|
||||
with codecs.open(self.txt_path, "r", encoding="gbk") as f:
|
||||
text = f.read().strip()
|
||||
chapters = re.split("\n第", text)
|
||||
for chapter in chapters:
|
||||
text = chapter.split("\n")
|
||||
title = text[0]
|
||||
text = "\n".join(text)
|
||||
book_content[title] = text
|
||||
self.contents = list(book_content.keys())
|
||||
return book_content
|
||||
|
||||
def get_index(self, key_name):
|
||||
"""
|
||||
根据书的章节名称,获取前一章与后一章
|
||||
:param key_name:
|
||||
:return:
|
||||
"""
|
||||
this_index = self.contents.index(key_name)
|
||||
last_chapter = self.contents[this_index - 1]
|
||||
next_chapter = self.contents[this_index + 1]
|
||||
return last_chapter, next_chapter
|
||||
|
||||
def start_read(self, chapter_name=None):
|
||||
"""
|
||||
开始阅读, 章节名称为空,且数据库中没有历史记录时,从第一章开始
|
||||
:param chapter_name: 章节名称
|
||||
:return:
|
||||
"""
|
||||
|
||||
last_chapter = self.db.get_params(["last_chapter"])
|
||||
# 如果没有历史记录或者指定章节,则是第一次阅读,在数据库中新增
|
||||
if not last_chapter:
|
||||
self.db.add_one(["last_chapter"], [self.contents[0]])
|
||||
if not chapter_name:
|
||||
chapter_name = last_chapter[0] if last_chapter else self.contents[0]
|
||||
chapter_name = chapter_name[0] if isinstance(chapter_name, tuple) else chapter_name
|
||||
read_content = "".join(self.book[chapter_name])
|
||||
print(read_content)
|
||||
forward = ""
|
||||
while forward not in ["n", "b", "q"]:
|
||||
forward = input("""
|
||||
n、下一章
|
||||
b、上一章
|
||||
q、退出
|
||||
""")
|
||||
|
||||
last_chapter_name, next_chapter_name = self.get_index(chapter_name)
|
||||
if forward == "q":
|
||||
sys.exit(0)
|
||||
# 根据命令选择上一章或者下一章,并更新历史记录
|
||||
if forward == "n":
|
||||
self.db.update_one("last_chapter", next_chapter_name)
|
||||
return next_chapter_name
|
||||
if forward == "b":
|
||||
self.db.update_one("last_chapter", last_chapter_name)
|
||||
return last_chapter_name
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
mybook = Reader("gmzz", "test.txt")
|
||||
chapter_name = None
|
||||
while True:
|
||||
os.system('cls')
|
||||
chapter_name = mybook.start_read(chapter_name)
|
||||
Reference in New Issue
Block a user