File size: 15,897 Bytes
f9d8231
b560569
896ae69
b0464a9
87a87e7
1ba4c1b
f9d8231
f7fc39b
2a3b22e
d252c6d
adb3bbe
538b42b
179ea1f
2a3b22e
 
 
 
 
 
9d99925
 
 
 
 
 
 
b0464a9
2a3b22e
 
 
b0464a9
2a3b22e
b0464a9
 
 
2a3b22e
 
 
f9d8231
2a3b22e
 
f9d8231
2a3b22e
 
f9d8231
 
 
2a3b22e
f9d8231
 
 
b0464a9
 
 
2a3b22e
b0464a9
3038c7b
2a3b22e
 
 
 
 
f9d8231
 
 
 
 
 
 
 
 
 
 
2a3b22e
f9d8231
2a3b22e
 
f9d8231
2a3b22e
 
 
 
 
 
f9d8231
2a3b22e
 
 
f9d8231
 
2a3b22e
 
f9d8231
2a3b22e
 
f9d8231
2a3b22e
 
f9d8231
2a3b22e
f9d8231
 
 
 
 
 
 
 
 
3038c7b
9f71fb3
2a3b22e
 
 
 
9d99925
f9d8231
2a3b22e
f9d8231
9f71fb3
 
f9d8231
2a3b22e
9d99925
 
2a3b22e
9d99925
2a3b22e
 
f9d8231
 
 
 
 
 
9d99925
 
f9d8231
9d99925
 
 
f9d8231
 
9d99925
 
 
 
f9d8231
9d99925
 
 
 
 
 
 
 
 
 
 
 
4a9a646
9d99925
 
9f71fb3
f9d8231
 
9f71fb3
2a3b22e
9d99925
 
 
f9d8231
 
9f71fb3
b0464a9
2a3b22e
b0464a9
2a3b22e
 
b0464a9
4cc3230
b0464a9
2a3b22e
b0464a9
2a3b22e
b0464a9
 
 
 
2a3b22e
b0464a9
2a3b22e
b0464a9
 
2a3b22e
adb3bbe
 
179ea1f
2a3b22e
 
adb3bbe
3038c7b
 
adb3bbe
b0464a9
f9d8231
 
179ea1f
2a3b22e
adb3bbe
 
2a3b22e
f9d8231
faf26ff
2a3b22e
f9d8231
2a3b22e
 
 
 
faf26ff
2a3b22e
f9d8231
 
2a3b22e
 
f9d8231
 
 
 
2a3b22e
 
 
f9d8231
2a3b22e
f9d8231
2a3b22e
 
 
 
 
 
faf26ff
 
 
 
 
7ab0240
adb3bbe
f9d8231
adb3bbe
f9d8231
179ea1f
a9b7f24
b0464a9
88d3a6e
b0464a9
2051c7a
b0464a9
f466d89
b0464a9
6d43d2f
b0464a9
179ea1f
adb3bbe
 
179ea1f
b0464a9
 
adb3bbe
06d22e5
538b42b
f9d8231
538b42b
f9d8231
b0464a9
538b42b
 
179ea1f
b8b7e00
538b42b
2a3b22e
f9d8231
 
2a3b22e
f9d8231
2a3b22e
 
538b42b
adb3bbe
179ea1f
2a3b22e
b0464a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -- coding: utf-8 --
import gradio as gr
import json
import os
import logging
import html
import pandas as pd # Ensure pandas is imported if you're dealing with DataFrames

