File size: 3,066 Bytes
91ebd3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import json
import pandas as pd

def extract_usernames(file_obj, is_following=False):
    if file_obj is None:
        return set()
    
    try:
        # Read the uploaded file
        data = json.load(open(file_obj.name, "r"))
        
        # Instagram's JSON structure can vary slightly by account age
        # 'following' usually has a top-level key, 'followers' is often just a list
        if is_following and "relationships_following" in data:
            entries = data["relationships_following"]
        else:
            entries = data if isinstance(data, list) else data.get("relationships_followers", [])

        usernames = []
        for entry in entries:
            # Extract username from the 'string_list_data' field
            if "string_list_data" in entry:
                usernames.append(entry["string_list_data"][0]["value"])
        
        return set(usernames)
    except Exception as e:
        return f"Error: {str(e)}"

def analyze_stats(followers_file, following_file):
    followers = extract_usernames(followers_file)
    following = extract_usernames(following_file, is_following=True)
    
    if isinstance(followers, str): return followers, None, None
    if isinstance(following, str): return following, None, None

    # Logic: Who doesn't follow back?
    not_following_back = list(following - followers)
    # Logic: Who do you not follow back?
    fans = list(followers - following)
    
    df_not_back = pd.DataFrame(not_following_back, columns=["Username"])
    df_fans = pd.DataFrame(fans, columns=["Username"])
    
    summary = f"๐Ÿ“Š **Results:**\n- **Not Following You Back:** {len(not_following_back)}\n- **Fans (You don't follow back):** {len(fans)}"
    
    return summary, df_not_back, df_fans

# --- UI Layout ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# ๐Ÿ“ธ Instagram Safe Tracker")
    gr.Markdown("""
    ### ๐Ÿ›ก๏ธ 100% Account Safe
    No login required. This app only analyzes the data files you download from Instagram.
    
    **How to get your files:**
    1. Go to Instagram **Settings** > **Your Activity** > **Download your information**.
    2. Click **Download or transfer information**.
    3. Select **Followers and Following**.
    4. Select **Format: JSON** (NOT HTML) and **Date Range: All Time**.
    5. Once you get the email, upload `followers_1.json` and `following.json` below.
    """)
    
    with gr.Row():
        file_followers = gr.File(label="Upload followers_1.json")
        file_following = gr.File(label="Upload following.json")
        
    btn = gr.Button("๐Ÿ” Find Unfollowers", variant="primary")
    
    out_summary = gr.Markdown()
    
    with gr.Tabs():
        with gr.TabItem("Not Following You Back"):
            out_not_back = gr.DataFrame()
        with gr.TabItem("Fans (You don't follow back)"):
            out_fans = gr.DataFrame()

    btn.click(
        analyze_stats, 
        inputs=[file_followers, file_following], 
        outputs=[out_summary, out_not_back, out_fans]
    )

demo.launch()