Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,9 +9,18 @@ API_KEY = "AIzaSyCHmsNAEP7I-ASxt8uS2kxc8O5P2x_-KQY"
|
|
9 |
SEARCH_API_KEY = "AIzaSyCHmsNAEP7I-ASxt8uS2kxc8O5P2x_-KQY"
|
10 |
CX = "561935e384c5e4a19" # Your Custom Search Engine ID
|
11 |
|
12 |
-
# Initialize Google Maps client
|
13 |
gmaps = googlemaps.Client(key=API_KEY)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def get_business_suggestions(query, location):
|
16 |
try:
|
17 |
if not query or len(query) < 3:
|
@@ -131,15 +140,9 @@ def save_to_excel(data, filename="my_website_rankings.xlsx"):
|
|
131 |
combined_data[keyword]["SERP Link"] = entry.get("Link", "-")
|
132 |
|
133 |
df = pd.DataFrame(list(combined_data.values()))
|
134 |
-
column_order = [
|
135 |
-
"Date", "Time", "Keyword",
|
136 |
-
"Google Maps Rank", "Google Maps Business Name", "Google Maps Address",
|
137 |
-
"SERP Rank", "SERP Title", "SERP Link"
|
138 |
-
]
|
139 |
-
df = df[column_order]
|
140 |
df.to_excel(filename, index=False, engine='openpyxl')
|
141 |
|
142 |
-
st.success(f"β
|
143 |
st.download_button(
|
144 |
label="π₯ Download Excel File",
|
145 |
data=open(filename, "rb").read(),
|
@@ -149,103 +152,85 @@ def save_to_excel(data, filename="my_website_rankings.xlsx"):
|
|
149 |
except Exception as e:
|
150 |
st.error(f"Error saving Excel file: {str(e)}")
|
151 |
|
152 |
-
#
|
153 |
st.set_page_config(page_title="Business Rank Checker", layout="wide")
|
154 |
st.title("π Business Rank Checker")
|
155 |
-
st.markdown("Check your business ranking on Google Maps and search results")
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
st.session_state.business_place_id = None
|
161 |
-
|
162 |
-
with st.form("search_form"):
|
163 |
-
col1, col2 = st.columns(2)
|
164 |
-
with col1:
|
165 |
location = st.text_input("π Target Location", "Nottingham, MD, USA")
|
166 |
-
with col2:
|
167 |
search_radius_km = st.slider("π Search Radius (km)", 1, 20, 2)
|
|
|
168 |
|
169 |
-
|
|
|
|
|
170 |
|
171 |
-
|
172 |
-
suggestions = get_business_suggestions(st.session_state.business_query, location)
|
173 |
if suggestions:
|
174 |
-
selected_option = st.selectbox(
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
website_url = st.text_input("π Your Website URL", "", help="Enter your website to check if it appears in search results")
|
191 |
keywords_input = st.text_input("π Search Keywords (comma separated)", "Phone repair, mobile repair")
|
192 |
-
|
193 |
-
submitted = st.form_submit_button("π Check Rankings")
|
194 |
-
|
195 |
-
if submitted and st.session_state.business_selection and st.session_state.business_place_id:
|
196 |
-
keywords = [kw.strip() for kw in keywords_input.split(",") if kw.strip()]
|
197 |
-
|
198 |
-
if not keywords:
|
199 |
-
st.warning("Please enter at least one search keyword")
|
200 |
-
st.stop()
|
201 |
-
|
202 |
-
my_website_rankings = []
|
203 |
-
|
204 |
-
with st.spinner("Analyzing rankings..."):
|
205 |
-
for keyword in keywords:
|
206 |
-
st.subheader(f"π Results for keyword: '{keyword}'")
|
207 |
-
|
208 |
-
st.markdown("### π Google Maps Rankings")
|
209 |
-
maps_rankings = get_google_maps_rank(
|
210 |
-
keyword, location,
|
211 |
-
st.session_state.business_selection, st.session_state.business_place_id,
|
212 |
-
search_radius_km
|
213 |
-
)
|
214 |
-
|
215 |
-
if "error" in maps_rankings:
|
216 |
-
st.error(f"Error: {maps_rankings['error']}")
|
217 |
-
else:
|
218 |
-
my_website_rankings.append({
|
219 |
-
"Keyword": keyword,
|
220 |
-
"Source": "Google Maps",
|
221 |
-
"Rank": maps_rankings["rank"],
|
222 |
-
"Business Name": maps_rankings["name"],
|
223 |
-
"Address": maps_rankings["address"]
|
224 |
-
})
|
225 |
-
st.markdown(f"#### Your Business: **{st.session_state.business_selection}**")
|
226 |
-
st.write(f"Rank: {maps_rankings['rank']}")
|
227 |
-
st.write(f"Address: {maps_rankings['address']}")
|
228 |
-
|
229 |
-
st.markdown(f"### π Search Results for '{keyword}' in {location}")
|
230 |
-
serp_results = get_serp_rankings(keyword, location, website_url)
|
231 |
-
|
232 |
-
if "error" in serp_results:
|
233 |
-
st.error(serp_results["error"])
|
234 |
-
else:
|
235 |
-
my_website_rankings.append({
|
236 |
-
"Keyword": keyword,
|
237 |
-
"Source": "Google Search",
|
238 |
-
"Rank": serp_results["rank"],
|
239 |
-
"Title": serp_results["title"],
|
240 |
-
"Link": serp_results["link"]
|
241 |
-
})
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
st.write(f"Title: {serp_results['title']}")
|
246 |
-
st.write(f"Link: {serp_results['link']}")
|
247 |
|
248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
SEARCH_API_KEY = "AIzaSyCHmsNAEP7I-ASxt8uS2kxc8O5P2x_-KQY"
|
10 |
CX = "561935e384c5e4a19" # Your Custom Search Engine ID
|
11 |
|
|
|
12 |
gmaps = googlemaps.Client(key=API_KEY)
|
13 |
|
14 |
+
# -------------------- SESSION STATE INIT --------------------
|
15 |
+
if "current_step" not in st.session_state:
|
16 |
+
st.session_state.current_step = 1
|
17 |
+
|
18 |
+
if "selected_business" not in st.session_state:
|
19 |
+
st.session_state.selected_business = None
|
20 |
+
if "business_place_id" not in st.session_state:
|
21 |
+
st.session_state.business_place_id = None
|
22 |
+
|
23 |
+
# -------------------- FUNCTIONS --------------------
|
24 |
def get_business_suggestions(query, location):
|
25 |
try:
|
26 |
if not query or len(query) < 3:
|
|
|
140 |
combined_data[keyword]["SERP Link"] = entry.get("Link", "-")
|
141 |
|
142 |
df = pd.DataFrame(list(combined_data.values()))
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
df.to_excel(filename, index=False, engine='openpyxl')
|
144 |
|
145 |
+
st.success(f"β
Rankings saved to **{filename}**")
|
146 |
st.download_button(
|
147 |
label="π₯ Download Excel File",
|
148 |
data=open(filename, "rb").read(),
|
|
|
152 |
except Exception as e:
|
153 |
st.error(f"Error saving Excel file: {str(e)}")
|
154 |
|
155 |
+
# -------------------- UI --------------------
|
156 |
st.set_page_config(page_title="Business Rank Checker", layout="wide")
|
157 |
st.title("π Business Rank Checker")
|
|
|
158 |
|
159 |
+
# Step 1 - Business Selection
|
160 |
+
if st.session_state.current_step == 1:
|
161 |
+
with st.form("step1_form"):
|
|
|
|
|
|
|
|
|
|
|
162 |
location = st.text_input("π Target Location", "Nottingham, MD, USA")
|
|
|
163 |
search_radius_km = st.slider("π Search Radius (km)", 1, 20, 2)
|
164 |
+
business_query = st.text_input("π’ Your Business Name", "")
|
165 |
|
166 |
+
suggestions = []
|
167 |
+
if business_query and len(business_query) >= 3:
|
168 |
+
suggestions = get_business_suggestions(business_query, location)
|
169 |
|
170 |
+
selected_option = None
|
|
|
171 |
if suggestions:
|
172 |
+
selected_option = st.selectbox("π Select your business", [s["name"] for s in suggestions])
|
173 |
+
|
174 |
+
confirm_btn = st.form_submit_button("β
Confirm Selection")
|
175 |
+
if confirm_btn and selected_option:
|
176 |
+
selected_place_id = next(s["place_id"] for s in suggestions if s["name"] == selected_option)
|
177 |
+
st.session_state.selected_business = selected_option
|
178 |
+
st.session_state.business_place_id = selected_place_id
|
179 |
+
st.session_state.location = location
|
180 |
+
st.session_state.search_radius_km = search_radius_km
|
181 |
+
st.session_state.current_step = 2
|
182 |
+
|
183 |
+
# Step 2 - Ranking Check
|
184 |
+
if st.session_state.current_step == 2:
|
185 |
+
st.subheader(f"π Business Selected: {st.session_state.selected_business}")
|
186 |
+
website_url = st.text_input("π Your Website URL")
|
|
|
|
|
187 |
keywords_input = st.text_input("π Search Keywords (comma separated)", "Phone repair, mobile repair")
|
188 |
+
back_btn, check_btn = st.columns([1, 3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
+
if back_btn.button("π Change Business"):
|
191 |
+
st.session_state.current_step = 1
|
|
|
|
|
192 |
|
193 |
+
if check_btn.button("π Check Rankings"):
|
194 |
+
keywords = [kw.strip() for kw in keywords_input.split(",") if kw.strip()]
|
195 |
+
rankings_data = []
|
196 |
+
|
197 |
+
with st.spinner("Analyzing..."):
|
198 |
+
for keyword in keywords:
|
199 |
+
st.markdown(f"### π {keyword}")
|
200 |
+
|
201 |
+
maps_result = get_google_maps_rank(
|
202 |
+
keyword,
|
203 |
+
st.session_state.location,
|
204 |
+
st.session_state.selected_business,
|
205 |
+
st.session_state.business_place_id,
|
206 |
+
st.session_state.search_radius_km
|
207 |
+
)
|
208 |
|
209 |
+
if "error" in maps_result:
|
210 |
+
st.error(maps_result["error"])
|
211 |
+
else:
|
212 |
+
st.write(f"**Google Maps Rank:** {maps_result['rank']}")
|
213 |
+
st.write(f"**Address:** {maps_result['address']}")
|
214 |
+
rankings_data.append({
|
215 |
+
"Keyword": keyword,
|
216 |
+
"Source": "Google Maps",
|
217 |
+
"Rank": maps_result['rank'],
|
218 |
+
"Business Name": maps_result['name'],
|
219 |
+
"Address": maps_result['address']
|
220 |
+
})
|
221 |
+
|
222 |
+
serp_result = get_serp_rankings(keyword, st.session_state.location, website_url)
|
223 |
+
if "error" in serp_result:
|
224 |
+
st.error(serp_result["error"])
|
225 |
+
else:
|
226 |
+
st.write(f"**SERP Rank:** {serp_result['rank']}")
|
227 |
+
rankings_data.append({
|
228 |
+
"Keyword": keyword,
|
229 |
+
"Source": "Google Search",
|
230 |
+
"Rank": serp_result['rank'],
|
231 |
+
"Title": serp_result['title'],
|
232 |
+
"Link": serp_result['link']
|
233 |
+
})
|
234 |
+
|
235 |
+
if rankings_data:
|
236 |
+
save_to_excel(rankings_data)
|