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
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
import logging
from flask import request, jsonify, Blueprint
from services.horoscope_service import horoscope_service
from services.llm_service import llm_service
from services.scheduler_service import scheduler_service
from services.wordpress_service import wordpress_service
from utils.rate_limiter import RateLimiter
from models import db, Horoscope, ConsolidatedHoroscope, ScheduledJob, WordPressExport
from datetime import datetime, date
import json

logger = logging.getLogger(__name__)

# Create Blueprint
horoscope_bp = Blueprint('horoscope', __name__, url_prefix='/api/horoscope')

# API-wide rate limiter (10 requests per minute)
api_rate_limiter = RateLimiter(window_size=60, max_requests=10)


@horoscope_bp.route('/health', methods=['GET'])
def health_check():
    """Health check endpoint for horoscope API"""
    return jsonify({
        "status": "ok",
        "services": {
            "horoscope_scraper": "up",
            "llm": "up" if llm_service.api_key else "down",
            "scheduler": "up" if scheduler_service.running else "down",
            "wordpress": "up" if wordpress_service.is_configured else "down"
        }
    })


@horoscope_bp.route('/scrape', methods=['POST'])
def scrape_horoscope():
    """Scrape horoscope for a specific sign"""
    # Check rate limit
    if not api_rate_limiter.can_proceed():
        return jsonify({
            "error": "Rate limit exceeded",
            "wait_seconds": api_rate_limiter.get_wait_time()
        }), 429
    
    # Record request for rate limiting
    api_rate_limiter.record_request()
    
    # Get parameters from request
    data = request.get_json()
    if not data:
        return jsonify({"error": "Missing request data"}), 400
    
    sign = data.get('sign')
    source = data.get('source')
    date_str = data.get('date')
    
    if not sign:
        return jsonify({"error": "Missing 'sign' parameter"}), 400
    
    if sign.lower() not in horoscope_service.scrapers["astrology.com"].ZODIAC_SIGNS:
        return jsonify({"error": f"Invalid zodiac sign: {sign}"}), 400
    
    # If source is specified, check if it's valid
    if source and source not in horoscope_service.scrapers:
        return jsonify({"error": f"Unknown source: {source}"}), 400
    
    # Scrape from all sources or the specified one
    if source:
        result = horoscope_service.scrape_sign(source, sign, date_str)
    else:
        result = horoscope_service.scrape_sign_from_all_sources(sign, date_str)
    
    return jsonify(result)


@horoscope_bp.route('/scrape-all', methods=['POST'])
def scrape_all_horoscopes():
    """Scrape horoscopes for all signs from all sources"""
    # Check rate limit
    if not api_rate_limiter.can_proceed():
        return jsonify({
            "error": "Rate limit exceeded",
            "wait_seconds": api_rate_limiter.get_wait_time()
        }), 429
    
    # Record request for rate limiting
    api_rate_limiter.record_request()
    
    # Get date from request
    data = request.get_json() or {}
    date_str = data.get('date')
    
    # Scrape all horoscopes
    results = horoscope_service.scrape_all_horoscopes(date_str)
    
    return jsonify({"results": results})


@horoscope_bp.route('/get/<sign>', methods=['GET'])
def get_horoscope(sign):
    """Get horoscope for a specific sign"""
    # Check if sign is valid
    if sign.lower() not in horoscope_service.scrapers["astrology.com"].ZODIAC_SIGNS:
        return jsonify({"error": f"Invalid zodiac sign: {sign}"}), 400
    
    # Get optional parameters
    date_str = request.args.get('date')
    source = request.args.get('source')
    
    # Get horoscope
    result = horoscope_service.get_horoscope(sign, date_str, source)
    
    return jsonify(result)


@horoscope_bp.route('/get-all', methods=['GET'])
def get_all_horoscopes():
    """Get horoscopes for all signs for a specific date"""
    # Get date parameter
    date_str = request.args.get('date')
    
    # Get horoscopes
    result = horoscope_service.get_horoscopes_for_date(date_str)
    
    return jsonify(result)


