|
|
from flask import Blueprint, render_template, request, jsonify, current_app
|
|
|
from flask_login import login_required, current_user
|
|
|
from app.services.google_scraper import GoogleAdsScraper
|
|
|
from app.models.google_ad import GoogleAd
|
|
|
from app.services.ai_processor import AIPipeline
|
|
|
from app import db, celery
|
|
|
import logging
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
google_ads_bp = Blueprint('google_ads', __name__, url_prefix='/google-ads')
|
|
|
|
|
|
@google_ads_bp.route('/', methods=['GET'])
|
|
|
@login_required
|
|
|
def index():
|
|
|
"""Google Ads dashboard page."""
|
|
|
return render_template('google_ads/index.html')
|
|
|
|
|
|
@google_ads_bp.route('/search', methods=['GET', 'POST'])
|
|
|
@login_required
|
|
|
def search_ads():
|
|
|
"""Search for Google Ads."""
|
|
|
if request.method == 'POST':
|
|
|
search_query = request.form.get('query')
|
|
|
num_pages = int(request.form.get('num_pages', 3))
|
|
|
|
|
|
|
|
|
task = scrape_google_search_ads.delay(search_query, num_pages, current_user.id)
|
|
|
|
|
|
return jsonify({
|
|
|
'status': 'success',
|
|
|
'message': 'Google Ads scraping started',
|
|
|
'task_id': task.id
|
|
|
})
|
|
|
|
|
|
|
|
|
return render_template('google_ads/search.html')
|
|
|
|
|
|
@google_ads_bp.route('/display', methods=['GET', 'POST'])
|
|
|
@login_required
|
|
|
def display_ads():
|
|
|
"""Scrape display ads from a URL."""
|
|
|
if request.method == 'POST':
|
|
|
target_url = request.form.get('url')
|
|
|
scroll_count = int(request.form.get('scroll_count', 5))
|
|
|
|
|
|
|
|
|
task = scrape_google_display_ads.delay(target_url, scroll_count, current_user.id)
|
|
|
|
|
|
return jsonify({
|
|
|
'status': 'success',
|
|
|
'message': 'Google Display Ads scraping started',
|
|
|
'task_id': task.id
|
|
|
})
|
|
|
|
|
|
|
|
|
return render_template('google_ads/display.html')
|
|
|
|
|
|
@google_ads_bp.route('/results', methods=['GET'])
|
|
|
@login_required
|
|
|
def view_results():
|
|
|
"""View Google Ads results."""
|
|
|
ad_type = request.args.get('type', 'all')
|
|
|
query = request.args.get('query', '')
|
|
|
|
|
|
|
|
|
ads_query = GoogleAd.query
|
|
|
|
|
|
if ad_type != 'all':
|
|
|
ads_query = ads_query.filter(GoogleAd.ad_type == ad_type)
|
|
|
|
|
|
if query:
|
|
|
ads_query = ads_query.filter(
|
|
|
(GoogleAd.title.ilike(f'%{query}%')) |
|
|
|
(GoogleAd.description.ilike(f'%{query}%')) |
|
|
|
(GoogleAd.search_query.ilike(f'%{query}%'))
|
|
|
)
|
|
|
|
|
|
|
|
|
ads = ads_query.order_by(GoogleAd.created_at.desc()).all()
|
|
|
|
|
|
return render_template('google_ads/results.html', ads=ads, ad_type=ad_type, query=query)
|
|
|
|
|
|
@google_ads_bp.route('/api/ads', methods=['GET'])
|
|
|
@login_required
|
|
|
def api_get_ads():
|
|
|
"""API endpoint to get Google Ads data."""
|
|
|
ad_type = request.args.get('type', 'all')
|
|
|
query = request.args.get('query', '')
|
|
|
limit = int(request.args.get('limit', 50))
|
|
|
|
|
|
|
|
|
ads_query = GoogleAd.query
|
|
|
|
|
|
if ad_type != 'all':
|
|
|
ads_query = ads_query.filter(GoogleAd.ad_type == ad_type)
|
|
|
|
|
|
if query:
|
|
|
ads_query = ads_query.filter(
|
|
|
(GoogleAd.title.ilike(f'%{query}%')) |
|
|
|
(GoogleAd.description.ilike(f'%{query}%')) |
|
|
|
(GoogleAd.search_query.ilike(f'%{query}%'))
|
|
|
)
|
|
|
|
|
|
|
|
|
ads = ads_query.order_by(GoogleAd.created_at.desc()).limit(limit).all()
|
|
|
|
|
|
|
|
|
result = []
|
|
|
for ad in ads:
|
|
|
ad_data = {
|
|
|
'id': ad.id,
|
|
|
'ad_type': ad.ad_type,
|
|
|
'title': ad.title,
|
|
|
'description': ad.description,
|
|
|
'display_url': ad.display_url,
|
|
|
'target_url': ad.target_url,
|
|
|
'image_url': ad.image_url,
|
|
|
'position': ad.position,
|
|
|
'search_query': ad.search_query,
|
|
|
'page_url': ad.page_url,
|
|
|
'sentiment': ad.sentiment,
|
|
|
'created_at': ad.created_at.isoformat() if ad.created_at else None
|
|
|
}
|
|
|
result.append(ad_data)
|
|
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
@celery.task
|
|
|
def scrape_google_search_ads(search_query, num_pages, user_id):
|
|
|
"""Celery task to scrape Google search ads."""
|
|
|
try:
|
|
|
scraper = GoogleAdsScraper()
|
|
|
ads_data = scraper.scrape_search_ads(search_query, num_pages)
|
|
|
|
|
|
|
|
|
ai_pipeline = AIPipeline()
|
|
|
|
|
|
for ad_data in ads_data:
|
|
|
|
|
|
ad = GoogleAd.from_search_ad_data(ad_data, search_query, user_id)
|
|
|
|
|
|
|
|
|
if ad.title or ad.description:
|
|
|
try:
|
|
|
|
|
|
ad_content = type('obj', (object,), {
|
|
|
'content': f"{ad.title} {ad.description}"
|
|
|
})
|
|
|
|
|
|
|
|
|
ai_results = ai_pipeline.process_ad(ad_content)
|
|
|
ad.sentiment = ai_results.get('sentiment')
|
|
|
except Exception as e:
|
|
|
logger.error(f"Error processing ad with AI: {e}")
|
|
|
|
|
|
|
|
|
db.session.add(ad)
|
|
|
|
|
|
db.session.commit()
|
|
|
return {'status': 'success', 'count': len(ads_data)}
|
|
|
|
|
|
except Exception as e:
|
|
|
logger.error(f"Error in Google search ads scraping task: {e}")
|
|
|
db.session.rollback()
|
|
|
return {'status': 'error', 'message': str(e)}
|
|
|
|
|
|
@celery.task
|
|
|
def scrape_google_display_ads(target_url, scroll_count, user_id):
|
|
|
"""Celery task to scrape Google display ads."""
|
|
|
try:
|
|
|
scraper = GoogleAdsScraper()
|
|
|
ads_data = scraper.scrape_display_ads(target_url, scroll_count)
|
|
|
|
|
|
|
|
|
for ad_data in ads_data:
|
|
|
|
|
|
ad = GoogleAd.from_display_ad_data(ad_data, user_id)
|
|
|
|
|
|
|
|
|
db.session.add(ad)
|
|
|
|
|
|
db.session.commit()
|
|
|
return {'status': 'success', 'count': len(ads_data)}
|
|
|
|
|
|
except Exception as e:
|
|
|
logger.error(f"Error in Google display ads scraping task: {e}")
|
|
|
db.session.rollback()
|
|
|
return {'status': 'error', 'message': str(e)} |