joshuadunlop commited on
Commit
f5ed2ae
·
verified ·
1 Parent(s): 53590af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -27,44 +27,35 @@ def get_backlinks(api_login, api_key, target_url, filters):
27
 
28
  # Log the full response for debugging
29
  st.text("API Response:")
30
- # st.json(response.json()) # This will display the full JSON response in the Streamlit app
31
-
32
- # For debugging: Display the status code and any text
33
  st.text(f"Response Status Code: {response.status_code}")
34
  st.text(f"Response Headers: {response.headers}")
35
  try:
36
- st.text(f"Response Body: {response.json()}")
37
- except ValueError:
38
- st.text("Response Body: <Not a JSON response>")
39
-
40
- # Check if the response contains 'results' key
41
- if response.status_code == 200:
42
- response_data = response.json()
43
-
44
- # Check if 'results' key is in the response
45
- if 'results' in response_data:
46
- results = response_data['results']
47
- if results:
48
- # Adjust the following line based on the actual JSON structure
49
- df = pd.json_normalize(results)
50
- return df
51
- else:
52
- st.error("Received empty data from API.")
53
- return None
54
  else:
55
- # Handle other internal status codes here
56
- internal_status_code = response_data.get('status_code', None)
57
- internal_status_message = response_data.get('status_message', 'No specific message provided')
58
- st.error(f"Internal Status Code: {internal_status_code}, Message: {internal_status_message}")
59
  return None
60
  else:
61
- error_message = response.json().get('status_message', 'No specific error message provided')
62
- st.error(f"Error: Code: {response.status_code} Message: {error_message}")
63
  return None
64
 
65
  def convert_df_to_csv(df):
66
  # Convert DataFrame to CSV
67
- return df.to_csv().encode('utf-8')
68
 
69
  # Streamlit layout
70
  st.sidebar.title("DataForSEO API Parameters")
@@ -107,7 +98,7 @@ col1, col2 = st.columns(2)
107
 
108
  with col1:
109
  st.header("Input")
110
- target_url = st.text_input("Enter the target URL") # Define target_url here
111
 
112
  generate_button = st.sidebar.button("Generate All")
113
  reset_button = st.sidebar.button("Reset")
@@ -116,16 +107,26 @@ reset_button = st.sidebar.button("Reset")
116
  if generate_button and target_url:
117
  df = get_backlinks(api_login, api_key, target_url, filters)
118
  if df is not None:
119
- # Convert DataFrame to CSV
120
  csv = convert_df_to_csv(df)
121
- # Create download link
122
  st.download_button(
123
  label="Download data as CSV",
124
  data=csv,
125
  file_name='backlinks.csv',
126
  mime='text/csv',
127
  )
 
 
128
 
129
  # Reset functionality
130
  if reset_button:
131
  st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Log the full response for debugging
29
  st.text("API Response:")
 
 
 
30
  st.text(f"Response Status Code: {response.status_code}")
31
  st.text(f"Response Headers: {response.headers}")
32
  try:
33
+ response_json = response.json()
34
+ st.text(f"Response Body: {response_json}")
35
+ except ValueError as e:
36
+ st.text(f"Response Body: <Not a JSON response>\nError: {e}")
37
+
38
+ # Check if the response contains 'results' key and handle the JSON structure appropriately
39
+ if response.status_code == 200 and 'results' in response_json:
40
+ results = response_json['results']
41
+ if results:
42
+ # Check if the results actually contain the backlink data you expect
43
+ st.text(f"Results: {results}") # Debugging line to show the results structure
44
+
45
+ # If the structure is as expected, convert to DataFrame
46
+ df = pd.json_normalize(results)
47
+ return df
 
 
 
48
  else:
49
+ st.error("Received empty data from API.")
 
 
 
50
  return None
51
  else:
52
+ # Handle API errors
53
+ st.error(f"Error: Code: {response.status_code} Message: {response_json.get('status_message', 'No specific error message provided')}")
54
  return None
55
 
56
  def convert_df_to_csv(df):
57
  # Convert DataFrame to CSV
58
+ return df.to_csv(index=False).encode('utf-8')
59
 
60
  # Streamlit layout
61
  st.sidebar.title("DataForSEO API Parameters")
 
98
 
99
  with col1:
100
  st.header("Input")
101
+ target_url = st.text_input("Enter the target URL")
102
 
103
  generate_button = st.sidebar.button("Generate All")
104
  reset_button = st.sidebar.button("Reset")
 
107
  if generate_button and target_url:
108
  df = get_backlinks(api_login, api_key, target_url, filters)
109
  if df is not None:
 
110
  csv = convert_df_to_csv(df)
 
111
  st.download_button(
112
  label="Download data as CSV",
113
  data=csv,
114
  file_name='backlinks.csv',
115
  mime='text/csv',
116
  )
117
+ else:
118
+ st.error("Failed to generate CSV: No data returned from the API or data processing error.")
119
 
120
  # Reset functionality
121
  if reset_button:
122
  st.experimental_rerun()
123
+
124
+ # Ensure that the download button is only visible if the DataFrame is not None
125
+ if df is not None:
126
+ csv = convert_df_to_csv(df)
127
+ st.download_button(
128
+ label="Download data as CSV",
129
+ data=csv,
130
+ file_name='backlinks.csv',
131
+ mime='text/csv',
132
+ )