Jay-2776 commited on
Commit
a60ecc7
ยท
verified ยท
1 Parent(s): 1cfd28d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import pandas as pd
4
+ import time
5
+ import os
6
+ from urllib.parse import urlparse
7
+
8
+ # ---------------- CONFIG ----------------
9
+ API_KEY = os.getenv("SERPAPI_KEY")
10
+ BASE_URL = "https://serpapi.com/search.json"
11
+
12
+ TARGET_DOMAIN = "rishabhsoft.com"
13
+ MAX_PAGES = 10 # Top 10 pages only (Top 100)
14
+
15
+ if not API_KEY:
16
+ raise ValueError("SERPAPI_KEY is not set in environment variables")
17
+
18
+ # ---------------- CORE LOGIC ----------------
19
+ def check_domain_rank(keyword):
20
+ """
21
+ Stops immediately once the domain is found.
22
+ Uses USA Google results only.
23
+ """
24
+
25
+ for page in range(MAX_PAGES):
26
+ params = {
27
+ "engine": "google",
28
+ "q": keyword,
29
+ "api_key": API_KEY,
30
+ "start": page * 10,
31
+ "num": 10,
32
+
33
+ # ---- FORCE USA RESULTS ----
34
+ "gl": "us",
35
+ "hl": "en",
36
+ "location": "United States",
37
+ "google_domain": "google.com"
38
+ }
39
+
40
+ r = requests.get(BASE_URL, params=params)
41
+ if r.status_code != 200:
42
+ raise RuntimeError(f"API Error: {r.text}")
43
+
44
+ data = r.json()
45
+ organic = data.get("organic_results", [])
46
+
47
+ if not organic:
48
+ break
49
+
50
+ for idx, res in enumerate(organic, start=1):
51
+ url = res.get("link", "")
52
+ domain = urlparse(url).netloc.replace("www.", "")
53
+
54
+ if TARGET_DOMAIN in domain:
55
+ # ๐Ÿš€ STOP SCANNING AS SOON AS FOUND
56
+ return {
57
+ "Keyword": keyword,
58
+ "Domain": TARGET_DOMAIN,
59
+ "Page": page + 1,
60
+ "Position on Page": idx,
61
+ "Absolute Rank": page * 10 + idx,
62
+ "URL": url,
63
+ "Title": res.get("title")
64
+ }
65
+
66
+ time.sleep(1) # rate safety
67
+
68
+ # Not ranked in Top 100
69
+ return {
70
+ "Keyword": keyword,
71
+ "Domain": TARGET_DOMAIN,
72
+ "Page": "Not Found",
73
+ "Position on Page": "Not Found",
74
+ "Absolute Rank": "Not Found",
75
+ "URL": None,
76
+ "Title": "Not ranking in top 100"
77
+ }
78
+
79
+ # ---------------- BULK KEYWORD HANDLER ----------------
80
+ def run_bulk_keywords(text):
81
+ if not text.strip():
82
+ return None, None
83
+
84
+ keywords = [k.strip() for k in text.splitlines() if k.strip()]
85
+ results = []
86
+
87
+ for kw in keywords:
88
+ results.append(check_domain_rank(kw))
89
+
90
+ df = pd.DataFrame(results)
91
+
92
+ output_file = "rishabhsoft_us_keyword_rankings.xlsx"
93
+ df.to_excel(output_file, index=False)
94
+
95
+ return df, output_file
96
+
97
+ # ---------------- UI ----------------
98
+ with gr.Blocks(title="Domain Rank Checker (USA)") as demo:
99
+ gr.Markdown("## ๐Ÿ” Domain Rank Checker โ€” USA Google Results")
100
+ gr.Markdown(
101
+ "**Tracked domain:** https://www.rishabhsoft.com/\n\n"
102
+ "- Searches **USA Google only**\n"
103
+ "- Checks **Top 10 pages (Top 100)**\n"
104
+ "- **Stops scanning once domain is found** (credit optimized)\n"
105
+ "- Paste **one keyword per line**"
106
+ )
107
+
108
+ keyword_box = gr.Textbox(
109
+ label="Keywords (one per line)",
110
+ lines=10,
111
+ placeholder="power apps development company\nenterprise software development"
112
+ )
113
+
114
+ run_btn = gr.Button("Check Rankings")
115
+
116
+ output_table = gr.Dataframe(wrap=True)
117
+ download_file = gr.File(label="Download Result Excel")
118
+
119
+ run_btn.click(
120
+ fn=run_bulk_keywords,
121
+ inputs=keyword_box,
122
+ outputs=[output_table, download_file]
123
+ )
124
+
125
+ demo.launch(share=True, debug=True)