# Import functions from your custom modules
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
# Updated import to include fetch_posts_from_bubble
from Bubble_API_Calls import (
    fetch_linkedin_token_from_bubble,
    bulk_upload_to_bubble,
    fetch_posts_from_bubble  # Added new function
)
from Linkedin_Data_API_Calls import (
    fetch_linkedin_posts_core,
    fetch_comments,
    analyze_sentiment,
    compile_detailed_posts,
    prepare_data_for_bubble
)

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def check_token_status(token_state):
    """Checks the status of the LinkedIn token."""
    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):
    """
    Processes the user token from the URL, fetches LinkedIn token from Bubble,
    fetches initial posts from Bubble, and updates the token state and UI accordingly.
    Returns updates for status_box, token_state, and sync_posts_to_bubble_btn.
    """
    logging.info(f"Processing token with URL user token: '{url_user_token}', Org URN: '{org_urn}'")
    
    # Initialize or copy existing state, adding bubble_posts_df
    new_state = token_state.copy() if token_state else {"token": None, "client_id": None, "org_urn": None, "bubble_posts_df": None}
    # Ensure org_urn is updated from input, and bubble_posts_df is reset/initialized for this run.
    # Token will be set later if fetched.
    new_state.update({"org_urn": org_urn, "bubble_posts_df": None, "token": new_state.get("token")}) 

    # Determine button properties - default to hidden and non-interactive
    button_visible = False
    button_interactive = False

    client_id = os.environ.get("Linkedin_client_id")
    if not client_id:
        logging.error("CRITICAL ERROR: 'Linkedin_client_id' environment variable not set.")
        new_state["client_id"] = "ENV VAR MISSING"
    else:
        new_state["client_id"] = client_id

    # Attempt to fetch LinkedIn token from Bubble (related to LinkedIn API access)
    if url_user_token and "not found" not in url_user_token and "Could not access" not in url_user_token:
        logging.info(f"Attempting to fetch LinkedIn token from Bubble with user token: {url_user_token}")
        try:
            parsed_linkedin_token = fetch_linkedin_token_from_bubble(url_user_token)
            if isinstance(parsed_linkedin_token, dict) and "access_token" in parsed_linkedin_token:
                new_state["token"] = parsed_linkedin_token # Update token in new_state
                logging.info("βœ… LinkedIn Token successfully fetched from Bubble.")
            else:
                new_state["token"] = None # Explicitly set to None if fetch fails
                logging.warning(f"❌ Failed to fetch a valid LinkedIn token from Bubble. Response: {parsed_linkedin_token}")
        except Exception as e:
            new_state["token"] = None # Explicitly set to None on error
            logging.error(f"❌ Exception while fetching LinkedIn token from Bubble: {e}")
    else:
        new_state["token"] = None # Ensure token is None if no valid url_user_token
        logging.info("No valid URL user token provided for LinkedIn token fetch, or an error was indicated.")

    # Fetch posts from Bubble using org_urn
    current_org_urn = new_state.get("org_urn")
    if current_org_urn:
        logging.info(f"Attempting to fetch posts from Bubble for org_urn: {current_org_urn}")
        try:
            # Assuming fetch_posts_from_bubble returns a Pandas DataFrame or None
            df_bubble_posts = fetch_posts_from_bubble(current_org_urn)
            new_state["bubble_posts_df"] = df_bubble_posts # Store DataFrame in state

            if df_bubble_posts is not None and not df_bubble_posts.empty:
                logging.info(f"βœ… Successfully fetched {len(df_bubble_posts)} posts from Bubble. Sync button will be enabled.")
                button_visible = True
                button_interactive = True
            else:
                logging.info("ℹ️ No posts found in Bubble for this organization or DataFrame is empty. Sync button will remain hidden.")
                # button_visible and button_interactive remain False
        except Exception as e:
            logging.error(f"❌ Error fetching posts from Bubble: {e}")
            # button_visible and button_interactive remain False
    else:
        logging.warning("Org URN not available in state. Cannot fetch posts from Bubble.")
        # button_visible and button_interactive remain False
        
    token_status_message = check_token_status(new_state) # Check based on potentially updated new_state["token"]
    
    # Log the determined visibility before creating the update object
    logging.info(f"Token processing complete. LinkedIn Token Status: {token_status_message}. Button visible: {button_visible}, Button interactive: {button_interactive}")
    
    # Create a gr.update object for the button
    button_component_update = gr.update(visible=button_visible, interactive=button_interactive)
    
    return token_status_message, new_state, button_component_update

