# -*- coding: utf-8 -*- import gradio as gr import json import os from Data_Fetching_and_Rendering import fetch_and_render_dashboard from analytics_fetch_and_rendering import fetch_and_render_analytics from mentions_dashboard import generate_mentions_dashboard from gradio_utils import get_url_user_token from Bubble_API_Calls import fetch_linkedin_token_from_bubble def check_token_status(token_state): return "✅ Token available" if token_state and token_state.get("token") else "❌ Token not available" def process_and_store_bubble_token(url_user_token, org_urn, token_state): new_state = token_state.copy() if token_state else {"token": None, "client_id": None, "org_urn": None} new_state.update({"token": None, "org_urn": org_urn}) client_id = os.environ.get("Linkedin_client_id") if not client_id: print("❌ CRITICAL ERROR: 'Linkedin_client_id' environment variable not set.") new_state["client_id"] = "ENV VAR MISSING" return check_token_status(new_state), new_state new_state["client_id"] = client_id if not url_user_token or "not found" in url_user_token or "Could not access" in url_user_token: return check_token_status(new_state), new_state print(f"Attempting to fetch token from Bubble with user token: {url_user_token}") parsed = fetch_linkedin_token_from_bubble(url_user_token) if isinstance(parsed, dict) and "access_token" in parsed: new_state["token"] = parsed print("✅ Token successfully fetched from Bubble.") else: print("❌ Failed to fetch a valid token from Bubble.") return check_token_status(new_state), new_state def guarded_fetch_dashboard(token_state): if not token_state or not token_state.get("token"): return "

❌ Access denied. No token available.

" return fetch_and_render_dashboard(token_state.get("client_id"), token_state.get("token")) def guarded_fetch_analytics(token_state): if not token_state or not token_state.get("token"): return ("

❌ Access denied. No token available.

", None, None, None, None, None, None, None) return fetch_and_render_analytics(token_state.get("client_id"), token_state.get("token")) def run_mentions_and_load(token_state): if not token_state or not token_state.get("token"): return ("

❌ Access denied. No token available.

", None) return generate_mentions_dashboard(token_state.get("client_id"), token_state.get("token")) with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), title="LinkedIn Post Viewer & Analytics") as app: token_state = gr.State(value={"token": None, "client_id": None, "org_urn": None}) gr.Markdown("# 🚀 LinkedIn Organization Post Viewer & Analytics") gr.Markdown("Token is supplied via URL parameter for Bubble.io lookup. Then explore dashboard and analytics.") url_user_token_display = gr.Textbox(label="User Token (from URL - Hidden)", interactive=False, visible=False) status_box = gr.Textbox(label="Overall Token Status", interactive=False) org_urn = gr.Textbox(visible=False) # Needed for input, was missing from initial script app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn]) url_user_token_display.change( fn=process_and_store_bubble_token, inputs=[url_user_token_display, org_urn, token_state], outputs=[status_box, token_state] ) app.load(fn=check_token_status, inputs=[token_state], outputs=status_box) gr.Timer(5.0).tick(fn=check_token_status, inputs=[token_state], outputs=status_box) with gr.Tabs(): with gr.TabItem("1️⃣ Dashboard"): gr.Markdown("View your organization's recent posts and their engagement statistics.") fetch_dashboard_btn = gr.Button("📊 Fetch Posts & Stats", variant="primary") dashboard_html = gr.HTML("

Waiting for token...

") fetch_dashboard_btn.click(fn=guarded_fetch_dashboard, inputs=[token_state], outputs=[dashboard_html]) with gr.TabItem("2️⃣ Analytics"): gr.Markdown("View follower count and monthly gains for your organization.") fetch_analytics_btn = gr.Button("📈 Fetch Follower Analytics", variant="primary") follower_count = gr.Markdown("

Waiting for token...

") with gr.Row(): follower_plot, growth_plot = gr.Plot(), gr.Plot() with gr.Row(): eng_rate_plot = gr.Plot() with gr.Row(): interaction_plot = gr.Plot() with gr.Row(): eb_plot = gr.Plot() with gr.Row(): mentions_vol_plot, mentions_sentiment_plot = gr.Plot(), gr.Plot() fetch_analytics_btn.click( fn=guarded_fetch_analytics, inputs=[token_state], outputs=[follower_count, follower_plot, growth_plot, eng_rate_plot, interaction_plot, eb_plot, mentions_vol_plot, mentions_sentiment_plot] ) with gr.TabItem("3️⃣ Mentions"): gr.Markdown("Analyze sentiment of recent posts that mention your organization.") fetch_mentions_btn = gr.Button("🧠 Fetch Mentions & Sentiment", variant="primary") mentions_html = gr.HTML("

Waiting for token...

") mentions_plot = gr.Plot() fetch_mentions_btn.click( fn=run_mentions_and_load, inputs=[token_state], outputs=[mentions_html, mentions_plot] ) if __name__ == "__main__": if not os.environ.get("Linkedin_client_id"): print("WARNING: The 'Linkedin_client_id' environment variable is not set. The application may not function correctly.") app.launch(server_name="0.0.0.0", server_port=7860, share=True)