utils_create_rss_record.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import time
  2. import psycopg2
  3. from psycopg2 import Error
  4. class CreateRssRecord(object):
  5. def __init__(self):
  6. self.hostname = 'erhe.top'
  7. self.port = 20788
  8. self.database = 'freshrss'
  9. self.user = 'freshrss'
  10. self.password = 'freshrss'
  11. self.conn = None
  12. def connect(self):
  13. """连接到 PostgreSQL 数据库"""
  14. try:
  15. self.conn = psycopg2.connect(
  16. dbname=self.database,
  17. user=self.user,
  18. password=self.password,
  19. host=self.hostname,
  20. port=self.port
  21. )
  22. except Error as e:
  23. print(f"Error connecting to the database: {e}")
  24. else:
  25. print("Connected to the database successfully.")
  26. if not self.conn:
  27. raise Exception("Database connection failed")
  28. def check_and_insert(self, data):
  29. """检查 URL 是否存在,如果不存在则插入整个数据字典"""
  30. try:
  31. with self.conn.cursor() as cursor:
  32. # 检查 URL 是否存在
  33. select_sql = "SELECT COUNT(*) FROM freshrsstoor_feed WHERE url = %s"
  34. cursor.execute(select_sql, (data['url'],))
  35. result = cursor.fetchone()
  36. if result[0] == 0:
  37. # URL 不存在,插入新记录
  38. columns = ', '.join(data.keys())
  39. values = ', '.join(['%s'] * len(data))
  40. insert_sql = """INSERT INTO freshrsstoor_feed
  41. (url, kind, category, "name", website, description, "lastUpdate", priority, "pathEntries", "httpAuth", "error", "ttl", attributes, "cache_nbEntries", "cache_nbUnreads")
  42. VALUES ('{}', {}, {}, '{}', '{}', '{}', {}, {}, '{}', '{}', {}, {}, '{}', {}, {});""".format(
  43. data['url'],
  44. data['kind'],
  45. data['category'],
  46. data['name'],
  47. data['website'],
  48. data['description'],
  49. data['lastUpdate'],
  50. data['priority'],
  51. data['pathEntries'],
  52. data['httpAuth'],
  53. data['error'],
  54. data['ttl'],
  55. data['attributes'],
  56. data['cache_nbEntries'],
  57. data['cache_nbUnreads']
  58. )
  59. cursor.execute(insert_sql, tuple(data.values()))
  60. self.conn.commit()
  61. print("Data inserted successfully.")
  62. else:
  63. print("URL already exists.")
  64. except Error as e:
  65. print(f"Error: {e}")
  66. finally:
  67. if self.conn is not None:
  68. self.conn.close()
  69. # 使用示例
  70. if __name__ == "__main__":
  71. cr = CreateRssRecord()
  72. cr.connect()
  73. insert_data = {
  74. 'url': 'https://rsshub.app/jike/topic/556688fae4b00c57d9dd46ee',
  75. 'category': 7,
  76. 'name': '今日份的摄影 - 即刻圈子',
  77. 'website': 'http://finance.sina.com.cn/china/',
  78. 'description': '爱摄影的即友都在这里~分享原创摄影作品,感受照片背后的共鸣吧! - Powered by RSSHub',
  79. 'kind': 0,
  80. 'lastUpdate': int(time.time()),
  81. 'priority': 10,
  82. 'pathEntries': '',
  83. 'httpAuth': '',
  84. 'error': 0,
  85. 'ttl': 0,
  86. 'attributes': '{"curl_params":null,"ssl_verify":null,"timeout":null}',
  87. 'cache_nbEntries': 0,
  88. 'cache_nbUnreads': 0
  89. }
  90. cr.check_and_insert(insert_data)