Alialdlfy1 commited on
Commit
22eaabe
·
verified ·
1 Parent(s): 2353abf

Delete modules/blogger/database.py

Browse files
Files changed (1) hide show
  1. modules/blogger/database.py +0 -258
modules/blogger/database.py DELETED
@@ -1,258 +0,0 @@
1
- import json
2
- import os
3
- import time
4
- import threading
5
- import logging
6
-
7
- logger = logging.getLogger(__name__)
8
-
9
- DB_FILE = os.environ.get("BLOGGER_DB_PATH", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "data", "blogger_data.json"))
10
- lock = threading.Lock()
11
-
12
- DEFAULT_BLOGGER_DATA = {
13
- "config": {
14
- "blog_id": "",
15
- "client_id": "",
16
- "client_secret": "",
17
- "refresh_token": "",
18
- "publish_as_draft": False,
19
- "enabled": False,
20
- "default_jobs_image": "",
21
- },
22
- "ai_keys": {},
23
- "channels": {},
24
- "articles": {},
25
- "published_ids": [],
26
- "sections": {},
27
- "logs": [],
28
- "stats": {
29
- "total_published": 0,
30
- "total_failed": 0,
31
- "daily": {},
32
- "monthly": {},
33
- },
34
- }
35
-
36
-
37
- class BloggerDatabase:
38
- def __init__(self):
39
- self._cache = None
40
-
41
- def _ensure_file(self):
42
- os.makedirs(os.path.dirname(DB_FILE), exist_ok=True)
43
- if not os.path.exists(DB_FILE):
44
- with open(DB_FILE, "w", encoding="utf-8") as f:
45
- json.dump(DEFAULT_BLOGGER_DATA, f, ensure_ascii=False, indent=2)
46
-
47
- def _read(self):
48
- self._ensure_file()
49
- try:
50
- with open(DB_FILE, "r", encoding="utf-8") as f:
51
- data = json.load(f)
52
- except Exception:
53
- data = {}
54
- for key, value in DEFAULT_BLOGGER_DATA.items():
55
- if key not in data:
56
- data[key] = value.copy() if isinstance(value, (list, dict)) else value
57
- if not isinstance(data.get("config"), dict):
58
- data["config"] = {}
59
- for k, v in DEFAULT_BLOGGER_DATA["config"].items():
60
- data["config"].setdefault(k, v)
61
- return data
62
-
63
- def _write(self, data):
64
- self._ensure_file()
65
- tmp = DB_FILE + ".tmp"
66
- with open(tmp, "w", encoding="utf-8") as f:
67
- json.dump(data, f, ensure_ascii=False, indent=2)
68
- f.flush()
69
- try:
70
- os.fsync(f.fileno())
71
- except Exception:
72
- pass
73
- os.replace(tmp, DB_FILE)
74
- self._cache = None
75
-
76
- def get_config(self):
77
- with lock:
78
- return dict(self._read().get("config", {}))
79
-
80
- def update_config(self, key, value):
81
- with lock:
82
- data = self._read()
83
- data["config"][key] = value
84
- self._write(data)
85
-
86
- def get_channel(self, channel_id):
87
- with lock:
88
- data = self._read()
89
- return data.get("channels", {}).get(str(channel_id))
90
-
91
- def get_all_channels(self):
92
- with lock:
93
- data = self._read()
94
- return list(data.get("channels", {}).values())
95
-
96
- def save_channel(self, channel_id, channel_data):
97
- with lock:
98
- data = self._read()
99
- data["channels"][str(channel_id)] = channel_data
100
- self._write(data)
101
-
102
- def delete_channel(self, channel_id):
103
- with lock:
104
- data = self._read()
105
- data["channels"].pop(str(channel_id), None)
106
- self._write(data)
107
-
108
- def save_article(self, article_id, article_data):
109
- with lock:
110
- data = self._read()
111
- data["articles"][str(article_id)] = article_data
112
- self._write(data)
113
-
114
- def get_article(self, article_id):
115
- with lock:
116
- data = self._read()
117
- return data.get("articles", {}).get(str(article_id))
118
-
119
- def is_published(self, fingerprint):
120
- with lock:
121
- data = self._read()
122
- return fingerprint in data.get("published_ids", [])
123
-
124
- def mark_published(self, fingerprint):
125
- with lock:
126
- data = self._read()
127
- if fingerprint not in data["published_ids"]:
128
- data["published_ids"].append(fingerprint)
129
- data["stats"]["total_published"] += 1
130
- self._write(data)
131
-
132
- def mark_failed(self):
133
- with lock:
134
- data = self._read()
135
- data["stats"]["total_failed"] += 1
136
- self._write(data)
137
-
138
- def add_log(self, entry):
139
- with lock:
140
- data = self._read()
141
- data["logs"].append(entry)
142
- if len(data["logs"]) > 1000:
143
- data["logs"] = data["logs"][-1000:]
144
- self._write(data)
145
-
146
- def get_logs(self, limit=50):
147
- with lock:
148
- data = self._read()
149
- return list(data["logs"])[-limit:]
150
-
151
- def get_stats(self):
152
- with lock:
153
- data = self._read()
154
- return dict(data.get("stats", {}))
155
-
156
- def get_articles_by_status(self, status):
157
- with lock:
158
- data = self._read()
159
- return [a for a in data.get("articles", {}).values() if a.get("status") == status]
160
-
161
- def update_article_status(self, article_id, status, extra=None):
162
- with lock:
163
- data = self._read()
164
- if str(article_id) in data["articles"]:
165
- data["articles"][str(article_id)]["status"] = status
166
- if extra:
167
- data["articles"][str(article_id)].update(extra)
168
- self._write(data)
169
-
170
- def get_all_sections(self):
171
- with lock:
172
- data = self._read()
173
- return dict(data.get("sections", {}))
174
-
175
- def add_section(self, section_id, section_data):
176
- with lock:
177
- data = self._read()
178
- data["sections"][str(section_id)] = section_data
179
- self._write(data)
180
-
181
- def delete_section(self, section_id):
182
- with lock:
183
- data = self._read()
184
- data["sections"].pop(str(section_id), None)
185
- self._write(data)
186
-
187
- def get_all_ai_keys(self):
188
- with lock:
189
- data = self._read()
190
- return dict(data.get("ai_keys", {}))
191
-
192
- def add_ai_key(self, key_id, key_data):
193
- with lock:
194
- data = self._read()
195
- data["ai_keys"][str(key_id)] = {
196
- "name": key_data.get("name", str(key_id)),
197
- "key": key_data.get("key", ""),
198
- "enabled": key_data.get("enabled", True),
199
- "usage_count": 0,
200
- "error_count": 0,
201
- "last_used": 0,
202
- "added_at": int(time.time()),
203
- }
204
- self._write(data)
205
-
206
- def delete_ai_key(self, key_id):
207
- with lock:
208
- data = self._read()
209
- data["ai_keys"].pop(str(key_id), None)
210
- self._write(data)
211
-
212
- def set_ai_key_enabled(self, key_id, enabled):
213
- with lock:
214
- data = self._read()
215
- if str(key_id) in data["ai_keys"]:
216
- data["ai_keys"][str(key_id)]["enabled"] = enabled
217
- self._write(data)
218
-
219
- def increment_daily_count(self, section):
220
- with lock:
221
- data = self._read()
222
- today = time.strftime("%Y-%m-%d")
223
- if "daily" not in data["stats"]:
224
- data["stats"]["daily"] = {}
225
- if today not in data["stats"]["daily"]:
226
- data["stats"]["daily"][today] = {}
227
- data["stats"]["daily"][today][section] = data["stats"]["daily"][today].get(section, 0) + 1
228
- self._write(data)
229
-
230
- def increment_ai_key_usage(self, key_id):
231
- with lock:
232
- data = self._read()
233
- if str(key_id) in data["ai_keys"]:
234
- data["ai_keys"][str(key_id)]["usage_count"] = data["ai_keys"][str(key_id)].get("usage_count", 0) + 1
235
- data["ai_keys"][str(key_id)]["last_used"] = int(time.time())
236
- self._write(data)
237
-
238
- def increment_ai_key_error(self, key_id):
239
- with lock:
240
- data = self._read()
241
- if str(key_id) in data["ai_keys"]:
242
- data["ai_keys"][str(key_id)]["error_count"] = data["ai_keys"][str(key_id)].get("error_count", 0) + 1
243
- self._write(data)
244
-
245
- SCHEDULE_KEY = "_schedule"
246
-
247
- def get_schedule_state(self) -> dict:
248
- with lock:
249
- data = self._read()
250
- return data.get("stats", {}).get(self.SCHEDULE_KEY, {"day": "", "last_slot": -1})
251
-
252
- def save_schedule_state(self, state: dict):
253
- with lock:
254
- data = self._read()
255
- if "stats" not in data:
256
- data["stats"] = {}
257
- data["stats"][self.SCHEDULE_KEY] = state
258
- self._write(data)