Jay-2776 commited on
Commit
f7f4da3
Β·
verified Β·
1 Parent(s): 1aba8ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -28
app.py CHANGED
@@ -6,22 +6,27 @@ 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",
@@ -37,22 +42,30 @@ def check_domain_rank(keyword):
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,
@@ -60,12 +73,11 @@ def check_domain_rank(keyword):
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,
@@ -73,10 +85,10 @@ def check_domain_rank(keyword):
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
@@ -84,8 +96,8 @@ def run_bulk_keywords(text):
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
 
@@ -99,10 +111,11 @@ 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(
@@ -114,7 +127,7 @@ with gr.Blocks(title="Domain Rank Checker (USA)") as demo:
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,
@@ -122,4 +135,4 @@ with gr.Blocks(title="Domain Rank Checker (USA)") as demo:
122
  outputs=[output_table, download_file]
123
  )
124
 
125
- demo.launch(share=True, debug=True)
 
6
  from urllib.parse import urlparse
7
 
8
  # ---------------- CONFIG ----------------
9
+ API_KEY = os.getenv("HASDATA_API_KEY")
10
+ BASE_URL = "https://api.hasdata.com/scrape/google/serp"
11
 
12
  TARGET_DOMAIN = "rishabhsoft.com"
13
+ MAX_PAGES = 10 # Top 10 pages (Top 100 results)
14
 
15
  if not API_KEY:
16
+ raise ValueError("HASDATA_API_KEY is not set in Hugging Face Secrets")
17
 
18
  # ---------------- CORE LOGIC ----------------
19
  def check_domain_rank(keyword):
20
  """
21
+ Checks keyword ranking for TARGET_DOMAIN.
22
+ Stops immediately once domain is found.
23
+ USA Google results only.
24
  """
25
 
26
+ headers = {
27
+ "Accept": "application/json"
28
+ }
29
+
30
  for page in range(MAX_PAGES):
31
  params = {
32
  "engine": "google",
 
42
  "google_domain": "google.com"
43
  }
44
 
45
+ response = requests.get(BASE_URL, params=params, headers=headers, timeout=30)
46
+
47
+ if response.status_code != 200:
48
+ return {
49
+ "Keyword": keyword,
50
+ "Domain": TARGET_DOMAIN,
51
+ "Page": "Error",
52
+ "Position on Page": "Error",
53
+ "Absolute Rank": "Error",
54
+ "URL": None,
55
+ "Title": response.text
56
+ }
57
 
58
+ data = response.json()
59
+ organic_results = data.get("organic_results", [])
60
 
61
+ if not organic_results:
62
  break
63
 
64
+ for idx, result in enumerate(organic_results, start=1):
65
+ url = result.get("link", "")
66
  domain = urlparse(url).netloc.replace("www.", "")
67
 
68
  if TARGET_DOMAIN in domain:
 
69
  return {
70
  "Keyword": keyword,
71
  "Domain": TARGET_DOMAIN,
 
73
  "Position on Page": idx,
74
  "Absolute Rank": page * 10 + idx,
75
  "URL": url,
76
+ "Title": result.get("title")
77
  }
78
 
79
+ time.sleep(1) # rate-limit safety
80
 
 
81
  return {
82
  "Keyword": keyword,
83
  "Domain": TARGET_DOMAIN,
 
85
  "Position on Page": "Not Found",
86
  "Absolute Rank": "Not Found",
87
  "URL": None,
88
+ "Title": "Not ranking in Top 100"
89
  }
90
 
91
+ # ---------------- BULK HANDLER ----------------
92
  def run_bulk_keywords(text):
93
  if not text.strip():
94
  return None, None
 
96
  keywords = [k.strip() for k in text.splitlines() if k.strip()]
97
  results = []
98
 
99
+ for keyword in keywords:
100
+ results.append(check_domain_rank(keyword))
101
 
102
  df = pd.DataFrame(results)
103
 
 
111
  gr.Markdown("## πŸ” Domain Rank Checker β€” USA Google Results")
112
  gr.Markdown(
113
  "**Tracked domain:** https://www.rishabhsoft.com/\n\n"
114
+ "- πŸ‡ΊπŸ‡Έ USA Google results only\n"
115
+ "- πŸ”Ž Checks Top 10 pages (Top 100)\n"
116
+ "- ⚑ Stops when domain is found (credit-optimized)\n"
117
+ "- πŸ“‹ One keyword per line\n"
118
+ "- πŸ“₯ Excel export supported"
119
  )
120
 
121
  keyword_box = gr.Textbox(
 
127
  run_btn = gr.Button("Check Rankings")
128
 
129
  output_table = gr.Dataframe(wrap=True)
130
+ download_file = gr.File(label="Download Excel Report")
131
 
132
  run_btn.click(
133
  fn=run_bulk_keywords,
 
135
  outputs=[output_table, download_file]
136
  )
137
 
138
+ demo.launch()