@horoscope_bp.route('/consolidate/<sign>', methods=['POST'])
def consolidate_horoscope(sign):
    """Consolidate horoscopes for a specific sign using LLM"""
    # Check rate limit
    if not api_rate_limiter.can_proceed():
        return jsonify({
            "error": "Rate limit exceeded",
            "wait_seconds": api_rate_limiter.get_wait_time()
        }), 429
    
    # Record request for rate limiting
    api_rate_limiter.record_request()
    
    # Check if sign is valid
    if sign.lower() not in horoscope_service.scrapers["astrology.com"].ZODIAC_SIGNS:
        return jsonify({"error": f"Invalid zodiac sign: {sign}"}), 400
    
    # Get date from request
    data = request.get_json() or {}
    date_str = data.get('date')
    
    # Parse date
    if date_str:
        try:
            horoscope_date = datetime.strptime(date_str, '%Y-%m-%d').date()
        except ValueError:
            return jsonify({"error": f"Invalid date format: {date_str}. Use YYYY-MM-DD."}), 400
    else:
        horoscope_date = date.today()
    
    # Get horoscopes for the sign and date
    horoscopes = Horoscope.query.filter_by(
        sign=sign.lower(),
        date=horoscope_date
    ).all()
    
    if not horoscopes:
        # Try to scrape if no horoscopes found
        horoscope_service.scrape_sign_from_all_sources(sign, date_str)
        
        # Check again
        horoscopes = Horoscope.query.filter_by(
            sign=sign.lower(),
            date=horoscope_date
        ).all()
        
        if not horoscopes:
            return jsonify({"error": f"No horoscopes found for {sign} on {horoscope_date}"}), 404
    
    # Check if already consolidated
    existing = ConsolidatedHoroscope.query.filter_by(
        sign=sign.lower(),
        date=horoscope_date
    ).first()
    
    if existing:
        return jsonify({
            "message": f"Horoscope for {sign} on {horoscope_date} already consolidated",
            "horoscope": existing.to_dict()
        })
    
    # Convert to format needed by LLM service
    horoscope_data = [h.to_dict() for h in horoscopes]
    
    # Consolidate data using LLM
    consolidated = llm_service.consolidate_horoscopes(horoscope_data)
    
    if not consolidated or "error" in consolidated:
        return jsonify({
            "error": f"Error consolidating horoscopes: {consolidated.get('error', 'Unknown error')}"
        }), 500
    
    # Create new consolidated horoscope
    sources = [h.source for h in horoscopes]
    new_consolidated = ConsolidatedHoroscope()
    new_consolidated.sign = sign.lower()
    new_consolidated.date = horoscope_date
    new_consolidated.consolidated_prediction = consolidated.get("consolidated_prediction", "")
    new_consolidated.sources = json.dumps(sources)
    
    db.session.add(new_consolidated)
    db.session.commit()
    
    return jsonify({
        "message": f"Consolidated horoscope created for {sign} on {horoscope_date}",
        "horoscope": new_consolidated.to_dict()
    })


@horoscope_bp.route('/consolidate-all', methods=['POST'])
def consolidate_all_horoscopes():
    """Consolidate horoscopes for all signs using LLM"""
    # Check rate limit
    if not api_rate_limiter.can_proceed():
        return jsonify({
            "error": "Rate limit exceeded",
            "wait_seconds": api_rate_limiter.get_wait_time()
        }), 429
    
    # Record request for rate limiting
    api_rate_limiter.record_request()
    
    # Get date from request
    data = request.get_json() or {}
    date_str = data.get('date')
    
    # Parse date
    if date_str:
        try:
            horoscope_date = datetime.strptime(date_str, '%Y-%m-%d').date()
        except ValueError:
            return jsonify({"error": f"Invalid date format: {date_str}. Use YYYY-MM-DD."}), 400
    else:
        horoscope_date = date.today()
    
    # Get all zodiac signs
    signs = horoscope_service.scrapers["astrology.com"].ZODIAC_SIGNS
    
    results = {}
    for sign in signs:
        # Get horoscopes for the sign and date
        horoscopes = Horoscope.query.filter_by(
            sign=sign,
            date=horoscope_date
        ).all()
        
        if not horoscopes:
            results[sign] = {"status": "skipped", "message": "No horoscopes found"}
            continue
        
        # Check if already consolidated
        existing = ConsolidatedHoroscope.query.filter_by(
            sign=sign,
            date=horoscope_date
        ).first()
        
        if existing:
            results[sign] = {"status": "exists", "message": "Already consolidated"}
            continue
        
        # Convert to format needed by LLM service
        horoscope_data = [h.to_dict() for h in horoscopes]
        
        # Consolidate data using LLM
        consolidated = llm_service.consolidate_horoscopes(horoscope_data)
        
        if not consolidated or "error" in consolidated:
            results[sign] = {
                "status": "error",
                "message": f"Error: {consolidated.get('error', 'Unknown error')}"
            }
            continue
        
        # Create new consolidated horoscope
        sources = [h.source for h in horoscopes]
        new_consolidated = ConsolidatedHoroscope()
        new_consolidated.sign = sign
        new_consolidated.date = horoscope_date
        new_consolidated.consolidated_prediction = consolidated.get("consolidated_prediction", "")
        new_consolidated.sources = json.dumps(sources)
        
        db.session.add(new_consolidated)
        results[sign] = {"status": "success", "message": "Consolidated successfully"}
    
    db.session.commit()
    
    return jsonify({
        "message": f"Consolidated horoscopes for {horoscope_date}",
        "results": results
    })


