poemsforaphrodite commited on
Commit
bb2fff1
1 Parent(s): 6ca8b8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -43
app.py CHANGED
@@ -2,7 +2,6 @@ import datetime
2
  import base64
3
  import os
4
  import streamlit as st
5
- from streamlit_elements import elements
6
  from google_auth_oauthlib.flow import Flow
7
  from googleapiclient.discovery import build
8
  from dotenv import load_dotenv
@@ -19,38 +18,25 @@ load_dotenv()
19
  COHERE_API_KEY = os.environ["COHERE_API_KEY"]
20
  co = cohere.Client(COHERE_API_KEY)
21
 
22
- # Configuration: Set to True if running locally, False if running on Streamlit Cloud
23
- IS_LOCAL = False
24
-
25
  # Constants
26
  SEARCH_TYPES = ["web", "image", "video", "news", "discover", "googleNews"]
27
  DATE_RANGE_OPTIONS = [
28
- "Last 7 Days",
29
- "Last 30 Days",
30
- "Last 3 Months",
31
- "Last 6 Months",
32
- "Last 12 Months",
33
- "Last 16 Months",
34
- "Custom Range"
35
  ]
36
  DEVICE_OPTIONS = ["All Devices", "desktop", "mobile", "tablet"]
37
  BASE_DIMENSIONS = ["page", "query", "country", "date"]
38
  MAX_ROWS = 250_000
39
  DF_PREVIEW_ROWS = 100
40
 
41
- # -------------
42
  # Streamlit App Configuration
43
- # -------------
44
-
45
  def setup_streamlit():
46
- st.set_page_config(page_title="✨ Simple Google Search Console Data | LeeFoot.co.uk", layout="wide")
47
- st.title("✨ Simple Google Search Console Data | June 2024")
48
- st.markdown(f"### Lightweight GSC Data Extractor. (Max {MAX_ROWS:,} Rows)")
49
  st.markdown(
50
  """
51
- <p>
52
- Created by <a href="https://twitter.com/LeeFootSEO" target="_blank">LeeFootSEO</a> |
53
- <a href="https://leefoot.co.uk" target="_blank">More Apps & Scripts on my Website</a>
54
  """,
55
  unsafe_allow_html=True
56
  )
@@ -76,10 +62,7 @@ def init_session_state():
76
  if 'custom_end_date' not in st.session_state:
77
  st.session_state.custom_end_date = datetime.date.today()
78
 
79
- # -------------
80
  # Data Processing Functions
81
- # -------------
82
-
83
  def fetch_content(url):
84
  try:
85
  response = requests.get(url)
@@ -121,10 +104,7 @@ def process_gsc_data(df):
121
  result = df_unique[['page', 'query', 'clicks', 'impressions', 'ctr', 'position', 'relevancy_score']]
122
  return result
123
 
124
- # -------------
125
  # Google Authentication Functions
126
- # -------------
127
-
128
  def load_config():
129
  client_config = {
130
  "web": {
@@ -132,7 +112,7 @@ def load_config():
132
  "client_secret": os.environ["CLIENT_SECRET"],
133
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
134
  "token_uri": "https://oauth2.googleapis.com/token",
135
- "redirect_uris": ["https://poemsforaphrodite-gscpro.hf.space/"],
136
  }
137
  }
138
  return client_config
@@ -163,10 +143,7 @@ def auth_search_console(client_config, credentials):
163
  }
164
  return searchconsole.authenticate(client_config=client_config, credentials=token)
165
 
166
- # -------------
167
  # Data Fetching Functions
168
- # -------------
169
-
170
  def list_gsc_properties(credentials):
171
  service = build('webmasters', 'v3', credentials=credentials)
172
  site_list = service.sites().list().execute()
