Update slixfeed.py
This commit is contained in:
parent
0e9fb54821
commit
0327b54624
1 changed files with 36 additions and 34 deletions
70
slixfeed.py
70
slixfeed.py
|
@ -78,7 +78,7 @@ class Slixfeed(slixmpp.ClientXMPP):
|
||||||
self.send_presence()
|
self.send_presence()
|
||||||
await self.get_roster()
|
await self.get_roster()
|
||||||
|
|
||||||
def message(self, msg):
|
async def message(self, msg):
|
||||||
"""
|
"""
|
||||||
Process incoming message stanzas. Be aware that this also
|
Process incoming message stanzas. Be aware that this also
|
||||||
includes MUC messages and error messages. It is usually
|
includes MUC messages and error messages. It is usually
|
||||||
|
@ -100,37 +100,37 @@ class Slixfeed(slixmpp.ClientXMPP):
|
||||||
elif message.lower().startswith('feed recent '):
|
elif message.lower().startswith('feed recent '):
|
||||||
print("COMMAND: feed recent")
|
print("COMMAND: feed recent")
|
||||||
print("ACCOUNT: " + str(msg['from']))
|
print("ACCOUNT: " + str(msg['from']))
|
||||||
action = initdb(msg['from'].bare,
|
action = await initdb(msg['from'].bare,
|
||||||
message[12:],
|
message[12:],
|
||||||
last_entries)
|
last_entries)
|
||||||
elif message.lower().startswith('feed search '):
|
elif message.lower().startswith('feed search '):
|
||||||
print("COMMAND: feed search")
|
print("COMMAND: feed search")
|
||||||
print("ACCOUNT: " + str(msg['from']))
|
print("ACCOUNT: " + str(msg['from']))
|
||||||
action = initdb(msg['from'].bare,
|
action = await initdb(msg['from'].bare,
|
||||||
message[12:],
|
message[12:],
|
||||||
search_entries)
|
search_entries)
|
||||||
elif message.lower().startswith('feed list'):
|
elif message.lower().startswith('feed list'):
|
||||||
print("COMMAND: feed list")
|
print("COMMAND: feed list")
|
||||||
print("ACCOUNT: " + str(msg['from']))
|
print("ACCOUNT: " + str(msg['from']))
|
||||||
action = initdb(msg['from'].bare,
|
action = await initdb(msg['from'].bare,
|
||||||
False,
|
False,
|
||||||
list_subscriptions)
|
list_subscriptions)
|
||||||
elif message.lower().startswith('feed add '):
|
elif message.lower().startswith('feed add '):
|
||||||
print("COMMAND: feed add")
|
print("COMMAND: feed add")
|
||||||
print("ACCOUNT: " + str(msg['from']))
|
print("ACCOUNT: " + str(msg['from']))
|
||||||
action = initdb(msg['from'].bare,
|
action = await initdb(msg['from'].bare,
|
||||||
message[9:],
|
message[9:],
|
||||||
add_feed)
|
add_feed)
|
||||||
elif message.lower().startswith('feed remove '):
|
elif message.lower().startswith('feed remove '):
|
||||||
print("COMMAND: feed remove")
|
print("COMMAND: feed remove")
|
||||||
print("ACCOUNT: " + str(msg['from']))
|
print("ACCOUNT: " + str(msg['from']))
|
||||||
action = initdb(msg['from'].bare,
|
action = await initdb(msg['from'].bare,
|
||||||
message[12:],
|
message[12:],
|
||||||
remove_feed)
|
remove_feed)
|
||||||
elif message.lower().startswith('feed status '):
|
elif message.lower().startswith('feed status '):
|
||||||
print("COMMAND: feed status")
|
print("COMMAND: feed status")
|
||||||
print("ACCOUNT: " + str(msg['from']))
|
print("ACCOUNT: " + str(msg['from']))
|
||||||
action = initdb(msg['from'].bare,
|
action = await initdb(msg['from'].bare,
|
||||||
message[12:],
|
message[12:],
|
||||||
toggle_status)
|
toggle_status)
|
||||||
elif message.lower().startswith('enable'):
|
elif message.lower().startswith('enable'):
|
||||||
|
@ -171,7 +171,7 @@ class Slixfeed(slixmpp.ClientXMPP):
|
||||||
# d = self.send_ping(self, jid)
|
# d = self.send_ping(self, jid)
|
||||||
# print('d')
|
# print('d')
|
||||||
# print(d)
|
# print(d)
|
||||||
new = initdb(jid, False, get_unread)
|
new = await initdb(jid, False, get_unread)
|
||||||
if new:
|
if new:
|
||||||
msg = self.make_message(mto=jid, mbody=new,
|
msg = self.make_message(mto=jid, mbody=new,
|
||||||
mtype='chat')
|
mtype='chat')
|
||||||
|
@ -209,9 +209,11 @@ async def check_updates():
|
||||||
files = os.listdir()
|
files = os.listdir()
|
||||||
for file in files:
|
for file in files:
|
||||||
jid = file[:-3]
|
jid = file[:-3]
|
||||||
await initdb(jid,
|
await initdb(
|
||||||
|
jid,
|
||||||
False,
|
False,
|
||||||
download_updates)
|
download_updates
|
||||||
|
)
|
||||||
await asyncio.sleep(60 * 30)
|
await asyncio.sleep(60 * 30)
|
||||||
|
|
||||||
asyncio.ensure_future(check_updates())
|
asyncio.ensure_future(check_updates())
|
||||||
|
@ -298,7 +300,7 @@ def get_default_dbdir():
|
||||||
|
|
||||||
# TODO Perhaps this needs to be executed
|
# TODO Perhaps this needs to be executed
|
||||||
# just once per program execution
|
# just once per program execution
|
||||||
def initdb(jid, message, callback):
|
async def initdb(jid, message, callback):
|
||||||
db_dir = get_default_dbdir()
|
db_dir = get_default_dbdir()
|
||||||
if not os.path.isdir(db_dir):
|
if not os.path.isdir(db_dir):
|
||||||
os.mkdir(db_dir)
|
os.mkdir(db_dir)
|
||||||
|
@ -331,9 +333,9 @@ def initdb(jid, message, callback):
|
||||||
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 await callback(conn, message)
|
||||||
else:
|
else:
|
||||||
return callback(conn)
|
return await callback(conn)
|
||||||
|
|
||||||
def create_connection(db_file):
|
def create_connection(db_file):
|
||||||
"""
|
"""
|
||||||
|
@ -371,7 +373,7 @@ async def download_updates(conn):
|
||||||
# cur = conn.cursor()
|
# cur = conn.cursor()
|
||||||
# get current date
|
# get current date
|
||||||
#today = date.today()
|
#today = date.today()
|
||||||
urls = get_subscriptions(conn)
|
urls = await get_subscriptions(conn)
|
||||||
for url in urls:
|
for url in urls:
|
||||||
#"".join(url)
|
#"".join(url)
|
||||||
source = url[0]
|
source = url[0]
|
||||||
|
@ -393,14 +395,14 @@ async def download_updates(conn):
|
||||||
# NOTE Need to correct the SQL statement to do so
|
# NOTE Need to correct the SQL statement to do so
|
||||||
entries = feed.entries
|
entries = feed.entries
|
||||||
length = len(entries)
|
length = len(entries)
|
||||||
remove_entry(conn, source, length)
|
await remove_entry(conn, source, length)
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if entry.has_key("title"):
|
if entry.has_key("title"):
|
||||||
title = entry.title
|
title = entry.title
|
||||||
else:
|
else:
|
||||||
title = feed["feed"]["title"]
|
title = feed["feed"]["title"]
|
||||||
link = source if not entry.link else entry.link
|
link = source if not entry.link else entry.link
|
||||||
exist = check_entry(conn, title, link)
|
exist = await check_entry(conn, title, link)
|
||||||
if not exist:
|
if not exist:
|
||||||
if entry.has_key("summary"):
|
if entry.has_key("summary"):
|
||||||
summary = entry.summary
|
summary = entry.summary
|
||||||
|
@ -412,8 +414,8 @@ async def download_updates(conn):
|
||||||
summary = '*** No summary ***'
|
summary = '*** No summary ***'
|
||||||
#print('~~~~~~summary not in entry')
|
#print('~~~~~~summary not in entry')
|
||||||
entry = (title, summary, link, source, 0);
|
entry = (title, summary, link, source, 0);
|
||||||
add_entry(conn, entry)
|
await add_entry(conn, entry)
|
||||||
set_date(conn, source)
|
await set_date(conn, source)
|
||||||
#make_message
|
#make_message
|
||||||
# message = title + '\n\n' + summary + '\n\nLink: ' + link
|
# message = title + '\n\n' + summary + '\n\nLink: ' + link
|
||||||
# print(message)
|
# print(message)
|
||||||
|
@ -433,7 +435,7 @@ async def download_page(url):
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.run_until_complete
|
loop.run_until_complete
|
||||||
|
|
||||||
def check_feed(conn, url):
|
async def check_feed(conn, url):
|
||||||
"""
|
"""
|
||||||
Check whether a feed exists
|
Check whether a feed exists
|
||||||
Query for feeds by url
|
Query for feeds by url
|
||||||
|
@ -447,7 +449,7 @@ def check_feed(conn, url):
|
||||||
cur.execute(sql, (url,))
|
cur.execute(sql, (url,))
|
||||||
return cur.fetchone()
|
return cur.fetchone()
|
||||||
|
|
||||||
def add_feed(conn, url):
|
async def add_feed(conn, url):
|
||||||
"""
|
"""
|
||||||
Add a new feed into the feeds table
|
Add a new feed into the feeds table
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -457,7 +459,7 @@ def add_feed(conn, url):
|
||||||
#conn = create_connection(db_file)
|
#conn = create_connection(db_file)
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
print(time.strftime("%H:%M:%S"), "conn.cursor() from add_feed(conn, url)")
|
print(time.strftime("%H:%M:%S"), "conn.cursor() from add_feed(conn, url)")
|
||||||
exist = check_feed(conn, url)
|
exist = await check_feed(conn, url)
|
||||||
if not exist:
|
if not exist:
|
||||||
feed = feedparser.parse(url)
|
feed = feedparser.parse(url)
|
||||||
if feed.bozo:
|
if feed.bozo:
|
||||||
|
@ -479,7 +481,7 @@ def add_feed(conn, url):
|
||||||
msg = "News source is already listed in the subscription list"
|
msg = "News source is already listed in the subscription list"
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def remove_feed(conn, id):
|
async def remove_feed(conn, id):
|
||||||
"""
|
"""
|
||||||
Delete a feed by feed id
|
Delete a feed by feed id
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -505,7 +507,7 @@ def remove_feed(conn, id):
|
||||||
return """News source <{}> has been removed from subscriptions list
|
return """News source <{}> has been removed from subscriptions list
|
||||||
""".format(url)
|
""".format(url)
|
||||||
|
|
||||||
def get_unread(conn):
|
async def get_unread(conn):
|
||||||
"""
|
"""
|
||||||
Check read status of entry
|
Check read status of entry
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -544,7 +546,7 @@ def get_unread(conn):
|
||||||
mark_as_read(conn, id)
|
mark_as_read(conn, id)
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
def mark_as_read(conn, id):
|
async def mark_as_read(conn, id):
|
||||||
"""
|
"""
|
||||||
Set read status of entry
|
Set read status of entry
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -559,7 +561,7 @@ def mark_as_read(conn, id):
|
||||||
#conn.close()
|
#conn.close()
|
||||||
|
|
||||||
# TODO test
|
# TODO test
|
||||||
def toggle_status(conn, id):
|
async def toggle_status(conn, id):
|
||||||
"""
|
"""
|
||||||
Set status of feed
|
Set status of feed
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -591,7 +593,7 @@ def toggle_status(conn, id):
|
||||||
print(time.strftime("%H:%M:%S"), "conn.commit() from toggle_status(conn, id)")
|
print(time.strftime("%H:%M:%S"), "conn.commit() from toggle_status(conn, id)")
|
||||||
return notice
|
return notice
|
||||||
|
|
||||||
def toggle_state(jid, state):
|
async def toggle_state(jid, state):
|
||||||
"""
|
"""
|
||||||
Set status of update
|
Set status of update
|
||||||
:param jid: jid of the user
|
:param jid: jid of the user
|
||||||
|
@ -622,7 +624,7 @@ def toggle_state(jid, state):
|
||||||
# if path.exists(db_file):
|
# if path.exists(db_file):
|
||||||
# os.renames(db_file, jid,+".db")
|
# os.renames(db_file, jid,+".db")
|
||||||
|
|
||||||
def set_date(conn, url):
|
async def set_date(conn, url):
|
||||||
"""
|
"""
|
||||||
Set last update date of feed
|
Set last update date of feed
|
||||||
:param url: url of the feed
|
:param url: url of the feed
|
||||||
|
@ -636,7 +638,7 @@ def set_date(conn, url):
|
||||||
conn.commit()
|
conn.commit()
|
||||||
print(time.strftime("%H:%M:%S"), "conn.commit() from set_date(conn, url)")
|
print(time.strftime("%H:%M:%S"), "conn.commit() from set_date(conn, url)")
|
||||||
|
|
||||||
def get_subscriptions(conn):
|
async def get_subscriptions(conn):
|
||||||
"""
|
"""
|
||||||
Query feeds
|
Query feeds
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -648,7 +650,7 @@ def get_subscriptions(conn):
|
||||||
result = cur.execute(sql)
|
result = cur.execute(sql)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def list_subscriptions(conn):
|
async def list_subscriptions(conn):
|
||||||
"""
|
"""
|
||||||
Query feeds
|
Query feeds
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -677,7 +679,7 @@ def list_subscriptions(conn):
|
||||||
"feed add https://reclaimthenet.org/feed/")
|
"feed add https://reclaimthenet.org/feed/")
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def last_entries(conn, num):
|
async def last_entries(conn, num):
|
||||||
"""
|
"""
|
||||||
Query feeds
|
Query feeds
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -699,7 +701,7 @@ 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):
|
async def search_entries(conn, query):
|
||||||
"""
|
"""
|
||||||
Query feeds
|
Query feeds
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -724,7 +726,7 @@ def search_entries(conn, query):
|
||||||
else:
|
else:
|
||||||
return "No results found for: {}".format(query)
|
return "No results found for: {}".format(query)
|
||||||
|
|
||||||
def check_entry(conn, title, link):
|
async def check_entry(conn, title, link):
|
||||||
"""
|
"""
|
||||||
Check whether an entry exists
|
Check whether an entry exists
|
||||||
Query entries by title and link
|
Query entries by title and link
|
||||||
|
@ -739,7 +741,7 @@ def check_entry(conn, title, link):
|
||||||
cur.execute(sql, {"title": title, "link": link})
|
cur.execute(sql, {"title": title, "link": link})
|
||||||
return cur.fetchone()
|
return cur.fetchone()
|
||||||
|
|
||||||
def add_entry(conn, entry):
|
async def add_entry(conn, entry):
|
||||||
"""
|
"""
|
||||||
Add a new entry into the entries table
|
Add a new entry into the entries table
|
||||||
:param conn:
|
:param conn:
|
||||||
|
@ -754,7 +756,7 @@ def add_entry(conn, entry):
|
||||||
conn.commit()
|
conn.commit()
|
||||||
print(time.strftime("%H:%M:%S"), "conn.commit() from add_entry(conn, entry)")
|
print(time.strftime("%H:%M:%S"), "conn.commit() from add_entry(conn, entry)")
|
||||||
|
|
||||||
def remove_entry(conn, source, length):
|
async def remove_entry(conn, source, length):
|
||||||
"""
|
"""
|
||||||
Maintain list of entries
|
Maintain list of entries
|
||||||
Check the number returned by feed and delete
|
Check the number returned by feed and delete
|
||||||
|
|
Loading…
Reference in a new issue