Sitemap generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.3 KiB

4 years ago
  1. import logging
  2. import sqlite3
  3. class SQLiteTodo(object):
  4. def __init__(self, db_name):
  5. self.connection = sqlite3.connect(db_name)
  6. self.__init_tables()
  7. def __init_tables(self):
  8. cursor = self.connection.cursor()
  9. cursor.execute("DROP TABLE IF EXISTS todo_queue;")
  10. cursor.execute("""
  11. CREATE TABLE todo_queue (
  12. url text(1000) primary key
  13. );
  14. """)
  15. self.connection.commit()
  16. cursor.close()
  17. def add(self, url):
  18. cursor = self.connection.cursor()
  19. try:
  20. cursor.execute("""insert into todo_queue values (?);""", (url,))
  21. except Exception as e:
  22. logging.info(e)
  23. finally:
  24. self.connection.commit()
  25. cursor.close()
  26. def remove(self, url):
  27. cursor = self.connection.cursor()
  28. try:
  29. cursor.execute("""delete from todo_queue where url = ?;""", (url,))
  30. except Exception as e:
  31. logging.info(e)
  32. finally:
  33. self.connection.commit()
  34. cursor.close()
  35. def __contains__(self, item):
  36. cursor = self.connection.cursor()
  37. result = False
  38. try:
  39. cursor.execute("""select 1 from todo_queue where url = ?""", (item, ))
  40. row = cursor.fetchone()
  41. if len(row):
  42. result = True
  43. except Exception as e:
  44. logging.info(e)
  45. finally:
  46. cursor.close()
  47. return result
  48. def __iter__(self):
  49. cursor = self.connection.cursor()
  50. result = []
  51. try:
  52. cursor.execute("""select url from todo_queue""")
  53. rows = cursor.fetchall()
  54. result = [row[0] for row in rows]
  55. except Exception as e:
  56. logging.info(e)
  57. finally:
  58. cursor.close()
  59. return iter(result)
  60. def __next__(self):
  61. for url in self:
  62. yield url
  63. def __len__(self):
  64. cursor = self.connection.cursor()
  65. result = []
  66. try:
  67. cursor.execute("""select count(*) as cnt from todo_queue""")
  68. row = cursor.fetchone()
  69. result = row[0]
  70. except Exception as e:
  71. logging.info(e)
  72. finally:
  73. cursor.close()
  74. return result