File size: 13,448 Bytes
70f8027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import streamlit as st
import requests
import pandas as pd
import time
from datetime import datetime, timedelta
from urllib.parse import urlencode
from openai import OpenAI
from io import StringIO
import json

# Set up the application title and layout
st.set_page_config(
    page_title="Steam App Reviews - Themes Analysis",
)

st.title("🎮 Steam App Reviews - Themes Analysis")

# Sidebar inputs for user interaction
st.sidebar.header("User Input Parameters")

# User input for OpenAI API key
api_key_input = st.sidebar.text_input(
    "Enter your OpenAI API Key:",
    type="password",
    help="Your API key will be used to access the OpenAI API for theme extraction.",
)

# Initialize OpenAI client
client = None
if api_key_input:
    client = OpenAI(api_key=api_key_input)
else:
    st.sidebar.warning("Please enter your OpenAI API Key to proceed.")

# User input for App ID
appid = st.sidebar.text_input("Enter the Steam App ID:", value="1782120")

# Date input for selecting a week
st.sidebar.write("Select the date range for reviews:")
start_date = st.sidebar.date_input(
    "Start Date", value=datetime.today() - timedelta(days=7)
)
end_date = st.sidebar.date_input("End Date", value=datetime.today())

# Validate dates
if start_date > end_date:
    st.error("Error: End date must fall after start date.")
elif not api_key_input:
    st.info("Please input your OpenAI API Key to proceed.")
