Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
352 lines (297 loc) · 12.7 KB
/
Copy pathapp.py
File metadata and controls
352 lines (297 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Email & City Scraper — Flask backend.
"""
from __future__ import annotations
import csv
import io
import json
import logging
import os
import re
import threading
import time
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor, as_completed
from urllib.parse import urlparse
import openpyxl
import requests
import xlrd
from flask import Flask, Response, jsonify, render_template, request, stream_with_context
from scraper import scrape_url
# ── Logging ────────────────────────────────────────────────────────
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
)
log = logging.getLogger("app")
# ── Flask app ──────────────────────────────────────────────────────
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 10 * 1024 * 1024
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.jinja_env.auto_reload = True
@app.after_request
def _no_cache(resp):
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"
return resp
MAX_WORKERS = 8
MAX_URLS = 500
DOMAIN_COOLDOWN = 1.0 # seconds between requests to the same domain
# ── Header / URL detection ─────────────────────────────────────────
WEBSITE_COL_RE = re.compile(
r"^\s*(website|web\s*site|site|url|domain|homepage|web|link)s?\s*$", re.I
)
GSHEET_RE = re.compile(r"docs\.google\.com/spreadsheets/d/([a-zA-Z0-9_-]+)", re.I)
GSHEET_GID_RE = re.compile(r"[#?&]gid=(\d+)", re.I)
URL_LIKE_RE = re.compile(
r"^(https?://)?([a-z0-9][a-z0-9\-]*\.)+[a-z]{2,}(/.*)?$", re.I
)
# ── Per-domain rate limiter ────────────────────────────────────────
class DomainThrottle:
"""One slot per domain — serializes requests so the same site isn't hammered."""
def __init__(self, cooldown: float):
self.cooldown = cooldown
self._last_hit: dict[str, float] = defaultdict(float)
self._locks: dict[str, threading.Lock] = defaultdict(threading.Lock)
self._meta_lock = threading.Lock()
def acquire(self, url: str) -> None:
domain = urlparse(url if "://" in url else "https://" + url).netloc.lower()
with self._meta_lock:
lock = self._locks[domain]
with lock:
elapsed = time.time() - self._last_hit[domain]
if elapsed < self.cooldown:
time.sleep(self.cooldown - elapsed)
self._last_hit[domain] = time.time()
throttle = DomainThrottle(DOMAIN_COOLDOWN)
def _throttled_scrape(url: str) -> dict | None:
throttle.acquire(url)
return scrape_url(url)
# ── Smart column detection ─────────────────────────────────────────
def _looks_like_url(v: str) -> bool:
return bool(URL_LIKE_RE.match(v.strip()))
def _extract_urls_from_rows(rows: list[list[str]]) -> list[str]:
if not rows:
return []
header = rows[0]
target_col = next(
(i for i, c in enumerate(header) if WEBSITE_COL_RE.match(c or "")),
None,
)
if target_col is None:
cols = max(len(r) for r in rows)
scores = [0] * cols
for r in rows:
for i, c in enumerate(r):
if c and _looks_like_url(c):
scores[i] += 1
if max(scores, default=0) >= 2:
target_col = scores.index(max(scores))
urls: list[str] = []
if target_col is not None:
start = 1 if WEBSITE_COL_RE.match(header[target_col] or "") else 0
for r in rows[start:]:
if target_col < len(r):
v = (r[target_col] or "").strip()
if v and _looks_like_url(v):
urls.append(v)
if not urls:
for r in rows:
for c in r:
v = (c or "").strip()
if v and _looks_like_url(v):
urls.append(v)
return urls
# ── File parsers ───────────────────────────────────────────────────
def _csv_rows(data: bytes) -> list[list[str]]:
text = data.decode("utf-8-sig", errors="replace")
return [list(row) for row in csv.reader(io.StringIO(text))]
def _xls_rows(data: bytes) -> list[list[str]]:
wb = xlrd.open_workbook(file_contents=data)
out = []
for s in wb.sheets():
for r in range(s.nrows):
out.append([str(s.cell_value(r, c)).strip() for c in range(s.ncols)])
return out
def _xlsx_rows(data: bytes) -> list[list[str]]:
wb = openpyxl.load_workbook(filename=io.BytesIO(data), data_only=True)
out = []
for s in wb.worksheets:
for r in s.iter_rows(values_only=True):
out.append([("" if c is None else str(c)).strip() for c in r])
return out
# ── Google Sheets ──────────────────────────────────────────────────
def _is_gsheet(u: str) -> bool:
return bool(GSHEET_RE.search(u))
def _fetch_gsheet_csv(url: str) -> bytes | None:
m = GSHEET_RE.search(url)
if not m:
return None
sid = m.group(1)
gid = (GSHEET_GID_RE.search(url) or [None, "0"])[1] if GSHEET_GID_RE.search(url) else "0"
export_url = f"https://docs.google.com/spreadsheets/d/{sid}/export?format=csv&gid={gid}"
try:
r = requests.get(export_url, timeout=20, allow_redirects=True)
r.raise_for_status()
return r.content
except Exception:
return None
# ── URL collection ─────────────────────────────────────────────────
def _collect_urls(request) -> tuple[list[str], str | None]:
"""Returns (urls, error_message)."""
urls: list[str] = []
if "file" in request.files:
f = request.files["file"]
fn = (f.filename or "").lower()
data = f.read()
try:
if fn.endswith(".csv"):
urls = _extract_urls_from_rows(_csv_rows(data))
elif fn.endswith(".xlsx"):
urls = _extract_urls_from_rows(_xlsx_rows(data))
elif fn.endswith(".xls"):
urls = _extract_urls_from_rows(_xls_rows(data))
else:
return [], "Unsupported file type. Use CSV or XLS/XLSX."
except Exception as exc:
return [], f"Could not parse file: {exc}"
else:
body = request.get_json(silent=True) or {}
raw = body.get("urls", "") or request.form.get("urls", "")
cands = [u.strip() for u in re.split(r"[,\n\r]+", raw) if u.strip()]
for c in cands:
if _is_gsheet(c):
sheet = _fetch_gsheet_csv(c)
if sheet is None:
return [], (
"Could not download the Google Sheet. "
"Make sure it's shared as 'Anyone with the link can view'."
)
urls.extend(_extract_urls_from_rows(_csv_rows(sheet)))
else:
urls.append(c)
urls = list(dict.fromkeys(u.strip() for u in urls if u.strip()))[:MAX_URLS]
return urls, None
# ── Routes ─────────────────────────────────────────────────────────
@app.route("/")
def index():
return render_template("index.html")
@app.route("/scrape", methods=["POST"])
def scrape():
"""Synchronous scrape — used when SSE isn't available (older clients)."""
urls, err = _collect_urls(request)
if err:
return jsonify({"error": err}), 400
if not urls:
return jsonify({"error": "No website URLs found in your input."}), 400
results, failed = [], []
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as ex:
futs = {ex.submit(_throttled_scrape, u): u for u in urls}
for f in as_completed(futs):
u = futs[f]
try:
r = f.result()
if r:
results.append(r)
else:
failed.append(u)
except Exception as exc:
log.warning("scrape error for %s: %s", u, exc)
failed.append(u)
return jsonify(
{"results": results, "failed": failed,
"count": len(results), "scanned": len(urls)}
)
@app.route("/scrape-stream", methods=["POST"])
def scrape_stream():
"""Stream live progress via Server-Sent Events."""
urls, err = _collect_urls(request)
if err:
return jsonify({"error": err}), 400
if not urls:
return jsonify({"error": "No website URLs found in your input."}), 400
def gen():
yield f"data: {json.dumps({'event':'start','total':len(urls)})}\n\n"
results, failed = [], []
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as ex:
futs = {ex.submit(_throttled_scrape, u): u for u in urls}
done = 0
for f in as_completed(futs):
u = futs[f]
done += 1
try:
r = f.result()
except Exception as exc:
log.warning("stream scrape error for %s: %s", u, exc)
r = None
if r:
results.append(r)
payload = {"event": "row", "result": r, "done": done, "total": len(urls)}
else:
failed.append(u)
payload = {"event": "failed", "url": u, "done": done, "total": len(urls)}
yield f"data: {json.dumps(payload)}\n\n"
yield f"data: {json.dumps({'event':'end','count':len(results),'failed_count':len(failed),'scanned':len(urls),'failed':failed})}\n\n"
return Response(
stream_with_context(gen()),
mimetype="text/event-stream",
headers={"X-Accel-Buffering": "no", "Cache-Control": "no-cache"},
)
@app.route("/export", methods=["POST"])
def export():
body = request.get_json(silent=True) or {}
rows = body.get("results", [])
fmt = (body.get("format") or "csv").lower()
# Keep this in sync with the dict returned by scraper.scrape_url
fieldnames = [
"url", "company", "niche", "people", "emails", "phones",
"socials", "city", "language", "tech", "contact_form",
]
# Normalize each row to have all fields
for r in rows:
for f in fieldnames:
r.setdefault(f, "")
if fmt == "xlsx":
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Results"
ws.append([f.capitalize() for f in fieldnames])
for r in rows:
ws.append([r.get(f, "") for f in fieldnames])
# Auto-width columns
for col_idx, f in enumerate(fieldnames, start=1):
longest = max([len(str(r.get(f, ""))) for r in rows] + [len(f)])
ws.column_dimensions[openpyxl.utils.get_column_letter(col_idx)].width = min(longest + 2, 60)
buf = io.BytesIO()
wb.save(buf)
buf.seek(0)
return Response(
buf.getvalue(),
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": 'attachment; filename="results.xlsx"'},
)
# Default: CSV
buf = io.StringIO()
w = csv.DictWriter(buf, fieldnames=fieldnames)
w.writeheader()
w.writerows(rows)
return Response(
buf.getvalue(),
mimetype="text/csv",
headers={"Content-Disposition": 'attachment; filename="results.csv"'},
)
# ── Entry point ────────────────────────────────────────────────────
def _run_waitress(port: int):
"""Production server (no dev-warning, handles concurrency properly)."""
from waitress import serve
log.info("Starting waitress on http://0.0.0.0:%s", port)
serve(app, host="0.0.0.0", port=port, threads=16)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
try:
_run_waitress(port)
except ImportError:
log.warning("waitress not installed — falling back to Flask dev server")
app.run(debug=False, host="0.0.0.0", port=port)