@@ -191,10 +168,7 @@ def fetch_data_loading(webproperty, search_type, start_date, end_date, dimension
191
  processed_df = process_gsc_data(df)
192
  return processed_df
193
 
194
- # -------------
195
  # Utility Functions
196
- # -------------
197
-
198
  def update_dimensions(selected_search_type):
199
  return BASE_DIMENSIONS + ['device'] if selected_search_type in SEARCH_TYPES else BASE_DIMENSIONS
200
 
@@ -224,14 +198,7 @@ def property_change():
224
  def make_clickable(val):
225
  return f'<a href="{val}" target="_blank">{val}</a>'
226
 
227
- # -------------
228
  # File & Download Operations
229
- # -------------
230
-
231
- def show_dataframe(report):
232
- with st.expander("Preview the First 100 Rows (Unique Pages with Top Query)"):
233
- st.dataframe(report.head(DF_PREVIEW_ROWS))
234
-
235
  def download_csv_link(report):
236
  def to_csv(df):
237
  return df.to_csv(index=False, encoding='utf-8-sig')
@@ -240,10 +207,7 @@ def download_csv_link(report):
240
  href = f'<a href="data:file/csv;base64,{b64_csv}" download="search_console_data.csv">Download CSV File</a>'
241
  st.markdown(href, unsafe_allow_html=True)
242
 
243
- # -------------
244
  # Streamlit UI Components
245
- # -------------
246
-
247
  def show_google_sign_in(auth_url):
248
  st.info("Please sign in with Google to use the application.")
249
  st.markdown(f'<a href="{auth_url}" target="_self">Sign in with Google</a>', unsafe_allow_html=True)
 
2
  import base64
3
  import os
4
  import streamlit as st
 
5
  from google_auth_oauthlib.flow import Flow
6
  from googleapiclient.discovery import build
7
  from dotenv import load_dotenv
 
18
  COHERE_API_KEY = os.environ["COHERE_API_KEY"]
19
  co = cohere.Client(COHERE_API_KEY)
20
 
 
 
 
21
  # Constants
22
  SEARCH_TYPES = ["web", "image", "video", "news", "discover", "googleNews"]
23
  DATE_RANGE_OPTIONS = [
24
+ "Last 7 Days", "Last 30 Days", "Last 3 Months",
25
+ "Last 6 Months", "Last 12 Months", "Last 16 Months", "Custom Range"
 
 
 
 
 
26
  ]
27
  DEVICE_OPTIONS = ["All Devices", "desktop", "mobile", "tablet"]
28
  BASE_DIMENSIONS = ["page", "query", "country", "date"]
29
  MAX_ROWS = 250_000
30
  DF_PREVIEW_ROWS = 100
31
 
 
32
  # Streamlit App Configuration
 
 
33
  def setup_streamlit():
34
+ st.set_page_config(page_title="✨ Simple Google Search Console Data", layout="wide")
35
+ st.title("✨ Simple Google Search Console Data")
 
36
  st.markdown(
37
  """
38
+ Created by [LeeFootSEO](https://twitter.com/LeeFootSEO) |
39
+ [More Apps & Scripts](https://leefoot.co.uk)
 
40
  """,
41
  unsafe_allow_html=True
42
  )
 
62
  if 'custom_end_date' not in st.session_state:
63
  st.session_state.custom_end_date = datetime.date.today()
64
 
 
65
  # Data Processing Functions
 
 
66
  def fetch_content(url):
67
  try:
68
  response = requests.get(url)
 
104
  result = df_unique[['page', 'query', 'clicks', 'impressions', 'ctr', 'position', 'relevancy_score']]
105
  return result
106
 
 
107
  # Google Authentication Functions
 
 
108
  def load_config():
109
  client_config = {
110
  "web": {
 
112
  "client_secret": os.environ["CLIENT_SECRET"],
113
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
114
  "token_uri": "https://oauth2.googleapis.com/token",
115
+ "redirect_uris": ["https://your-redirect-uri.com/"],
116
  }
117
  }
118
  return client_config
 
143
  }
144
  return searchconsole.authenticate(client_config=client_config, credentials=token)
145
 
 
146
  # Data Fetching Functions
 
 
147
  def list_gsc_properties(credentials):
148
  service = build('webmasters', 'v3', credentials=credentials)
149
  site_list = service.sites().list().execute()
 
168
  processed_df = process_gsc_data(df)
169
  return processed_df
170
 
 
171
  # Utility Functions
 
 
172
  def update_dimensions(selected_search_type):
173
  return BASE_DIMENSIONS + ['device'] if selected_search_type in SEARCH_TYPES else BASE_DIMENSIONS
174
 
 
198
  def make_clickable(val):
199
  return f'<a href="{val}" target="_blank">{val}</a>'
200
 
 
201
  # File & Download Operations
 
 
 
 
 
 
202
  def download_csv_link(report):
203
  def to_csv(df):
204
  return df.to_csv(index=False, encoding='utf-8-sig')
 
207
  href = f'<a href="data:file/csv;base64,{b64_csv}" download="search_console_data.csv">Download CSV File</a>'
208
  st.markdown(href, unsafe_allow_html=True)
209
 
 
210
  # Streamlit UI Components
 
 
211
  def show_google_sign_in(auth_url):
212
  st.info("Please sign in with Google to use the application.")
213
  st.markdown(f'<a href="{auth_url}" target="_self">Sign in with Google</a>', unsafe_allow_html=True)