else:
    # Fetch reviews button
    if st.sidebar.button("Fetch and Analyze Reviews"):
        st.write("Fetching reviews...")

        # Convert dates to timestamps
        start_timestamp = int(time.mktime(start_date.timetuple()))
        end_timestamp = int(
            time.mktime((end_date + timedelta(days=1)).timetuple())
        ) - 1  # Include the entire end date

        # Define the base API URL
        base_url = f"https://store.steampowered.com/appreviews/{appid}?json=1"

        # Define initial API parameters
        params = {
            "filter": "recent",
            "language": "all",
            "day_range": "365",  # Maximum allowed
            "review_type": "all",
            "purchase_type": "all",
            "num_per_page": "100",
            "cursor": "*",
            "filter_offtopic_activity": 0,
        }

        # Function to fetch reviews
        def fetch_reviews(max_reviews=200):
            """
            Fetches Steam reviews for the specified app within the given date range.
            The function uses pagination to retrieve reviews and applies a limit to avoid infinite loops.

            Args:
                max_reviews (int): Maximum number of reviews to fetch.

            Returns:
                list: A list of reviews within the specified date range.
            """
            reviews_list = []
            request_count = 0
            max_requests = 50  # Limit the number of requests to avoid infinite loops

            while True:
                # URL encode the cursor parameter
                params_encoded = params.copy()
                params_encoded["cursor"] = params["cursor"].replace("+", "%2B")

                # Construct the full URL with parameters
                url = base_url + "&" + urlencode(params_encoded)

                # Make the API request
                try:
                    response = requests.get(url)
                    response.raise_for_status()
                    data = response.json()
                except requests.exceptions.RequestException as e:
                    st.error(f"Steam API Error: {e}")
                    return None

                # Check if the request was successful
                if data.get("success") != 1:
                    st.error("Failed to fetch reviews from Steam API.")
                    return None

                # Append reviews to the list
                reviews = data.get("reviews", [])
                if not reviews:
                    # No more reviews
                    break

                for review in reviews:
                    # Filter reviews based on timestamp
                    if start_timestamp <= review.get("timestamp_created", 0) <= end_timestamp:
                        reviews_list.append(review)
                        if len(reviews_list) >= max_reviews:
                            break
                    elif review.get("timestamp_created", 0) < start_timestamp:
                        # Since reviews are ordered by most recent, we can break early
                        break

                # Update the cursor for the next batch
                new_cursor = data.get("cursor")
                if params["cursor"] == new_cursor:
                    # Exit if the cursor hasn't changed to avoid infinite loop
                    break
                params["cursor"] = new_cursor

                # Check if maximum number of reviews fetched
                if len(reviews_list) >= max_reviews:
                    break

                # Increment request count and check limit
                request_count += 1
                if request_count >= max_requests:
                    st.warning("Reached maximum number of requests. Some reviews may not be fetched.")
                    break

                # Optional: To avoid hitting the rate limit
                time.sleep(0.2)

            return reviews_list

        # Fetch the reviews
        reviews_data = fetch_reviews(max_reviews=200)

        # Check if reviews were fetched
        if reviews_data:
            st.success(f"Fetched {len(reviews_data)} reviews from App ID {appid}.")

            # Create a DataFrame from the review data
            df = pd.DataFrame(
                [
                    {
                        "Review ID": str(review.get("recommendationid")),
                        "Author SteamID": review.get("author", {}).get("steamid"),
                        "Language": review.get("language"),
                        "Review": review.get("review"),
                        "Posted On": datetime.fromtimestamp(
                            review.get("timestamp_created", 0)
                        ).strftime("%Y-%m-%d %H:%M:%S"),
                    }
                    for review in reviews_data
                ]
            )

            # Function to extract themes using OpenAI GPT-4o
            def extract_themes(df):
                """
                Uses OpenAI's GPT-4o model to identify the most common themes,
                provide descriptions, and list review IDs where each theme is mentioned.

                Args:
                    df (DataFrame): DataFrame containing the reviews.

                Returns:
                    DataFrame: A DataFrame containing themes, descriptions, and review references.
                """
                # Combine reviews into a single string with IDs
                reviews_text = "\n".join(
                    [
                        f"Review ID: {row['Review ID']}\nReview Text: {row['Review']}"
                        for _, row in df.iterrows()
                    ]
                )

                # Prepare the prompt
                prompt = f"""
Analyze the following user reviews and identify the most common themes or topics being discussed.
For each theme, provide a brief description and list the Review IDs where the theme is mentioned.

Provide the output as a JSON array matching the following structure:
[
    {{"Theme": "<theme_name>", "Description": "<description>", "Review IDs": ["<review_id1>", "<review_id2>", ...]}},
    ...
]

Ensure the output is valid JSON.

Reviews:
{reviews_text}
"""
                # Call OpenAI API
                response = client.chat.completions.create(
                    model="gpt-4o",
                    messages=[{"role": "user", "content": prompt}],
                    temperature=1,
                    max_tokens=2048,
                    top_p=1,
                    frequency_penalty=0,
                    presence_penalty=0,
                    response_format={
                        "type": "json_schema",
                        "json_schema": {
                            "name": "analyze_game_reviews",
                            "strict": True,
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "parameters": {
                                        "type": "object",
                                        "properties": {
                                            "themes": {
                                                "type": "array",
                                                "description": "List of themes identified in the game reviews",
                                                "items": {
                                                    "type": "object",
                                                    "properties": {
                                                        "Theme": {
                                                            "type": "string",
                                                            "description": "The main theme derived from the game reviews.",
                                                        },
                                                        "Description": {
                                                            "type": "string",
                                                            "description": "A summary of the issues or sentiments related to the theme.",
                                                        },
                                                        "Review IDs": {
                                                            "type": "array",
                                                            "description": "Array of IDs for reviews that relate to this theme.",
                                                            "items": {
                                                                "type": "string",
                                                                "description": "Unique identifier for a review.",
                                                            },
                                                        },
                                                    },
                                                    "required": [
                                                        "Theme",
                                                        "Description",
                                                        "Review IDs",
                                                    ],
                                                    "additionalProperties": False,
                                                },
                                            }
                                        },
                                        "required": ["themes"],
                                        "additionalProperties": False,
                                    }
                                },
                                "required": ["parameters"],
                                "additionalProperties": False,
                            },
                        },
                    },
                )

                # Get the response content
                response_text = response.choices[0].message.content  # Extract the text

                # Parse the JSON output
                try:
                    response_data = json.loads(response_text)
                    themes_list = (
                        response_data.get("parameters", {}).get("themes", [])
                    )  # Extract the 'themes' list
                    themes_df = pd.DataFrame(themes_list)  # Create DataFrame from the list
                    return themes_df
                except ValueError as ve:
                    st.error(f"Error parsing JSON response: {ve}")
                    st.error(f"Raw API Response: {response_text}")
                    return None

            # Extract themes
            themes_df = extract_themes(df)

            if themes_df is not None:
                st.write("### Most Common Themes")
                st.dataframe(themes_df)

                # Display detailed information for each theme
                for index, row in themes_df.iterrows():
                    theme = row["Theme"]
                    description = row["Description"]
                    review_ids = row["Review IDs"]

                    st.write(f"#### Theme: {theme}")
                    st.write(f"**Description:** {description}")
                    st.write(f"**Mentioned in {len(review_ids)} reviews.**")

                    # Get the reviews that mention the theme
                    try:
                        reviews_with_theme = df[
                            df["Review ID"].isin(review_ids)
                        ][["Review ID", "Review"]]
                        st.write("Reviews mentioning this theme:")
                        st.dataframe(reviews_with_theme)
                    except ValueError as e:
                        st.error(
                            f"Error displaying reviews for theme '{theme}': {e}"
                        )
            else:
                st.warning("Failed to extract themes.")

        else:
            st.warning("No reviews to display.")