def guarded_fetch_posts(token_state):
    """
    Fetches LinkedIn posts, analyzes them, and uploads to Bubble.
    This function is guarded by token availability.
    """
    logging.info("Starting guarded_fetch_posts process.")
    if not token_state or not token_state.get("token"): # Checks for LinkedIn token
        logging.error("Access denied for guarded_fetch_posts. No LinkedIn token available.")
        return "<p style='color:red; text-align:center;'>❌ Access denied. LinkedIn token not available. Please ensure token is fetched via URL parameter.</p>"

    client_id = token_state.get("client_id")
    token_dict = token_state.get("token") # This is the LinkedIn token dict
    org_urn = token_state.get('org_urn')

    if not org_urn:
        logging.error("Organization URN (org_urn) not found in token_state for guarded_fetch_posts.")
        return "<p style='color:red; text-align:center;'>❌ Configuration error: Organization URN missing.</p>"
    if not client_id or client_id == "ENV VAR MISSING":
        logging.error("Client ID not found or missing in token_state for guarded_fetch_posts.")
        return "<p style='color:red; text-align:center;'>❌ Configuration error: LinkedIn Client ID missing (check .env file or environment variables).</p>"

    # Additional check: Ensure the button was meant to be clickable (i.e., Bubble posts were found)
    # This is an indirect check, as the button's clickability should prevent this if UI works as intended.
    # However, adding a check on bubble_posts_df might be redundant if the button is correctly managed.
    # For now, relying on the LinkedIn token check as the primary guard for this function.

    try:
        logging.info(f"Step 1: Fetching core posts for org_urn: {org_urn} using LinkedIn API.")
        processed_raw_posts, stats_map, _ = fetch_linkedin_posts_core(client_id, token_dict, org_urn)

        if not processed_raw_posts:
            logging.info("No posts found to process via LinkedIn API after step 1.")
            return "<p style='color:orange; text-align:center;'>ℹ️ No new LinkedIn posts found to process at this time.</p>"

        post_urns = [post["id"] for post in processed_raw_posts if post.get("id")]
        logging.info(f"Extracted {len(post_urns)} post URNs for further processing.")

        logging.info("Step 2: Fetching comments via LinkedIn API.")
        all_comments_data = fetch_comments(client_id, token_dict, post_urns, stats_map)

        logging.info("Step 3: Analyzing sentiment.")
        sentiments_per_post = analyze_sentiment(all_comments_data)

        logging.info("Step 4: Compiling detailed posts.")
        detailed_posts = compile_detailed_posts(processed_raw_posts, stats_map, sentiments_per_post)

        logging.info("Step 5: Preparing data for Bubble.")
        li_posts, li_post_stats, li_post_comments = prepare_data_for_bubble(detailed_posts, all_comments_data)

        logging.info("Step 6: Uploading data to Bubble.")
        bulk_upload_to_bubble(li_posts, "LI_posts")
        bulk_upload_to_bubble(li_post_stats, "LI_post_stats")
        bulk_upload_to_bubble(li_post_comments, "LI_post_comments")

        logging.info("Successfully fetched from LinkedIn and uploaded posts and comments to Bubble.")
        return "<p style='color:green; text-align:center;'>βœ… Posts and comments from LinkedIn uploaded to Bubble.</p>"

    except ValueError as ve:
        logging.error(f"ValueError during LinkedIn data processing: {ve}")
        return f"<p style='color:red; text-align:center;'>❌ Error: {html.escape(str(ve))}</p>"
    except Exception as e:
        logging.exception("An unexpected error occurred in guarded_fetch_posts.") # Logs full traceback
        return "<p style='color:red; text-align:center;'>❌ An unexpected error occurred while processing LinkedIn data. Please check logs.</p>"

def guarded_fetch_dashboard(token_state):
    """Fetches and renders the dashboard if token is available."""
    if not token_state or not token_state.get("token"):
        return "❌ Access denied. No token available for dashboard."
    return "<p style='text-align: center; color: #555;'>Dashboard content would load here if implemented.</p>"