@horoscope_bp.route('/publish/<int:horoscope_id>', methods=['POST'])
def publish_to_wordpress(horoscope_id):
    """Publish a consolidated horoscope to WordPress"""
    # Check rate limit
    if not api_rate_limiter.can_proceed():
        return jsonify({
            "error": "Rate limit exceeded",
            "wait_seconds": api_rate_limiter.get_wait_time()
        }), 429
    
    # Record request for rate limiting
    api_rate_limiter.record_request()
    
    # Check if WordPress is configured
    if not wordpress_service.is_configured:
        return jsonify({"error": "WordPress API not configured"}), 500
    
    # Get the consolidated horoscope
    horoscope = ConsolidatedHoroscope.query.get(horoscope_id)
    if not horoscope:
        return jsonify({"error": f"Horoscope with ID {horoscope_id} not found"}), 404
    
    # Check if already published
    existing_export = WordPressExport.query.filter_by(horoscope_id=horoscope_id).first()
    if existing_export:
        return jsonify({
            "message": f"Horoscope already published to WordPress",
            "export": existing_export.to_dict()
        })
    
    # Publish to WordPress
    result = wordpress_service.publish_horoscope(horoscope)
    
    if not result or not result.get("success", False):
        return jsonify({
            "error": f"Error publishing to WordPress: {result.get('error', 'Unknown error')}"
        }), 500
    
    # Create export record
    export = WordPressExport()
    export.horoscope_id = horoscope_id
    export.wordpress_post_id = result.get("post_id")
    export.wordpress_url = result.get("url")
    export.status = "published"
    
    db.session.add(export)
    db.session.commit()
    
    return jsonify({
        "message": f"Published horoscope to WordPress",
        "export": export.to_dict()
    })


@horoscope_bp.route('/schedule', methods=['GET'])
def get_schedules():
    """Get list of scheduled jobs"""
    jobs = scheduler_service.get_all_jobs()
    return jsonify({"jobs": jobs})


@horoscope_bp.route('/schedule', methods=['POST'])
def add_schedule():
    """Add a new scheduled job"""
    # Get parameters from request
    data = request.get_json()
    if not data:
        return jsonify({"error": "Missing request data"}), 400
    
    name = data.get('name')
    frequency = data.get('frequency')
    
    if not name or not frequency:
        return jsonify({"error": "Missing 'name' or 'frequency' parameter"}), 400
    
    # Add job
    success = scheduler_service.add_job(name, frequency)
    
    if success:
        return jsonify({"message": f"Added job '{name}' with frequency '{frequency}'"})
    else:
        return jsonify({"error": f"Failed to add job '{name}'"}), 500


@horoscope_bp.route('/schedule/<name>', methods=['DELETE'])
def remove_schedule(name):
    """Remove a scheduled job"""
    # Remove job
    success = scheduler_service.remove_job(name)
    
    if success:
        return jsonify({"message": f"Removed job '{name}'"})
    else:
        return jsonify({"error": f"Failed to remove job '{name}'"}), 500


@horoscope_bp.route('/wordpress/test', methods=['GET'])
def test_wordpress():
    """Test WordPress connection"""
    result = wordpress_service.test_connection()
    
    if result.get("success", False):
        return jsonify(result)
    else:
        return jsonify(result), 500


# Register LLM method for horoscope consolidation
def consolidate_horoscopes(horoscope_data):
    """Consolidate multiple horoscope predictions using LLM"""
    if not horoscope_data:
        return {"error": "No horoscope data provided"}
    
    try:
        # Prepare data for LLM
        sign = horoscope_data[0].get("sign", "unknown")
        date_str = horoscope_data[0].get("date", "unknown date")
        
        sources_text = ""
        for i, data in enumerate(horoscope_data, 1):
            source = data.get("source", "Unknown Source")
            prediction = data.get("prediction", "No prediction available")
            
            sources_text += f"SOURCE {i} ({source}):\n"
            sources_text += f"Prediction: {prediction}\n\n"
        
        # Create prompt for consolidation
        prompt = f"""
        Please analyze and consolidate these daily horoscope predictions for {sign.upper()} for {date_str}.
        
        {sources_text}
        
        Create a single, coherent daily horoscope prediction that synthesizes the information from all sources.
        Focus on the common themes and advice while maintaining the mystical and guiding tone typical of horoscopes.
        The response should be 2-3 paragraphs long and should NOT mention the sources or that it's a consolidation.
        
        Respond with JSON in this format:
        {{
          "consolidated_prediction": "The consolidated horoscope text..."
        }}
        """
        
        # Call OpenAI API
        response = llm_service.client.chat.completions.create(
            model=llm_service.model_name,
            messages=[
                {"role": "system", "content": "You are an expert astrologer specializing in synthesizing horoscope predictions."},
                {"role": "user", "content": prompt}
            ],
            response_format={"type": "json_object"},
            temperature=0.7
        )
        
        # Parse the response
        result = json.loads(response.choices[0].message.content)
        return result
        
    except Exception as e:
        logger.error(f"Error consolidating horoscopes with LLM: {str(e)}")
        return {"error": f"Failed to consolidate horoscopes: {str(e)}"}

# Add custom LLM method to llm_service
llm_service.consolidate_horoscopes = consolidate_horoscopes