VISHAL BHANUSHALI преди 4 седмици
родител
ревизия
b6e4f6847c
променени са 1 файла, в които са добавени 25 реда и са изтрити 21 реда
  1. 25 21
      title_creator_app/views.py

+ 25 - 21
title_creator_app/views.py

@@ -215,7 +215,7 @@ def extract_title_or_error(product, selected_pt):
             val = attr.get("attributeValue")
             
             if desc == "Capacity":
-                extracted_data[desc] = f"Capacity {val}".replace(" in", "")
+                extracted_data[desc] = f"Capacity {val}"
             elif desc in ["Door Type", "Color"]:
                 extracted_data[desc] = val
             elif desc in ["Width", "Depth", "Height"]:
@@ -295,14 +295,16 @@ def construct_dynamic_title(raw_data,selected_pt):
         return "Could not found attribute name on product details page"
 
 @login_required
-async def title_creator_view(request):
+def title_creator_view(request):
     if request.method == 'POST' and request.FILES.get('file'):
+        scraper = cloudscraper.create_scraper()
         excel_file = request.FILES['file']
         selected_pt = request.POST.get('product_type')
         fs = FileSystemStorage()
         filename = fs.save(excel_file.name, excel_file)
         file_path = fs.path(filename)
-        scraper = cloudscraper.create_scraper()
+        
+
 
         try:
             # 1. Read Excel
@@ -313,16 +315,8 @@ async def title_creator_view(request):
                 df['New_Generated_Title'] = ""
 
             headers = {"User-Agent": "Mozilla/5.0"}
-            dynamic_token = await get_fresh_token(scraper)
             results_for_ui = []
-            # Specific Headers for the Item# API
-            api_headers = {
-                "accept": "application/json, text/plain, */*",
-                "authorization": f"Bearer {dynamic_token}",
-                # "authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
-                "client_id": "GEC",
-                "referer": "https://www.globalindustrial.com/"
-            }
+            
 
             # 3. Process each row
             for index, row in df.iterrows():
@@ -335,6 +329,16 @@ async def title_creator_view(request):
                 if pd.notna(url) and str(url).startswith('http'):
                     final_url = url
                 elif pd.notna(item_number):
+                    # Specific Headers for the Item# API
+                    dynamic_token =  get_fresh_token(scraper)
+
+                    api_headers = {
+                        "accept": "application/json, text/plain, */*",
+                        "authorization": f"Bearer {dynamic_token}",
+                        # "authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
+                        "client_id": "GEC",
+                        "referer": "https://www.globalindustrial.com/"
+                    }
                     # Call API to get URL from Item#
                     api_url = f"https://www.globalindustrial.com/catalogApis/catalog/autosuggest?key={item_number}&features=true"
                     try:
@@ -403,7 +407,6 @@ async def get_fresh_token(scraper):
         
         # 1. Check Cookies for 'Authorization'
         token = scraper.cookies.get('Authorization')
-        print("token found in cookies:----- ",token)
         if token:
             return token.replace('Bearer ', '').strip()
 
@@ -437,14 +440,7 @@ async def process_excel_task(file_path, selected_pt, task_id):
             df['New_Generated_Title'] = ""
 
         headers = {"User-Agent": "Mozilla/5.0"}
-        dynamic_token = await get_fresh_token(scraper)
-        # Specific Headers for the Item# API
-        api_headers = {
-            "accept": "application/json, text/plain, */*",
-            "authorization": f"Bearer {dynamic_token}",#"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
-            "client_id": "GEC",
-            "referer": "https://www.globalindustrial.com/"
-        }
+        
 
         # 3. Process each row
         for index, row in df.iterrows():
@@ -456,6 +452,14 @@ async def process_excel_task(file_path, selected_pt, task_id):
             if pd.notna(url) and str(url).startswith('http'):
                 final_url = url
             elif pd.notna(item_number):
+                dynamic_token = await get_fresh_token(scraper)
+                # Specific Headers for the Item# API
+                api_headers = {
+                    "accept": "application/json, text/plain, */*",
+                    "authorization": f"Bearer {dynamic_token}",#"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJERVYifQ.uOFB7h7_Aw6jbA1HSqVJ44tKMO7E1ljz1kV_JddeKL64YCOH57-l1ZX2Lly-Jnhdnxk3xMAeW5FawAgymEaMKA",
+                    "client_id": "GEC",
+                    "referer": "https://www.globalindustrial.com/"
+                }
                 # Call API to get URL from Item#
                 api_url = f"https://www.globalindustrial.com/catalogApis/catalog/autosuggest?key={item_number}&features=true"
                 try: