File size: 10,111 Bytes
adb3bbe
b560569
896ae69
b0464a9
87a87e7
f7fc39b
d252c6d
adb3bbe
538b42b
179ea1f
9d99925
57334a1
9d99925
 
 
 
 
 
 
b0464a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3038c7b
b0464a9
 
 
3038c7b
9f71fb3
9d99925
9f71fb3
9d99925
9f71fb3
 
 
 
9d99925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a9a646
9d99925
 
9f71fb3
9d99925
 
9f71fb3
9d99925
 
 
 
 
 
9f71fb3
 
3038c7b
b0464a9
 
3038c7b
b0464a9
 
4cc3230
b0464a9
 
 
 
 
 
 
 
 
 
f7fc39b
b0464a9
 
538b42b
adb3bbe
 
179ea1f
b0464a9
adb3bbe
3038c7b
 
adb3bbe
b0464a9
 
 
f7fc39b
b0464a9
f7fc39b
 
3038c7b
b0464a9
 
73e88eb
179ea1f
9f71fb3
3038c7b
b0464a9
adb3bbe
 
 
 
faf26ff
 
 
 
 
 
 
 
 
 
 
7ab0240
adb3bbe
 
 
f7fc39b
179ea1f
a9b7f24
b0464a9
88d3a6e
b0464a9
2051c7a
b0464a9
f466d89
b0464a9
6d43d2f
b0464a9
179ea1f
adb3bbe
 
179ea1f
b0464a9
 
adb3bbe
06d22e5
538b42b
 
 
b0464a9
 
538b42b
 
179ea1f
b8b7e00
538b42b
 
adb3bbe
179ea1f
 
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
# -*- coding: utf-8 -*-
import gradio as gr
import json
import os
import logging

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, bulk_upload_to_bubble

from Linkedin_Data_API_Calls import (
    fetch_linkedin_posts_core,
    fetch_comments,
    analyze_sentiment,
    compile_detailed_posts,
    prepare_data_for_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_posts(token_state):
    logging.info("Starting guarded_fetch_posts process.")
    if not token_state or not token_state.get("token"):
        logging.error("Access denied. No token available.")
        return "<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>"

    client_id = token_state.get("client_id")
    token_dict = token_state.get("token")
    org_urn = token_state.get('org_urn') # Ensure 'org_urn' is correctly fetched from token_state

    if not org_urn:
        logging.error("Organization URN (org_urn) not found in token_state.")
        return "<p style='color:red; text-align:center;'>❌ Configuration error: Organization URN missing.</p>"
    if not client_id:
        logging.error("Client ID not found in token_state.")
        return "<p style='color:red; text-align:center;'>❌ Configuration error: Client ID missing.</p>"


    try:
        # Step 1: Fetch core post data (text, summary, category) and their basic stats
        logging.info(f"Step 1: Fetching core posts for org_urn: {org_urn}")
        processed_raw_posts, stats_map, _ = fetch_linkedin_posts_core(client_id, token_dict, org_urn)
        # org_name is returned as the third item, captured as _ if not used directly here

        if not processed_raw_posts:
            logging.info("No posts found to process after step 1.")
            return "<p style='color:orange; text-align:center;'>ℹ️ No posts found to process.</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.")

        # Step 2: Fetch comments for these posts
        logging.info("Step 2: Fetching comments.")
        all_comments_data = fetch_comments(client_id, token_dict, post_urns, stats_map)

        # Step 3: Analyze sentiment of the comments
        logging.info("Step 3: Analyzing sentiment.")
        sentiments_per_post = analyze_sentiment(all_comments_data)

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

        # Step 5: Prepare data for Bubble
        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)

        # Step 6: Bulk upload to Bubble
        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 and uploaded posts and comments to Bubble.")
        return "<p style='color:green; text-align:center;'>βœ… Posts and comments uploaded to Bubble.</p>"

    except ValueError as ve: # Catch specific errors like "Failed to fetch posts"
        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. Please check logs.</p>"



def guarded_fetch_dashboard(token_state):
    if not token_state or not token_state.get("token"):
        return "<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>"
    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 ("<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>",
                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 ("<p style='color:red; text-align:center;'>❌ Access denied. No token available.</p>", 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.")
            
            sync_posts_to_bubble_btn = gr.Button("πŸ”„ Fetch, Analyze & Store Posts to Bubble", variant="primary")
            dashboard_html_output = gr.HTML("<p style='text-align: center; color: #555;'>Click the button to fetch posts and store them in Bubble. Status will appear here.</p>")
            
            # Corrected: The click handler now calls guarded_fetch_posts
            # and dashboard_html_output is correctly defined in this scope.
            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.")
            fetch_analytics_btn = gr.Button("πŸ“ˆ Fetch Follower Analytics", variant="primary")
            follower_count = gr.Markdown("<p style='text-align: center; color: #555;'>Waiting for 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.")
            fetch_mentions_btn = gr.Button("🧠 Fetch Mentions & Sentiment", variant="primary")
            mentions_html = gr.HTML("<p style='text-align: center; color: #555;'>Waiting for token...</p>")
            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)