GuglielmoTor commited on
Commit
9d99925
Β·
verified Β·
1 Parent(s): f023388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -11
app.py CHANGED
@@ -7,10 +7,15 @@ from Data_Fetching_and_Rendering import fetch_and_render_dashboard
7
  from analytics_fetch_and_rendering import fetch_and_render_analytics
8
  from mentions_dashboard import generate_mentions_dashboard
9
  from gradio_utils import get_url_user_token
10
- from Bubble_API_Calls import fetch_linkedin_token_from_bubble
11
-
12
- from Linkedin_Data_API_Calls import fetch_linkedin_posts, fetch_linkedin_comments, analyze_sentiment, prepare_data_for_bubble, bulk_upload_to_bubble
13
 
 
 
 
 
 
 
 
14
 
15
  def check_token_status(token_state):
16
  return "βœ… Token available" if token_state and token_state.get("token") else "❌ Token not available"
@@ -42,22 +47,68 @@ def process_and_store_bubble_token(url_user_token, org_urn, token_state):
42
  return check_token_status(new_state), new_state
43
 
44
  def guarded_fetch_posts(token_state):
 
45
  if not token_state or not token_state.get("token"):
 
46
  return "<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>"
47
 
48
  client_id = token_state.get("client_id")
49
  token_dict = token_state.get("token")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- posts = fetch_linkedin_posts(client_id, token_dict)
52
- comments_data = fetch_linkedin_comments(client_id, token_dict, [post["id"] for post in posts])
53
- sentiments = analyze_sentiment(comments_data)
54
- li_posts, li_post_stats, li_post_comments = prepare_data_for_bubble(posts, sentiments)
55
 
56
- bulk_upload_to_bubble(li_posts, "LI_post")
57
- bulk_upload_to_bubble(li_post_stats, "LI_post_stats")
58
- bulk_upload_to_bubble(li_post_comments, "LI_post_comments")
 
 
 
59
 
60
- return "<p style='color:green; text-align:center;'>βœ… Posts and comments uploaded to Bubble.</p>"
61
 
62
 
63
  def guarded_fetch_dashboard(token_state):
 
7
  from analytics_fetch_and_rendering import fetch_and_render_analytics
8
  from mentions_dashboard import generate_mentions_dashboard
9
  from gradio_utils import get_url_user_token
10
+ from Bubble_API_Calls import fetch_linkedin_token_from_bubble, bulk_upload_to_bubble
 
 
11
 
12
+ from Linkedin_Data_API_Calls import (
13
+ fetch_linkedin_posts_core,
14
+ fetch_comments,
15
+ analyze_sentiment,
16
+ compile_detailed_posts,
17
+ prepare_data_for_bubble
18
+ )
19
 
20
  def check_token_status(token_state):
21
  return "βœ… Token available" if token_state and token_state.get("token") else "❌ Token not available"
 
47
  return check_token_status(new_state), new_state
48
 
49
  def guarded_fetch_posts(token_state):
50
+ logging.info("Starting guarded_fetch_posts process.")
51
  if not token_state or not token_state.get("token"):
52
+ logging.error("Access denied. No token available.")
53
  return "<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>"
54
 
55
  client_id = token_state.get("client_id")
56
  token_dict = token_state.get("token")
57
+ org_urn = token_state.get('org_urn') # Ensure 'org_urn' is correctly fetched from token_state
58
+
59
+ if not org_urn:
60
+ logging.error("Organization URN (org_urn) not found in token_state.")
61
+ return "<p style='color:red; text-align:center;'>❌ Configuration error: Organization URN missing.</p>"
62
+ if not client_id:
63
+ logging.error("Client ID not found in token_state.")
64
+ return "<p style='color:red; text-align:center;'>❌ Configuration error: Client ID missing.</p>"
65
+
66
+
67
+ try:
68
+ # Step 1: Fetch core post data (text, summary, category) and their basic stats
69
+ logging.info(f"Step 1: Fetching core posts for org_urn: {org_urn}")
70
+ processed_raw_posts, stats_map, _ = fetch_linkedin_posts_core(client_id, token_dict, org_urn)
71
+ # org_name is returned as the third item, captured as _ if not used directly here
72
+
73
+ if not processed_raw_posts:
74
+ logging.info("No posts found to process after step 1.")
75
+ return "<p style='color:orange; text-align:center;'>ℹ️ No posts found to process.</p>"
76
+
77
+ post_urns = [post["id"] for post in processed_raw_posts if post.get("id")]
78
+ logging.info(f"Extracted {len(post_urns)} post URNs for further processing.")
79
+
80
+ # Step 2: Fetch comments for these posts
81
+ logging.info("Step 2: Fetching comments.")
82
+ all_comments_data = fetch_comments(client_id, token_dict, post_urns, stats_map)
83
+
84
+ # Step 3: Analyze sentiment of the comments
85
+ logging.info("Step 3: Analyzing sentiment.")
86
+ sentiments_per_post = analyze_sentiment(all_comments_data)
87
+
88
+ # Step 4: Compile detailed post objects
89
+ logging.info("Step 4: Compiling detailed posts.")
90
+ detailed_posts = compile_detailed_posts(processed_raw_posts, stats_map, sentiments_per_post)
91
+
92
+ # Step 5: Prepare data for Bubble
93
+ logging.info("Step 5: Preparing data for Bubble.")
94
+ li_posts, li_post_stats, li_post_comments = prepare_data_for_bubble(detailed_posts, all_comments_data)
95
+
96
+ # Step 6: Bulk upload to Bubble
97
+ logging.info("Step 6: Uploading data to Bubble.")
98
+ bulk_upload_to_bubble(li_posts, "LI_post")
99
+ bulk_upload_to_bubble(li_post_stats, "LI_post_stats")
100
+ bulk_upload_to_bubble(li_post_comments, "LI_post_comments")
101
 
102
+ logging.info("Successfully fetched and uploaded posts and comments to Bubble.")
103
+ return "<p style='color:green; text-align:center;'>βœ… Posts and comments uploaded to Bubble.</p>"
 
 
104
 
105
+ except ValueError as ve: # Catch specific errors like "Failed to fetch posts"
106
+ logging.error(f"ValueError during LinkedIn data processing: {ve}")
107
+ return f"<p style='color:red; text-align:center;'>❌ Error: {html.escape(str(ve))}</p>"
108
+ except Exception as e:
109
+ logging.exception("An unexpected error occurred in guarded_fetch_posts.") # Logs full traceback
110
+ return "<p style='color:red; text-align:center;'>❌ An unexpected error occurred. Please check logs.</p>"
111
 
 
112
 
113
 
114
  def guarded_fetch_dashboard(token_state):