-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
592 lines (488 loc) · 18.3 KB
/
Copy pathsync.py
File metadata and controls
592 lines (488 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# language: python
import warnings
# suppress pydantic's UnsupportedFieldAttributeWarning (and similar UserWarning from pydantic)
warnings.filterwarnings("ignore", message=".*UnsupportedFieldAttributeWarning.*")
warnings.filterwarnings("ignore", category=UserWarning, module=r"pydantic\..*")
from curses import echo
from email.mime import image
import os
import sqlite3
# from tkinter import W
import requests
import asyncio
from telegram import Bot, InputFile
from telegram.error import TelegramError
from io import BytesIO
import yaml
from datetime import datetime, timezone
import langid
from atproto import Client, models
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Get script directory
yaml_path = os.path.join(BASE_DIR, "keys.yaml") # Absolute path to keys.yaml
db_path = os.path.join(BASE_DIR, "microblog.db") # Absolute path to DB
images_dir = os.path.join(BASE_DIR, "images") # Absolute path to images directory
with open(yaml_path, "r") as file:
keys = yaml.safe_load(file)
# Telegram Bot Credentials
TELEGRAM_BOT_TOKEN = keys["telegram"]["bot_token"]
TELEGRAM_CHANNEL_ID = keys["telegram"]["channel_id"]
# WordPress Credentials
WORDPRESS_URLEN = keys["wordpress"]["urlen"]
WORDPRESS_URLFA = keys["wordpress"]["urlfa"]
WP_USERNAME = keys["wordpress"]["username"]
WP_PASSWORD = keys["wordpress"]["password"]
# Bluesky Credentials
if "bluesky" in keys:
bluesky_username = keys["bluesky"]["handle"]
bluesky_password = keys["bluesky"]["password"]
else:
bluesky_username = None
bluesky_password = None
# Initialize Telegram Bot
bot = Bot(token=TELEGRAM_BOT_TOKEN)
async def verify_bot_token():
try:
bot_info = await bot.get_me()
# print("Bot Info:", bot_info)
except TelegramError as e:
print("Failed to verify bot token:", e)
# Define the download_telegram_image function
async def download_telegram_image(file_id):
# print("Downloading image:", file_id)
try:
os.makedirs(images_dir, exist_ok=True)
image_path = os.path.join(images_dir, f"{file_id}.jpg")
if os.path.exists(image_path):
# print("Image already exists:", image_path)
return image_path
file = await bot.get_file(file_id)
file_path = file.file_path
response = requests.get(file_path)
if response.status_code == 200:
with open(image_path, "wb") as f:
f.write(response.content)
return image_path
else:
print(f"Failed to download image: {response.status_code}")
return None
except Exception as e:
print(f"Error downloading image: {e}")
return None
# Function to load image data
def load_image_data(image_path):
try:
with open(image_path, "rb") as img_file:
img_data = img_file.read()
return img_data
except Exception as e:
print(f"Error loading image data: {e}")
return None
def post_message_bluesky(
username, password, title, message, bluesky_uri, img_data=None
):
text_language = langid.classify(title)[0]
if text_language == "en":
langs = ["en", "en-AU"]
elif text_language == "fa":
langs = ["fa", "fa-IR"]
else: # Default to English
langs = ["en", "en-AU"]
# Ensure the message does not exceed 300 graphemes
message = message[:297] + "..." # Truncate message to 300 characters
title = title[:120] + "..." # Truncate title to 300 characters
client = Client()
client.login(username, password)
embed = models.AppBskyEmbedExternal.Main(
external=models.AppBskyEmbedExternal.External(
title=title,
description=message,
uri=bluesky_uri,
)
)
if img_data:
thumb = client.upload_blob(img_data)
embed.external.thumb = thumb.blob
post = client.send_post(
f"{title}",
embed=embed,
langs=langs,
)
return post.cid
# Initialize Database
def init_db():
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS posts (
message_id INTEGER PRIMARY KEY,
text TEXT,
text_language TEXT,
image_url TEXT,
wp_post_id INTEGER,
wp_media_id INTEGER,
created_at TEXT,
updated_at TEXT,
deleted INTEGER DEFAULT 0
)
"""
)
conn.commit()
conn.close()
print("Database initialized successfully.")
# Fetch Messages from Telegram Channel
async def fetch_channel_messages():
try:
updates = await bot.get_updates()
if not updates: # No new messages
print("No new messages feteched to process.")
return
for update in updates:
# print("Raw Update:", update) # Print raw data received from Telegram
if (
update.message and update.message.chat.id == TELEGRAM_CHANNEL_ID
): # Check if the message is from the correct channel
print("Message")
await process_message(update.message)
elif (
update.channel_post
and update.channel_post.chat.id == TELEGRAM_CHANNEL_ID
): # Check if the channel post is from the correct channel
print("Channael Post")
await process_message(update.channel_post)
elif (
update.edited_message
and update.edited_message.chat.id == TELEGRAM_CHANNEL_ID
): # Check if the edited message is from the correct channel
print("Edited Message")
await process_edited_message(update.edited_message)
elif (
update.edited_channel_post
and update.edited_channel_post.chat.id == TELEGRAM_CHANNEL_ID
): # Check if the edited channel post is from the correct channel
print("Edited Channel Post")
await process_edited_message(update.edited_channel_post)
else:
print("Unknown message type:", update, "\n\n\n")
except TelegramError as e:
print("Failed to fetch messages:", e)
# Process Telegram Message
async def process_message(message):
text = message.caption or message.text or ""
image_url = None
if message.photo:
file_id = new_func(message)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"SELECT image_url FROM posts WHERE image_url LIKE ?", (f"%{file_id}%",)
)
result = cursor.fetchone()
conn.close()
if result:
# If the image is already stored in the database, use the existing URL
print("Image already stored in the database:", result[0])
image_url = result[0]
else:
# If the image is not stored in the database, download it and store the URL
image_url = await download_telegram_image(file_id)
await store_message(message.message_id, text, image_url)
def new_func(message):
file_id = message.photo[-1].file_id
return file_id
# Process Edited Telegram Message
async def process_edited_message(message):
text = message.caption or message.text or ""
image_url = None
if message.photo:
file_id = message.photo[-1].file_id
image_url = await download_telegram_image(file_id)
await update_message(message.message_id, text, image_url)
# Store Message in SQLite
async def store_message(message_id, text, image_url):
text_language = langid.classify(text)[0]
if text_language == "en":
WORDPRESS_URL = WORDPRESS_URLEN
elif text_language == "fa":
WORDPRESS_URL = WORDPRESS_URLFA
else: # Default to English
WORDPRESS_URL = WORDPRESS_URLEN
if WORDPRESS_URL == "none":
print("WORDPRESS_URL is set to 'none', skipping WordPress operations.")
return
print(WORDPRESS_URL)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
created_at = datetime.now(timezone.utc).isoformat()
updated_at = created_at
cursor.execute(
"""
INSERT INTO posts (message_id, text, text_language, image_url, created_at, updated_at) VALUES (?, ? ,?, ?, ?, ?)
ON CONFLICT(message_id) DO UPDATE SET text=?, image_url=?, updated_at=?
""",
(
message_id,
text,
text_language,
image_url,
created_at,
updated_at,
text,
image_url,
updated_at,
),
)
conn.commit()
cursor.execute(
"SELECT wp_post_id, text_language FROM posts WHERE message_id=?", (message_id,)
)
wp_post_id, text_language = cursor.fetchone()
if text_language == "en":
WORDPRESS_URL = WORDPRESS_URLEN
elif text_language == "fa":
WORDPRESS_URL = WORDPRESS_URLFA
else: # Default to English
WORDPRESS_URL = WORDPRESS_URLEN
conn.close()
if wp_post_id and isinstance(wp_post_id, int):
await update_wordpress_post(
WORDPRESS_URL, message_id, wp_post_id, text, text, image_url
)
else:
await publish_to_wordpress(WORDPRESS_URL, message_id, text, text, image_url)
# Update Message in SQLite and WordPress
async def update_message(message_id, text, image_url):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
updated_at = datetime.now(timezone.utc).isoformat()
cursor.execute(
"""
UPDATE posts SET text=?, image_url=?, updated_at=? WHERE message_id=?
""",
(text, image_url, updated_at, message_id),
)
conn.commit()
cursor.execute(
"SELECT wp_post_id, text_language FROM posts WHERE message_id=?", (message_id,)
)
result = cursor.fetchone()
conn.close()
if result:
wp_post_id, text_language = result
if text_language == "en":
WORDPRESS_URL = WORDPRESS_URLEN
elif text_language == "fa":
WORDPRESS_URL = WORDPRESS_URLFA
else: # Default to English
WORDPRESS_URL = WORDPRESS_URLEN
if WORDPRESS_URL == "none":
print("WORDPRESS_URL is set to 'none', skipping WordPress operations.")
return
if wp_post_id and isinstance(wp_post_id, int):
await update_wordpress_post(
WORDPRESS_URL, message_id, wp_post_id, text, text, image_url
)
else:
print(f"No WordPress post ID found for message ID {message_id}")
else:
print(f"No record found in the database for message ID {message_id}")
# Publish Post to WordPress
async def publish_to_wordpress(WORDPRESS_URL, message_id, title, content, image_url):
if WORDPRESS_URL == "none":
print("WORDPRESS_URL is set to 'none', skipping WordPress operations.")
return
print("Publishing to WordPress:", message_id) # Debugging information
auth = (WP_USERNAME, WP_PASSWORD)
media_id = None
if image_url:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"SELECT wp_media_id FROM posts WHERE message_id=?", (message_id,)
)
result = cursor.fetchone()
if result and result[0]:
media_id = result[0]
else:
media_id = await upload_image_to_wordpress(WORDPRESS_URL, image_url)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"""
UPDATE posts SET wp_media_id=? WHERE message_id=?
""",
(media_id, message_id),
)
conn.commit()
conn.close()
post_data = {
"title": title,
"content": content,
"status": "publish",
"featured_media": media_id if media_id else None,
}
# print("Publishing to WordPress:", post_data) # Debugging information
response = requests.post(
f"{WORDPRESS_URL}/wp-json/wp/v2/posts", json=post_data, auth=auth
)
print("Response:", response) # Debugging information
if response.status_code == 201:
wp_post_id = response.json()["id"]
print(f"WordPress post {wp_post_id} created successfully.")
await update_wp_post_id(message_id, wp_post_id)
# Post to Bluesky
if bluesky_username and bluesky_password:
image_data = load_image_data(image_url)
bluesky_uri = f"{WORDPRESS_URL}/?p={wp_post_id}"
bluesky_message_id = post_message_bluesky(
bluesky_username,
bluesky_password,
title,
content,
bluesky_uri,
image_data,
)
print(f"Bluesky post {bluesky_message_id} created successfully.")
else:
print(f"Failed to publish to WordPress: {response.status_code} {response.text}")
# Update WordPress Post
async def update_wordpress_post(
WORDPRESS_URL, message_id, wp_post_id, title, content, image_url
):
if WORDPRESS_URL == "none":
print("WORDPRESS_URL is set to 'none', skipping WordPress operations.")
return
auth = (WP_USERNAME, WP_PASSWORD)
media_id = None
if image_url:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"SELECT wp_media_id FROM posts WHERE message_id=?", (message_id,)
)
result = cursor.fetchone()
if result and result[0]:
media_id = result[0]
else:
media_id = await upload_image_to_wordpress(image_url)
cursor.execute(
"""
UPDATE posts SET wp_media_id=? WHERE message_id=?
""",
(media_id, message_id),
)
conn.commit()
conn.close()
post_data = {
"title": title,
"content": content,
"status": "publish",
"featured_media": media_id if media_id else None,
}
# print("Updating WordPress Post:", wp_post_id, post_data) # Debugging information
response = requests.post(
f"{WORDPRESS_URL}/wp-json/wp/v2/posts/{wp_post_id}", json=post_data, auth=auth
)
if response.status_code == 200:
print(f"WordPress post {wp_post_id} updated successfully.")
elif response.status_code == 404:
print(f"WordPress post {wp_post_id} not found, creating a new post.")
await publish_to_wordpress(WORDPRESS_URL, message_id, title, content, image_url)
async def upload_image_to_wordpress(WORDPRESS_URL, image_path):
if WORDPRESS_URL == "none":
print("WORDPRESS_URL is set to 'none', skipping WordPress operations.")
return
auth = (WP_USERNAME, WP_PASSWORD)
wpmwdiaurl = f"{WORDPRESS_URL}/wp-json/wp/v2/media"
with open(image_path, "rb") as img_file:
files = {"file": img_file}
headers = {
"Authorization": "Basic <base64-encoded-credentials>",
"Content-Type": "image/jpeg",
}
try:
response = requests.post(
wpmwdiaurl,
files=files,
auth=auth,
headers={
"Content-Disposition": f'attachment; filename="{os.path.basename(image_path)}"'
},
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
wp_media_id = response.json()["id"]
return wp_media_id
except requests.exceptions.RequestException as e:
print(f"Error uploading image: {e}")
if response.status_code != 201:
print(f"Response status code: {response.status_code} {response.text}")
return None
# Update WordPress Post ID in SQLite
async def update_wp_post_id(message_id, wp_post_id):
print(f"Updating WordPress Post ID for message ID {message_id} to {wp_post_id}")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"""
UPDATE posts SET wp_post_id=? WHERE message_id=?
""",
(wp_post_id, message_id),
)
conn.commit()
conn.close()
# Check for Deleted Messages in Telegram Channel and Delete from WordPress
# This function is not working as expected. It needs to be fixed.
async def check_deleted_messages():
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("SELECT message_id, wp_post_id FROM posts WHERE deleted=0")
stored_messages = cursor.fetchall()
print("Stored Messages ID:", stored_messages)
updates = await bot.get_updates()
current_message_ids = set()
for update in updates:
if update.channel_post and update.channel_post.chat.id == TELEGRAM_CHANNEL_ID:
current_message_ids.add(update.channel_post.message_id)
print("Current Message IDs:", current_message_ids)
deleted_message_ids = {row[0] for row in stored_messages} - current_message_ids
print("Deleted Message IDs:", deleted_message_ids)
for message_id in deleted_message_ids:
print(f"Message {message_id} has been deleted.")
cursor.execute("UPDATE posts SET deleted=1 WHERE message_id=?", (message_id,))
conn.commit()
cursor.execute("SELECT wp_post_id FROM posts WHERE message_id=?", (message_id,))
wp_post_id = cursor.fetchone()
if wp_post_id:
await delete_wordpress_post(WORDPRESS_URL, wp_post_id[0])
conn.close()
# Delete WordPress Post
async def delete_wordpress_post(WORDPRESS_URL, wp_post_id):
if WORDPRESS_URL == "none":
print("WORDPRESS_URL is set to 'none', skipping WordPress operations.")
return
auth = (WP_USERNAME, WP_PASSWORD)
response = requests.delete(
f"{WORDPRESS_URL}/wp-json/wp/v2/posts/{wp_post_id}", auth=auth
)
print("Deleting WordPress Post:", wp_post_id) # Debugging information
if response.status_code == 200:
print(f"WordPress post {wp_post_id} deleted successfully.")
# Sync Process
async def sync():
print("Starting sync process...")
await fetch_channel_messages()
# disabled the check_deleted_messages function until it is fixed
# await check_deleted_messages()
print("Sync process completed.")
async def run_main():
# Call the verification function
await verify_bot_token()
# Run the sync process
await sync()
def main():
# Initialize Database
init_db()
# Run the main async function
asyncio.run(run_main())
if __name__ == "__main__":
main()