#!/usr/bin/env python3 """ Test the complete workflow with affiliate tag functionality """ import asyncio import sys import os # Add the current directory to Python path to import our modules sys.path.append(os.path.dirname(os.path.abspath(__file__))) from url_utils import expand_ecommerce_url, is_amazon_short_link, is_amazon_url from scraper import scrape import re from regex_patterns import amazon_url_patterns async def test_complete_workflow(): """Test the complete workflow with affiliate tag functionality""" user_link = "https://amzn.to/4nDPQmT" print(f"🔗 Testing Complete Workflow with Affiliate Tags") print(f"Original URL: {user_link}") print("=" * 60) # Step 1: Process URL (expand + add affiliate tag) if is_amazon_short_link(user_link): print("✅ Detected as Amazon short link") print("🔄 Expanding URL and adding affiliate tag...") processed_url = expand_ecommerce_url(user_link, add_affiliate_tag=True, affiliate_tag='thrissurvarth-21') print(f"✅ Processed URL: {processed_url}") # Verify affiliate tag is present if 'tag=thrissurvarth-21' in processed_url: print("🎯 ✅ Affiliate tag 'thrissurvarth-21' successfully added!") else: print("❌ Affiliate tag not found") else: print("❌ Not detected as Amazon short link") return # Step 2: Platform detection platform = "amazon" if any(re.match(pattern, processed_url) for pattern in amazon_url_patterns) else "unknown" print(f"🏷️ Platform detected: {platform}") if platform == "amazon": # Step 3: Scrape product information print("🔍 Scraping product information...") try: product_name, price = await scrape(processed_url, platform) if product_name and price: print("🎉 SUCCESS! Complete workflow test passed:") print(f" 📦 Product: {product_name}") print(f" 💰 Price: ₹{price}") print(f" 🔗 Final URL (with affiliate tag): {processed_url}") print(f" 🏷️ Platform: {platform}") # Verify the final URL structure if 'tag=thrissurvarth-21' in processed_url and is_amazon_url(processed_url): print(" ✅ Affiliate tag correctly preserved in final URL") else: print(" ⚠️ Issue with affiliate tag in final URL") else: print("❌ Failed to extract product information") except Exception as e: print(f"❌ Error during scraping: {e}") else: print("❌ Platform detection failed") print("\n" + "=" * 60) print("🏁 Complete workflow test finished!") if __name__ == "__main__": asyncio.run(test_complete_workflow())