rivapereira123 commited on
Commit
b6fee06
Β·
verified Β·
1 Parent(s): 5ae8648

Update modules/analysis.py

Browse files
Files changed (1) hide show
  1. modules/analysis.py +85 -0
modules/analysis.py CHANGED
@@ -710,3 +710,88 @@ def analyze_github(readme_text=None):
710
  return clean_text("\\n".join(tips))
711
 
712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710
  return clean_text("\\n".join(tips))
711
 
712
 
713
+
714
+ import requests
715
+ import time
716
+ import json
717
+
718
+ # Make sure this is defined globally
719
+ memo_data = []
720
+
721
+ def fetch_and_analyze_linkedin(linkedin_url):
722
+ if not linkedin_url.strip():
723
+ return "⚠️ Please enter a valid LinkedIn profile URL."
724
+
725
+ apify_token = os.getenv("APIFY_TOKEN")
726
+ if not apify_token:
727
+ return "⚠️ APIFY_TOKEN not found in environment variables."
728
+
729
+ actor_id = "rivapereira268~linkedin-profile-full-sections-scraper---no-cookies-task"
730
+ start_url = f"https://api.apify.com/v2/actor-tasks/{actor_id}/runs?token={apify_token}"
731
+ input_payload = {"profileUrls": [linkedin_url]}
732
+
733
+ try:
734
+ # Step 1: Start Apify run
735
+ run_response = requests.post(start_url, json=input_payload)
736
+ run_data = run_response.json()
737
+ if "data" not in run_data or "id" not in run_data["data"]:
738
+ return "❌ Failed to start Apify task."
739
+
740
+ run_id = run_data["data"]["id"]
741
+ print(f"[DEBUG] Apify task started. Run ID: {run_id}")
742
+
743
+ # Step 2: Poll for status
744
+ status_url = f"https://api.apify.com/v2/actor-runs/{run_id}?token={apify_token}"
745
+ for _ in range(30):
746
+ time.sleep(2)
747
+ run_status = requests.get(status_url).json()
748
+ status = run_status["data"]["status"]
749
+ print(f"[DEBUG] Apify task status: {status}")
750
+ if status == "SUCCEEDED":
751
+ break
752
+ elif status in ["FAILED", "ABORTED"]:
753
+ return f"❌ Apify task failed: {status}"
754
+
755
+ # Step 3: Fetch dataset
756
+ dataset_id = run_status["data"]["defaultDatasetId"]
757
+ items_url = f"https://api.apify.com/v2/datasets/{dataset_id}/items?format=json"
758
+ items = requests.get(items_url).json()
759
+ print(f"[DEBUG] Items fetched: {len(items)}")
760
+
761
+ if not items or not isinstance(items, list):
762
+ return "❌ No data returned from Apify. LinkedIn profile may be private or blocked."
763
+
764
+ # Step 4: Unwrap nested list if needed
765
+ while isinstance(items[0], list):
766
+ items = items[0]
767
+ profile_data = items[0]
768
+
769
+ if not isinstance(profile_data, dict):
770
+ return "❌ Apify returned unexpected data format."
771
+
772
+ # Step 5: Analyze profile and extract actions
773
+ result = analyze_scraped_linkedin_profile(profile_data)
774
+ print(f"[DEBUG] Analysis Result: {result[:100]}...")
775
+
776
+ actions = extract_actions_from_feedback(result, source="Linky")
777
+ print(f"[DEBUG] Actions Extracted: {len(actions)}")
778
+ memo_data.extend(actions)
779
+
780
+ # Step 6: Add generic Linky nudges if missing
781
+ existing_texts = [entry["text"] for entry in memo_data]
782
+ linky_nudges = [
783
+ {"type": "Action", "text": "🧩 Create a free Linktree to unify your portfolio links", "source": "Linky"},
784
+ {"type": "Action", "text": "🀝 Reach out for 1–2 professional recommendations on LinkedIn", "source": "Linky"},
785
+ {"type": "Action", "text": "🌿 Add a volunteering experience β€” even academic or event-based", "source": "Linky"},
786
+ {"type": "Action", "text": "πŸ“Š Review your LinkedIn Analytics this month", "source": "Linky"},
787
+ ]
788
+ for nudge in linky_nudges:
789
+ if nudge["text"] not in existing_texts:
790
+ memo_data.append(nudge)
791
+
792
+ return result
793
+
794
+ except Exception as e:
795
+ print(f"[ERROR] Exception during LinkedIn analysis: {e}")
796
+ return f"❌ Internal error: {e}"
797
+