Add search functionality
This commit is contained in:
parent
d8987b616d
commit
c9506e72b8
1 changed files with 23 additions and 10 deletions
33
slixfeed.py
33
slixfeed.py
|
@ -263,7 +263,6 @@ def print_help():
|
||||||
" https://slixmpp.readthedocs.io/ \n"
|
" https://slixmpp.readthedocs.io/ \n"
|
||||||
" feedparser \n"
|
" feedparser \n"
|
||||||
" https://pythonhosted.org/feedparser")
|
" https://pythonhosted.org/feedparser")
|
||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
# Function from buku
|
# Function from buku
|
||||||
|
@ -295,7 +294,6 @@ def get_default_dbdir():
|
||||||
return os.path.abspath('.')
|
return os.path.abspath('.')
|
||||||
else:
|
else:
|
||||||
data_home = os.path.join(os.environ.get('HOME'), '.local', 'share')
|
data_home = os.path.join(os.environ.get('HOME'), '.local', 'share')
|
||||||
|
|
||||||
return os.path.join(data_home, 'slixfeed')
|
return os.path.join(data_home, 'slixfeed')
|
||||||
|
|
||||||
# TODO Perhaps this needs to be executed
|
# TODO Perhaps this needs to be executed
|
||||||
|
@ -306,7 +304,6 @@ def initdb(jid, message, callback):
|
||||||
os.mkdir(db_dir)
|
os.mkdir(db_dir)
|
||||||
os.chdir(db_dir)
|
os.chdir(db_dir)
|
||||||
db_file = r"{}.db".format(jid)
|
db_file = r"{}.db".format(jid)
|
||||||
|
|
||||||
feeds_table_sql = """
|
feeds_table_sql = """
|
||||||
CREATE TABLE IF NOT EXISTS feeds (
|
CREATE TABLE IF NOT EXISTS feeds (
|
||||||
id integer PRIMARY KEY,
|
id integer PRIMARY KEY,
|
||||||
|
@ -315,7 +312,6 @@ def initdb(jid, message, callback):
|
||||||
status integer,
|
status integer,
|
||||||
updated text
|
updated text
|
||||||
); """
|
); """
|
||||||
|
|
||||||
entries_table_sql = """
|
entries_table_sql = """
|
||||||
CREATE TABLE IF NOT EXISTS entries (
|
CREATE TABLE IF NOT EXISTS entries (
|
||||||
id integer PRIMARY KEY,
|
id integer PRIMARY KEY,
|
||||||
|
@ -325,10 +321,8 @@ def initdb(jid, message, callback):
|
||||||
source text,
|
source text,
|
||||||
read integer
|
read integer
|
||||||
); """
|
); """
|
||||||
|
|
||||||
# create a database connection
|
# create a database connection
|
||||||
conn = create_connection(db_file)
|
conn = create_connection(db_file)
|
||||||
|
|
||||||
# create tables
|
# create tables
|
||||||
if conn is not None:
|
if conn is not None:
|
||||||
# create projects table
|
# create projects table
|
||||||
|
@ -336,7 +330,6 @@ def initdb(jid, message, callback):
|
||||||
create_table(conn, entries_table_sql)
|
create_table(conn, entries_table_sql)
|
||||||
else:
|
else:
|
||||||
print("Error! cannot create the database connection.")
|
print("Error! cannot create the database connection.")
|
||||||
|
|
||||||
if message:
|
if message:
|
||||||
return callback(conn, message)
|
return callback(conn, message)
|
||||||
else:
|
else:
|
||||||
|
@ -355,7 +348,6 @@ def create_connection(db_file):
|
||||||
return conn
|
return conn
|
||||||
except Error as e:
|
except Error as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def create_table(conn, create_table_sql):
|
def create_table(conn, create_table_sql):
|
||||||
|
@ -435,11 +427,9 @@ async def download_page(url):
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
html = await response.text()
|
html = await response.text()
|
||||||
return html
|
return html
|
||||||
|
|
||||||
print("Status:", response.status)
|
print("Status:", response.status)
|
||||||
print("Content-type:", response.headers['content-type'])
|
print("Content-type:", response.headers['content-type'])
|
||||||
|
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.run_until_complete
|
loop.run_until_complete
|
||||||
|
|
||||||
|
@ -708,6 +698,29 @@ def last_entries(conn, num):
|
||||||
""".format(str(result[0]), str(result[1]))
|
""".format(str(result[0]), str(result[1]))
|
||||||
return titles_list
|
return titles_list
|
||||||
|
|
||||||
|
def search_entries(conn, query):
|
||||||
|
"""
|
||||||
|
Query feeds
|
||||||
|
:param conn:
|
||||||
|
:param query: string
|
||||||
|
:return: rows (string)
|
||||||
|
"""
|
||||||
|
cur = conn.cursor()
|
||||||
|
sql = "SELECT title, link FROM entries WHERE title LIKE '%{}%' LIMIT 50".format(query)
|
||||||
|
# sql = "SELECT title, link FROM entries WHERE title OR link LIKE '%{}%'".format(query)
|
||||||
|
results = cur.execute(sql)
|
||||||
|
results_list = "Search results for '{}': \n".format(query)
|
||||||
|
counter = 0
|
||||||
|
for result in results:
|
||||||
|
counter += 1
|
||||||
|
# titles_list += """\nTitle: {} \nLink: {}
|
||||||
|
results_list += """\n{} \n{}
|
||||||
|
""".format(str(result[0]), str(result[1]))
|
||||||
|
if counter:
|
||||||
|
return results_list + "\n Total of {} results".format(counter)
|
||||||
|
else:
|
||||||
|
return "No results found for: {}.".format(query)
|
||||||
|
|
||||||
def check_entry(conn, title, link):
|
def check_entry(conn, title, link):
|
||||||
"""
|
"""
|
||||||
Check whether an entry exists
|
Check whether an entry exists
|
||||||
|
|
Loading…
Reference in a new issue