diff --git a/scripts/social_media_audit.py b/scripts/social_media_audit.py index 0ede7f0..b459152 100644 --- a/scripts/social_media_audit.py +++ b/scripts/social_media_audit.py @@ -33,6 +33,20 @@ from datetime import datetime, timedelta from typing import Optional, Dict, List, Tuple, Any from urllib.parse import urlparse import time +from pathlib import Path + +# Load .env file from project root +try: + from dotenv import load_dotenv + # Find .env file relative to this script + script_dir = Path(__file__).resolve().parent + project_root = script_dir.parent + env_path = project_root / '.env' + if env_path.exists(): + load_dotenv(env_path) + logging.info(f"Loaded .env from {env_path}") +except ImportError: + pass # python-dotenv not installed, rely on system environment import requests from bs4 import BeautifulSoup @@ -882,6 +896,18 @@ class SocialMediaAuditor: return [dict(row._mapping) for row in result] + def get_company_id_by_slug(self, slug: str) -> Optional[int]: + """Get company ID by slug.""" + with self.Session() as session: + query = text(""" + SELECT id FROM companies WHERE slug = :slug + """) + result = session.execute(query, {'slug': slug}) + row = result.fetchone() + if row: + return row[0] + return None + def audit_company(self, company: Dict) -> Dict[str, Any]: """ Perform full audit for a single company. @@ -1111,6 +1137,7 @@ class SocialMediaAuditor: def main(): parser = argparse.ArgumentParser(description='Social Media & Website Audit') parser.add_argument('--company-id', type=int, help='Audit single company by ID') + parser.add_argument('--company-slug', type=str, help='Audit single company by slug') parser.add_argument('--batch', type=str, help='Audit batch of companies (e.g., 1-10)') parser.add_argument('--all', action='store_true', help='Audit all companies') parser.add_argument('--dry-run', action='store_true', help='Print results without saving') @@ -1125,6 +1152,14 @@ def main(): if args.company_id: summary = auditor.run_audit(company_ids=[args.company_id], dry_run=args.dry_run) + elif args.company_slug: + # Look up company ID by slug + company_id = auditor.get_company_id_by_slug(args.company_slug) + if company_id: + summary = auditor.run_audit(company_ids=[company_id], dry_run=args.dry_run) + else: + print(f"Error: Company with slug '{args.company_slug}' not found") + sys.exit(1) elif args.batch: start, end = map(int, args.batch.split('-')) summary = auditor.run_audit(batch_start=start, batch_end=end, dry_run=args.dry_run) diff --git a/scripts/verify_google_places.py b/scripts/verify_google_places.py new file mode 100644 index 0000000..fd8c066 --- /dev/null +++ b/scripts/verify_google_places.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 +""" +Verify Google Places API integration for social_media_audit.py. + +This script tests the GooglePlacesSearcher class directly without +requiring database access. +""" +import os +import sys +from pathlib import Path + +# Add user site-packages to path +sys.path.insert(0, '/Users/maciejpi/Library/Python/3.9/lib/python/site-packages') + +# Load .env file from project root +try: + from dotenv import load_dotenv + script_dir = Path(__file__).resolve().parent + project_root = script_dir.parent + env_path = project_root / '.env' + if env_path.exists(): + load_dotenv(env_path) + print(f"Loaded .env from {env_path}") +except ImportError: + print("python-dotenv not installed, using system environment") + +# Import GooglePlacesSearcher from social_media_audit +from social_media_audit import GooglePlacesSearcher, BraveSearcher + +def main(): + api_key = os.getenv('GOOGLE_PLACES_API_KEY') + print(f"\nGoogle Places API Key: {'*' * 10 + api_key[-8:] if api_key else 'NOT SET'}") + + if not api_key or api_key == 'your_places_api_key_here': + print("ERROR: GOOGLE_PLACES_API_KEY not configured in .env") + sys.exit(1) + + # Test GooglePlacesSearcher directly + print("\n" + "=" * 60) + print("Testing GooglePlacesSearcher for INPI company") + print("=" * 60) + + searcher = GooglePlacesSearcher(api_key=api_key) + + # Step 1: Find place + print("\n1. Finding place for 'INPI' in Wejherowo...") + place_id = searcher.find_place('INPI', 'Wejherowo') + + if not place_id: + print(" ERROR: Could not find INPI on Google Places") + print(" This could mean:") + print(" - The company doesn't have a Google Business Profile") + print(" - The API key doesn't have Places API enabled") + print(" - The search query needs adjustment") + sys.exit(1) + + print(f" Found place_id: {place_id}") + + # Step 2: Get place details + print("\n2. Getting place details...") + details = searcher.get_place_details(place_id) + + print(f"\n Results:") + print(f" - google_rating: {details.get('google_rating')}") + print(f" - google_reviews_count: {details.get('google_reviews_count')}") + print(f" - business_status: {details.get('business_status')}") + print(f" - opening_hours: {'Yes' if details.get('opening_hours') else 'Not available'}") + + if details.get('opening_hours'): + hours = details.get('opening_hours', {}) + if hours.get('weekday_text'): + print(" - Hours:") + for day in hours['weekday_text']: + print(f" {day}") + + # Step 3: Test via BraveSearcher.search_google_reviews + print("\n" + "=" * 60) + print("Testing BraveSearcher.search_google_reviews()") + print("=" * 60) + + brave = BraveSearcher() + result = brave.search_google_reviews('INPI', 'Wejherowo') + + print(f"\n Results:") + print(f" - google_rating: {result.get('google_rating')}") + print(f" - google_reviews_count: {result.get('google_reviews_count')}") + print(f" - business_status: {result.get('business_status')}") + print(f" - opening_hours: {'Yes' if result.get('opening_hours') else 'Not available'}") + + # Verify expected data + print("\n" + "=" * 60) + print("VERIFICATION SUMMARY") + print("=" * 60) + + success = True + + if result.get('google_rating') is not None: + print(f"[PASS] google_rating present: {result['google_rating']}") + else: + print("[FAIL] google_rating is None") + success = False + + if result.get('google_reviews_count') is not None: + print(f"[PASS] google_reviews_count present: {result['google_reviews_count']}") + else: + print("[FAIL] google_reviews_count is None") + success = False + + if success: + print("\n[SUCCESS] Google Places API integration is working!") + print("The social_media_audit.py can now collect Google reviews data.") + else: + print("\n[WARNING] Some data was not retrieved.") + print("Check if the business has a Google Business Profile with reviews.") + + return 0 if success else 1 + + +if __name__ == '__main__': + sys.exit(main())