-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1465 lines (1212 loc) · 52.1 KB
/
app.py
File metadata and controls
1465 lines (1212 loc) · 52.1 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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Flask-based Microblog with Bluesky & Mastodon Auto-Posting
A web interface for managing and posting to social media with:
- Blog-like archive of posts with pagination and search
- Post creation with optional images, links, and commentary
- Automatic posting to Bluesky and Mastodon
- Scheduled hourly posting from queue
"""
import os
import sys
import requests
import threading
import time
import sqlite3
import feedparser
import threading
from datetime import datetime
from urllib.parse import urljoin, urlparse
from io import BytesIO
from PIL import Image
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, session
from werkzeug.utils import secure_filename
from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
from atproto import Client, models
from mastodon import Mastodon
from bs4 import BeautifulSoup
# Configuration
TOPOST_FILE = 'topost.txt'
POSTED_FILE = 'posted.txt'
IMAGES_FOLDER = 'images'
UPLOAD_FOLDER = 'static/uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
DATABASE = 'microblog.db'
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
post_lock = threading.Lock()
last_auto_post_time = time.time()
# Flask app setup
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'change-this-secret-key-in-production')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
# Ensure directories exist
os.makedirs(IMAGES_FOLDER, exist_ok=True)
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Database functions
def init_db():
"""Initialize the database with required tables"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
# Settings table
c.execute('''CREATE TABLE IF NOT EXISTS settings
(key TEXT PRIMARY KEY, value TEXT)''')
# Users table
c.execute('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
# RSS feeds table - UPDATED with auto_queue, auto_post_mode, and last_checked
c.execute('''CREATE TABLE IF NOT EXISTS rss_feeds
(id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT UNIQUE NOT NULL,
name TEXT,
auto_queue INTEGER DEFAULT 0,
auto_post_mode TEXT DEFAULT 'queue',
last_checked TIMESTAMP,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
# Migration: Add auto_post_mode column if it doesn't exist
try:
c.execute("SELECT auto_post_mode FROM rss_feeds LIMIT 1")
except sqlite3.OperationalError:
c.execute("ALTER TABLE rss_feeds ADD COLUMN auto_post_mode TEXT DEFAULT 'queue'")
# RSS entries tracking table - NEW
c.execute('''CREATE TABLE IF NOT EXISTS rss_seen_entries
(id INTEGER PRIMARY KEY AUTOINCREMENT,
feed_id INTEGER NOT NULL,
entry_link TEXT NOT NULL,
seen_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(feed_id, entry_link),
FOREIGN KEY(feed_id) REFERENCES rss_feeds(id) ON DELETE CASCADE)''')
conn.commit()
conn.close()
def get_setting(key, default=None):
"""Get a setting from the database"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT value FROM settings WHERE key = ?', (key,))
result = c.fetchone()
conn.close()
return result[0] if result else default
def set_setting(key, value):
"""Set a setting in the database"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)', (key, value))
conn.commit()
conn.close()
def get_user(username):
"""Get a user from the database"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT id, username, password_hash FROM users WHERE username = ?', (username,))
result = c.fetchone()
conn.close()
return {'id': result[0], 'username': result[1], 'password_hash': result[2]} if result else None
def create_user(username, password):
"""Create a new user"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
password_hash = generate_password_hash(password)
try:
c.execute('INSERT INTO users (username, password_hash) VALUES (?, ?)', (username, password_hash))
conn.commit()
conn.close()
return True
except sqlite3.IntegrityError:
conn.close()
return False
def user_exists():
"""Check if any user exists in the database"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT COUNT(*) FROM users')
count = c.fetchone()[0]
conn.close()
return count > 0
def is_duplicate_link(url):
"""Check if a URL already exists in posted.txt or topost.txt"""
if not url:
return False
# Normalize URL (remove trailing slashes, convert to lowercase for comparison)
normalized_url = url.rstrip('/').lower()
# Check posted.txt
try:
with open(POSTED_FILE, 'r', encoding='utf-8') as f:
for line in f:
parsed = parse_posted_line(line)
if parsed and parsed.get('url'):
existing_url = parsed['url'].rstrip('/').lower()
if existing_url == normalized_url:
return True
except FileNotFoundError:
pass
# Check topost.txt
try:
with open(TOPOST_FILE, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
# Parse the content to extract URL
parsed = parse_content(line)
if parsed['type'] == 'url':
existing_url = parsed['url'].rstrip('/').lower()
if existing_url == normalized_url:
return True
except FileNotFoundError:
pass
return False
def get_link_status(url):
if not url:
return None
normalized_url = url.rstrip('/').lower()
try:
with open(POSTED_FILE, 'r', encoding='utf-8') as f:
for line in f:
parsed = parse_posted_line(line)
if parsed and parsed.get('url'):
if parsed['url'].rstrip('/').lower() == normalized_url:
return 'posted'
except FileNotFoundError:
pass
try:
with open(TOPOST_FILE, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
parsed = parse_content(line)
if parsed['type'] == 'url':
if parsed['url'].rstrip('/').lower() == normalized_url:
return 'queued'
except FileNotFoundError:
pass
return None
# RSS feed functions
def add_rss_feed(url, name=None, auto_queue=False, auto_post_mode='queue'):
"""Add an RSS feed to the database"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
try:
c.execute('INSERT INTO rss_feeds (url, name, auto_queue, auto_post_mode) VALUES (?, ?, ?, ?)',
(url, name, 1 if auto_queue else 0, auto_post_mode))
conn.commit()
conn.close()
return True
except sqlite3.IntegrityError:
conn.close()
return False
def get_rss_feeds():
"""Get all RSS feeds from the database"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT id, url, name, auto_queue, auto_post_mode FROM rss_feeds ORDER BY added_at DESC')
feeds = [{'id': row[0], 'url': row[1], 'name': row[2], 'auto_queue': bool(row[3]),
'auto_post_mode': row[4] if len(row) > 4 and row[4] else 'queue'}
for row in c.fetchall()]
conn.close()
return feeds
def update_rss_feed_auto_queue(feed_id, auto_queue):
"""Update the auto_queue setting for a feed"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('UPDATE rss_feeds SET auto_queue = ? WHERE id = ?',
(1 if auto_queue else 0, feed_id))
conn.commit()
conn.close()
def update_rss_feed_auto_post_mode(feed_id, auto_post_mode):
"""Update the auto_post_mode for a feed (queue, social, or local)"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('UPDATE rss_feeds SET auto_post_mode = ? WHERE id = ?',
(auto_post_mode, feed_id))
conn.commit()
conn.close()
def update_rss_feed_last_checked(feed_id):
"""Update the last_checked timestamp for a feed"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('UPDATE rss_feeds SET last_checked = CURRENT_TIMESTAMP WHERE id = ?', (feed_id,))
conn.commit()
conn.close()
def mark_rss_entry_seen(feed_id, entry_link):
"""Mark an RSS entry as seen"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
try:
c.execute('INSERT INTO rss_seen_entries (feed_id, entry_link) VALUES (?, ?)',
(feed_id, entry_link))
conn.commit()
conn.close()
return True
except sqlite3.IntegrityError:
# Already seen
conn.close()
return False
def is_rss_entry_seen(feed_id, entry_link):
"""Check if an RSS entry has been seen"""
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
c.execute('SELECT COUNT(*) FROM rss_seen_entries WHERE feed_id = ? AND entry_link = ?',
(feed_id, entry_link))
count = c.fetchone()[0]
conn.close()
return count > 0
def fetch_rss_entries(feed_url, limit=15):
try:
feed = feedparser.parse(feed_url)
if feed.bozo and not feed.entries:
return None, "Failed to parse RSS feed"
entries = []
for entry in feed.entries[:limit]:
entries.append({
'title': entry.get('title', 'No title'),
'link': entry.get('link', ''),
'published': entry.get('published', ''),
'summary': entry.get('summary', ''),
'author': entry.get('author', '')
})
return entries, None
except Exception as e:
return None, str(e)
def check_and_queue_new_rss_entries():
"""Check all auto-queue feeds for new entries and add them based on auto_post_mode"""
global last_auto_post_time
feeds = get_rss_feeds()
auto_queue_feeds = [f for f in feeds if f['auto_queue']]
for feed in auto_queue_feeds:
try:
entries, error = fetch_rss_entries(feed['url'], limit=10)
if error or not entries:
print(f"Error checking RSS feed {feed['name'] or feed['url']}: {error}")
continue
new_entries_count = 0
auto_post_mode = feed.get('auto_post_mode', 'queue')
for entry in entries:
if not entry.get('link'):
continue
# Check if we've seen this entry before
if not is_rss_entry_seen(feed['id'], entry['link']):
content = f"{entry['link']}|{entry['title']}"
if auto_post_mode == 'social':
# Post directly to social media
if post_to_social_media(content):
add_to_posted(content)
print(f"Auto-posted to socials from {feed['name'] or feed['url']}: {entry['title']}")
last_auto_post_time = time.time()
else:
print(f"Failed to auto-post to socials: {entry['title']}")
elif auto_post_mode == 'local':
# Add directly to posted (local only)
add_to_posted(content)
print(f"Auto-posted locally from {feed['name'] or feed['url']}: {entry['title']}")
else:
# Default: add to queue
with open(TOPOST_FILE, 'a', encoding='utf-8') as f:
f.write(f"{content}\n")
print(f"Auto-queued from {feed['name'] or feed['url']}: {entry['title']}")
# Mark as seen
mark_rss_entry_seen(feed['id'], entry['link'])
new_entries_count += 1
if new_entries_count > 0:
action = {'queue': 'queued', 'social': 'posted to socials', 'local': 'posted locally'}[auto_post_mode]
print(f"Auto-{action} {new_entries_count} new entries from {feed['name'] or feed['url']}")
# Update last checked time
update_rss_feed_last_checked(feed['id'])
except Exception as e:
print(f"Error processing RSS feed {feed['name'] or feed['url']}: {e}")
# Login decorator
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'user_id' not in session:
flash('Please log in to access this page.', 'error')
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# Initialize database on startup
init_db()
# Add custom Jinja2 filter for parsing content
@app.template_filter('parse_content')
def parse_content_filter(content):
return parse_content(content)
# Context processor to make site settings available to all templates
@app.context_processor
def inject_site_settings():
"""Make site_name and social_links available to all templates"""
return {
'site_name': get_setting('site_name', 'Microblog'),
'social_links': get_setting('social_links', '')
}
# Global variable to track last auto-post time
last_auto_post_time = time.time()
# Utility functions
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def parse_posted_line(line):
"""Parse a line from posted.txt - new pipe-delimited format only"""
if not line.strip():
return None
# New format: [DATETIME]|url|headline|imageFilename|summary|commentary
if not line.startswith('['):
return None
bracket_end = line.find(']')
if bracket_end == -1:
return None
timestamp_str = line[1:bracket_end]
try:
timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S')
except:
return None
# Check for pipe-delimited format
rest = line[bracket_end+1:].strip()
if not rest.startswith('|'):
return None
rest = rest[1:] # Remove leading pipe
parts = rest.split('|')
if len(parts) < 5:
return None
return {
'timestamp': timestamp,
'url': parts[0].strip() if parts[0].strip() and parts[0].strip() != 'NULL' else None,
'headline': parts[1].strip() if parts[1].strip() and parts[1].strip() != 'NULL' else None,
'image': parts[2].strip() if parts[2].strip() and parts[2].strip() != 'NULL' else None,
'summary': parts[3].strip() if parts[3].strip() and parts[3].strip() != 'NULL' else None,
'commentary': parts[4].strip() if parts[4].strip() and parts[4].strip() != 'NULL' else None,
'raw': line
}
def parse_content(content):
"""Parse content to extract URL, image, and text
FIXED: Now uses elif to prevent double parsing of image URLs
Priority order:
1. Image files (local files with image extensions)
2. URLs (http/https links, including image URLs)
3. Text (everything else)
"""
if '|' not in content:
return {'type': 'text', 'text': content}
parts = content.split('|', 1)
first_part = parts[0].strip()
second_part = parts[1].strip()
# Check if it's a local image file FIRST (no http/https prefix)
# This ensures local image files are properly identified
image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp', '.tiff')
if not first_part.startswith(('http://', 'https://', 'www.')) and \
any(first_part.lower().endswith(ext) for ext in image_extensions):
return {'type': 'image', 'image': first_part, 'text': second_part}
# Check if it's a URL (this includes image URLs from the web)
elif first_part.startswith(('http://', 'https://', 'www.')):
return {'type': 'url', 'url': first_part, 'text': second_part}
# Otherwise it's just text
else:
return {'type': 'text', 'text': content}
def get_posted_entries(page=1, per_page=20, search_query=None):
"""Get paginated posted entries with optional search"""
try:
with open(POSTED_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Parse all lines
entries = []
for line in reversed(lines):
if line.strip():
parsed = parse_posted_line(line)
if parsed: # Only add if parsing succeeded
entries.append(parsed)
# Filter by search query if provided
if search_query:
filtered = []
for e in entries:
search_text = ' '.join(filter(None, [
e.get('headline', ''),
e.get('summary', ''),
e.get('commentary', ''),
e.get('content', '')
])).lower()
if search_query.lower() in search_text:
filtered.append(e)
entries = filtered
# Paginate
total = len(entries)
start = (page - 1) * per_page
end = start + per_page
return {
'entries': entries[start:end],
'total': total,
'page': page,
'per_page': per_page,
'total_pages': (total + per_page - 1) // per_page if total > 0 else 0
}
except FileNotFoundError:
return {'entries': [], 'total': 0, 'page': 1, 'per_page': per_page, 'total_pages': 0}
def get_all_posted_entries():
"""Get all posted entries without pagination"""
try:
with open(POSTED_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Parse all lines (reversed so newest first)
entries = []
for line in reversed(lines):
if line.strip():
parsed = parse_posted_line(line)
if parsed: # Only add if parsing succeeded
entries.append(parsed)
return entries
except FileNotFoundError:
return []
def get_queue_entries():
"""Get entries waiting in topost.txt"""
try:
with open(TOPOST_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
return [line.strip() for line in lines if line.strip()]
except FileNotFoundError:
return []
# Social media posting functions (from original script)
def fetch_page_metadata(url):
"""Fetch page title, description, and featured image from URL"""
try:
headers = {'User-Agent': USER_AGENT}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
title = None
title_tag = soup.find('title')
if title_tag:
title = title_tag.get_text().strip()
og_title = soup.find('meta', property='og:title')
if og_title and og_title.get('content'):
title = og_title.get('content').strip()
description = None
og_desc = soup.find('meta', property='og:description')
if og_desc and og_desc.get('content'):
description = og_desc.get('content').strip()
else:
meta_desc = soup.find('meta', attrs={'name': 'description'})
if meta_desc and meta_desc.get('content'):
description = meta_desc.get('content').strip()
image_url = None
og_image = soup.find('meta', property='og:image')
if og_image and og_image.get('content'):
image_url = og_image.get('content').strip()
image_url = urljoin(url, image_url)
if not image_url:
twitter_image = soup.find('meta', attrs={'name': 'twitter:image'})
if twitter_image and twitter_image.get('content'):
image_url = twitter_image.get('content').strip()
image_url = urljoin(url, image_url)
return {
'title': title or url,
'description': description or '',
'image_url': image_url
}
except Exception as e:
print(f"Error fetching metadata for {url}: {e}")
return {
'title': url,
'description': '',
'image_url': None
}
def load_local_image(filename):
"""Load and process a local image file"""
try:
image_path = os.path.join(IMAGES_FOLDER, filename)
if not os.path.exists(image_path):
print(f"Error: Image file not found: {image_path}")
return None
with open(image_path, 'rb') as f:
image_data = f.read()
img = Image.open(BytesIO(image_data))
if img.mode in ('RGBA', 'LA', 'P'):
img = img.convert('RGB')
max_size = (1200, 1200)
if img.size[0] > max_size[0] or img.size[1] > max_size[1]:
img.thumbnail(max_size, Image.Resampling.LANCZOS)
img_bytes = BytesIO()
img.save(img_bytes, format='JPEG', quality=85)
img_bytes.seek(0)
return img_bytes.getvalue()
except Exception as e:
print(f"Error loading local image {filename}: {e}")
return None
def download_and_process_image(image_url):
"""Download and process image"""
try:
headers = {'User-Agent': USER_AGENT}
response = requests.get(image_url, headers=headers, timeout=15)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
if img.mode in ('RGBA', 'LA', 'P'):
img = img.convert('RGB')
max_size = (1200, 1200)
if img.size[0] > max_size[0] or img.size[1] > max_size[1]:
img.thumbnail(max_size, Image.Resampling.LANCZOS)
img_bytes = BytesIO()
img.save(img_bytes, format='JPEG', quality=85)
img_bytes.seek(0)
return img_bytes.getvalue()
except Exception as e:
print(f"Error processing image {image_url}: {e}")
return None
def upload_image_to_bluesky(client, image_data):
"""Upload image to Bluesky"""
try:
blob = client.upload_blob(image_data)
return blob
except Exception as e:
print(f"Error uploading image to Bluesky: {e}")
return None
def create_bluesky_image_post(client, image_data, caption):
"""Create a Bluesky post with an image"""
try:
print("Uploading image to Bluesky...")
blob = upload_image_to_bluesky(client, image_data)
if not blob:
print("Failed to upload image to Bluesky")
return None
image_embed = models.AppBskyEmbedImages.Image(
alt="",
image=blob.blob
)
embed = models.AppBskyEmbedImages.Main(images=[image_embed])
response = client.send_post(text=caption, embed=embed)
print("✓ Image uploaded to Bluesky successfully")
return response
except Exception as e:
print(f"Error creating Bluesky image post: {e}")
return None
def create_simple_bluesky_post(client, text):
"""Create a simple text-only Bluesky post"""
try:
response = client.send_post(text=text)
return response
except Exception as e:
print(f"Error creating simple Bluesky post: {e}")
return None
def create_mastodon_image_post(mastodon_client, image_data, caption):
"""Create a Mastodon post with an image"""
try:
print("Uploading image to Mastodon...")
media_dict = mastodon_client.media_post(image_data, mime_type='image/jpeg')
media_id = media_dict['id']
response = mastodon_client.status_post(caption, media_ids=[media_id])
print("✓ Image uploaded to Mastodon successfully")
return response
except Exception as e:
print(f"Error creating Mastodon image post: {e}")
return None
def create_bluesky_post_with_embed(client, url, comment, metadata):
"""Create a Bluesky post with embedded link card"""
try:
external_embed = models.AppBskyEmbedExternal.External(
uri=url,
title=metadata['title'][:300],
description=metadata['description'][:1000] if metadata['description'] else ''
)
if metadata['image_url']:
print(f"Downloading featured image: {metadata['image_url']}")
image_data = download_and_process_image(metadata['image_url'])
if image_data:
print("Uploading image to Bluesky...")
blob = upload_image_to_bluesky(client, image_data)
if blob:
external_embed.thumb = blob.blob
print("✓ Image uploaded successfully")
else:
print("⚠️ Failed to upload image, posting without it")
else:
print("⚠️ Failed to download image, posting without it")
embed = models.AppBskyEmbedExternal.Main(external=external_embed)
response = client.send_post(text=comment, embed=embed)
return response
except Exception as e:
print(f"Error creating post with embed: {e}")
return None
def create_mastodon_post(mastodon_client, text, url=None, image_data=None):
"""Create a Mastodon post with optional image and URL"""
try:
media_id = None
if image_data:
print("Uploading image to Mastodon...")
media_dict = mastodon_client.media_post(image_data, mime_type='image/jpeg')
media_id = media_dict['id']
print("✓ Image uploaded to Mastodon successfully")
if url:
post_text = f"{text}\n\n{url}"
else:
post_text = text
if media_id:
response = mastodon_client.status_post(post_text, media_ids=[media_id])
else:
response = mastodon_client.status_post(post_text)
return response
except Exception as e:
print(f"Error creating Mastodon post: {e}")
return None
def create_simple_mastodon_post(mastodon_client, text):
"""Create a simple text-only Mastodon post"""
try:
response = mastodon_client.status_post(text)
return response
except Exception as e:
print(f"Error creating simple Mastodon post: {e}")
return None
def post_to_social_media(content):
"""Post content to Bluesky and Mastodon"""
try:
# Get credentials from database
bluesky_handle = get_setting('bluesky_handle')
bluesky_password = get_setting('bluesky_password')
mastodon_url = get_setting('mastodon_url')
mastodon_token = get_setting('mastodon_token')
if not all([bluesky_handle, bluesky_password, mastodon_url, mastodon_token]):
print("Error: Social media credentials not configured")
return False
parsed = parse_content(content)
bluesky_client = Client()
bluesky_client.login(bluesky_handle, bluesky_password)
mastodon_client = Mastodon(
access_token=mastodon_token,
api_base_url=mastodon_url
)
image_data = None
metadata = None
if parsed['type'] == 'url':
url = parsed['url']
text_content = parsed['text']
metadata = fetch_page_metadata(url)
# Download image for thumbnail
if metadata['image_url']:
image_data = download_and_process_image(metadata['image_url'])
# Post to Bluesky with embed
external_embed = models.AppBskyEmbedExternal.External(
uri=url,
title=metadata['title'][:300],
description=metadata['description'][:1000] if metadata['description'] else ''
)
# Add thumbnail if available
if image_data:
blob = upload_image_to_bluesky(bluesky_client, image_data)
if blob:
external_embed.thumb = blob.blob
embed = models.AppBskyEmbedExternal.Main(external=external_embed)
bluesky_client.send_post(text=text_content, embed=embed)
# Post to Mastodon
post_text = f"{text_content}\n\n{url}"
if image_data:
media_dict = mastodon_client.media_post(image_data, mime_type='image/jpeg')
mastodon_client.status_post(post_text, media_ids=[media_dict['id']])
else:
mastodon_client.status_post(post_text)
elif parsed['type'] == 'image':
image_filename = parsed['image']
text_content = parsed['text']
image_data = load_local_image(image_filename)
if image_data:
# Post to Bluesky
blob = upload_image_to_bluesky(bluesky_client, image_data)
if blob:
image_embed = models.AppBskyEmbedImages.Image(alt="", image=blob.blob)
embed = models.AppBskyEmbedImages.Main(images=[image_embed])
bluesky_client.send_post(text=text_content, embed=embed)
# Post to Mastodon
media_dict = mastodon_client.media_post(image_data, mime_type='image/jpeg')
mastodon_client.status_post(text_content, media_ids=[media_dict['id']])
else:
# Text only
text_content = parsed['text']
bluesky_client.send_post(text=text_content)
mastodon_client.status_post(text_content)
return True
except Exception as e:
print(f"Error posting to social media: {e}")
import traceback
traceback.print_exc()
return False
def add_to_posted(content):
"""Add entry to posted.txt with timestamp and metadata"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Parse the content
parsed = parse_content(content)
# Initialize metadata fields
url = 'NULL'
headline = 'NULL'
image_filename = 'NULL'
summary = 'NULL'
commentary = 'NULL'
if parsed['type'] == 'url':
url = parsed['url']
commentary = parsed['text'].replace('|', '-') if parsed['text'] else 'NULL'
# Fetch metadata
try:
metadata = fetch_page_metadata(url)
headline = metadata.get('title', 'NULL').replace('|', '-') # Remove pipes to avoid conflicts
# Get description/summary
description = metadata.get('description', '')
if description:
summary = description[:200].replace('|', '-')
if len(description) > 200:
summary += '...'
# Download and save image
image_url = metadata.get('image_url')
if image_url:
try:
import hashlib
# Create unique filename from URL hash
url_hash = hashlib.md5(url.encode()).hexdigest()[:12]
image_filename_local = f"link_{url_hash}.jpg"
image_path = os.path.join(IMAGES_FOLDER, image_filename_local)
# Download image
headers = {'User-Agent': USER_AGENT}
img_response = requests.get(image_url, headers=headers, timeout=10)
img_response.raise_for_status()
# Process and crop image to 300x200
img = Image.open(BytesIO(img_response.content))
if img.mode in ('RGBA', 'LA', 'P'):
img = img.convert('RGB')
# Calculate crop to 3:2 ratio (300x200)
target_ratio = 600 / 400 # 1.5
img_ratio = img.width / img.height
if img_ratio > target_ratio:
# Image is wider, crop width
new_width = int(img.height * target_ratio)
left = (img.width - new_width) // 2
img = img.crop((left, 0, left + new_width, img.height))
else:
# Image is taller, crop height
new_height = int(img.width / target_ratio)
top = (img.height - new_height) // 2
img = img.crop((0, top, img.width, top + new_height))
# Resize to exactly 300x200
img = img.resize((600, 400), Image.Resampling.LANCZOS)
# Save
img.save(image_path, format='JPEG', quality=85)
image_filename = image_filename_local
print(f"✓ Saved link image: {image_filename}")
except Exception as e:
print(f"Error downloading/processing link image: {e}")
except Exception as e:
print(f"Error fetching metadata: {e}")
elif parsed['type'] == 'image':
image_filename = parsed['image']
commentary = parsed['text'].replace('|', '-') if parsed['text'] else 'NULL'
else:
# Text only
commentary = parsed['text'].replace('|', '-') if parsed['text'] else 'NULL'
# Build the pipe-delimited line
# Format: [DATETIME]|url|headline|imageFilename|summary|commentary
line = f"[{timestamp}]|{url}|{headline}|{image_filename}|{summary}|{commentary}\n"
with open(POSTED_FILE, 'a', encoding='utf-8') as f:
f.write(line)
# Scheduled posting thread
def auto_poster_thread():
"""Background thread that posts from queue hourly"""
global last_auto_post_time
while True:
time.sleep(60)
with post_lock: # Acquire lock before checking/posting
if time.time() - last_auto_post_time >= 3600:
try:
with open(TOPOST_FILE, 'r', encoding='utf-8') as f:
lines = f.readlines()
if lines:
line_to_post = lines[0].strip()
if not line_to_post:
remaining_lines = lines[1:]
with open(TOPOST_FILE, 'w', encoding='utf-8') as f:
f.writelines(remaining_lines)
continue
remaining_lines = lines[1:]
if post_to_social_media(line_to_post):
# Remove from queue FIRST
with open(TOPOST_FILE, 'w', encoding='utf-8') as f:
f.writelines(remaining_lines)
# Then add to posted
add_to_posted(line_to_post)
print(f"Auto-posted: {line_to_post}")
last_auto_post_time = time.time()
else:
print(f"Failed to auto-post: {line_to_post}")
except FileNotFoundError:
pass
except Exception as e:
print(f"Error in auto-poster: {e}")