def guarded_fetch_analytics(token_state):
    """Fetches and renders analytics if token is available."""
    if not token_state or not token_state.get("token"):
        return ("❌ Access denied. No token available for analytics.",
                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):
    """Generates mentions dashboard if token is available."""
    if not token_state or not token_state.get("token"):
        return ("❌ Access denied. No token available for mentions.", None)
    return generate_mentions_dashboard(token_state.get("client_id"), token_state.get("token"))

# --- Gradio UI Blocks ---
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
               title="LinkedIn Post Viewer & Analytics") as app:

    # Initialize state with the new field for Bubble DataFrame
    token_state = gr.State(value={"token": None, "client_id": None, "org_urn": None, "bubble_posts_df": 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 LinkedIn Token Status", interactive=False, value="Initializing...") 
    org_urn_display = gr.Textbox(label="Organization URN (from URL - Hidden)", interactive=False, visible=False)

    app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn_display])

    with gr.Tabs():
        with gr.TabItem("1️⃣ Dashboard & Sync"):
            gr.Markdown("Fetch initial data from Bubble. If posts are found, you can choose to sync newer posts from LinkedIn.")
            
            sync_posts_to_bubble_btn = gr.Button(
                "πŸ”„ Fetch from LinkedIn, Analyze & Store to Bubble", # Updated label for clarity
                variant="primary",
                visible=False, 
                interactive=False
            )
            
            dashboard_html_output = gr.HTML(
                "<p style='text-align: center; color: #555;'>System initializing... "
                "Checking for existing data in Bubble. The 'Fetch from LinkedIn...' button will activate if initial data is found.</p>"
            )
            
            # Combined trigger: process tokens and Bubble data once both URL params are potentially loaded.
            # Using .then() to chain after initial load.
            # The `process_and_store_bubble_token` will run when `org_urn_display` (which is an output of app.load)
            # receives its value.
            org_urn_display.change(
                fn=process_and_store_bubble_token,
                inputs=[url_user_token_display, org_urn_display, token_state],
                outputs=[status_box, token_state, sync_posts_to_bubble_btn] 
            )
            # Fallback if url_user_token_display changes after org_urn_display (less likely but for robustness)
            url_user_token_display.change(
                fn=process_and_store_bubble_token,
                inputs=[url_user_token_display, org_urn_display, token_state],
                outputs=[status_box, token_state, sync_posts_to_bubble_btn]
            )

            sync_posts_to_bubble_btn.click(
                fn=guarded_fetch_posts, 
                inputs=[token_state], 
                outputs=[dashboard_html_output]
            )

        with gr.TabItem("2️⃣ Analytics"):
            gr.Markdown("View follower count and monthly gains for your organization (requires LinkedIn token).")
            fetch_analytics_btn = gr.Button("πŸ“ˆ Fetch Follower Analytics", variant="primary")
            follower_count = gr.Markdown("<p style='text-align: center; color: #555;'>Waiting for LinkedIn token...</p>")

            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 (requires LinkedIn token).")
            fetch_mentions_btn = gr.Button("🧠 Fetch Mentions & Sentiment", variant="primary")
            mentions_html = gr.HTML("<p style='text-align: center; color: #555;'>Waiting for LinkedIn token...</p>")
            mentions_plot = gr.Plot()
            fetch_mentions_btn.click(
                fn=run_mentions_and_load,
                inputs=[token_state],
                outputs=[mentions_html, mentions_plot]
            )
            
    # This app.load updates the status_box based on the initial token_state.
    # The process_and_store_bubble_token function will provide a more definitive update soon after.
    app.load(fn=lambda ts: check_token_status(ts), inputs=[token_state], outputs=status_box)
    # Timer to periodically update the LinkedIn token status display
    gr.Timer(15.0).tick(fn=lambda ts: check_token_status(ts), inputs=[token_state], outputs=status_box)


if __name__ == "__main__":
    if not os.environ.get("Linkedin_client_id"):
        logging.warning("WARNING: The 'Linkedin_client_id' environment variable is not set. The application may not function correctly for LinkedIn API calls.")
    app.launch(server_name="0.0.0.0", server_port=7860, share=True)