antennaDB.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. # vim: tabstop=4 shiftwidth=4 expandtab
  3. import sqlite3
  4. import os
  5. from multiFeedParsing import FeedEntry,TwtxtEntry
  6. # There's a lot of opening and closing going on here,
  7. # because several processes will be sharing one sqlite3
  8. # db, which is just a single file. We want to hog it
  9. # as little as possible to minimize the risk of
  10. # collisions. Some errors are tolerable; this is a
  11. # good enough effort.
  12. class AntennaDB():
  13. def __init__(self, dbPath="antenna.sqlite"):
  14. self.dbPath = dbPath
  15. def createDB(self):
  16. connection = sqlite3.connect(self.dbPath)
  17. cursor = connection.cursor()
  18. cursor.execute("CREATE TABLE IF NOT EXISTS entries (feedurl text, author text, updated datetime, title text, link text primary key)")
  19. cursor.execute("CREATE TABLE IF NOT EXISTS twtxt (feedurl text, author text, posted datetime, twt text)")
  20. connection.close()
  21. def getEntries(self):
  22. connection = sqlite3.connect(self.dbPath)
  23. cursor = connection.cursor()
  24. cursor.execute("SELECT feedurl, author, updated, title, link FROM entries ORDER BY updated DESC")
  25. results = []
  26. for result in cursor.fetchall():
  27. results.append(FeedEntry(feedurl = result[0], author = result[1], updated = result[2], title = result[3], link = result[4]))
  28. connection.close()
  29. return results
  30. def getTwts(self):
  31. connection = sqlite3.connect(self.dbPath)
  32. cursor = connection.cursor()
  33. cursor.execute("SELECT feedurl, author, posted, twt FROM twtxt ORDER BY posted DESC")
  34. results = []
  35. for result in cursor.fetchall():
  36. results.append(TwtxtEntry(feedurl = result[0], author = result[1], posted = result[2], twt = result[3]))
  37. connection.close()
  38. return results
  39. def deleteFeeds(self, urls):
  40. urlTuples = []
  41. for url in urls:
  42. urlTuples.append((url,))
  43. connection = sqlite3.connect(self.dbPath)
  44. cursor = connection.cursor()
  45. cursor.executemany("DELETE FROM entries WHERE feedurl LIKE ?", urlTuples)
  46. cursor.executemany("DELETE FROM twtxt WHERE feedurl LIKE ?", urlTuples)
  47. connection.commit()
  48. connection.close()
  49. # UPSERTs entries into the DB, if they're not too old. Returns how many entries were upserted.
  50. def insertFeedEntries(self, entries, limit=0):
  51. entries = [e for e in entries if e.updated > limit]
  52. entrytuples = []
  53. for entry in entries:
  54. entrytuples.append((entry.feedurl, entry.author, entry.updated, entry.title, entry.link))
  55. connection = sqlite3.connect(self.dbPath)
  56. cursor = connection.cursor()
  57. cursor.executemany("INSERT INTO entries (feedurl, author, updated, title, link) VALUES (?,?,?,?,?) ON CONFLICT (link) DO UPDATE SET author = excluded.author, updated = excluded.updated, title = excluded.title", entrytuples)
  58. connection.commit()
  59. connection.close()
  60. return len(entries)
  61. def insertTwtxtEntries(self, entries, limit=0):
  62. entries = [e for e in entries if e.posted > limit]
  63. entrytuples = []
  64. for entry in entries:
  65. entrytuples.append((entry.feedurl, entry.author, entry.posted, entry.twt))
  66. connection = sqlite3.connect(self.dbPath)
  67. cursor = connection.cursor()
  68. cursor.executemany("INSERT INTO twtxt (feedurl, author, posted, twt) VALUES (?,?,?,?)", entrytuples)
  69. connection.commit()
  70. connection.close()
  71. return len(entries)
  72. def pruneDB(self, limit):
  73. connection = sqlite3.connect(self.dbPath)
  74. cursor = connection.cursor()
  75. cursor.execute("DELETE FROM entries WHERE updated < ?", (limit,))
  76. cursor.execute("DELETE FROM twtxt WHERE posted < ?", (limit,))
  77. connection.commit()
  78. connection.close()