Repository navigation
Expand file tree
/
Copy pathextractor.py
More file actions
139 lines (118 loc) · 4.67 KB
/
Copy pathextractor.py
File metadata and controls
139 lines (118 loc) · 4.67 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
import os
from dotenv import load_dotenv
import requests
import json
import time
import re
# Load environment variables from .env file
load_dotenv()
API_KEY = os.getenv("PLACES_API_KEY")
CATEGORY = os.getenv("CATEGORY")
CITIES = os.getenv("CITIES").split(",") # Split comma-separated cities into a list
# Email regex and contact regex patterns
email_regex = r'(([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+))'
contact_regex = r'(href=[\'"]?contact([^\'" >]+)")'
ignore_ext = [".jpg", ".jpeg", ".png", ".gif", ".webp"]
# Check if API_KEY, CATEGORY, and CITIES are loaded
if not API_KEY:
print("Error: PLACES_API_KEY is missing or could not be loaded from the .env file.")
else:
print("API Key loaded successfully.")
if not CATEGORY:
print("Error: CATEGORY is missing or could not be loaded from the .env file.")
else:
print(f"Search category loaded: {CATEGORY}")
if not CITIES:
print("Error: CITIES are missing or could not be loaded from the .env file.")
else:
print(f"Cities loaded: {CITIES}")
def get_companies(city, api_key, category):
# Initialize pagination variables
search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
all_companies = []
page = 0
next_page_token = None
# Loop through pages
while page < 20:
# Set up query parameters, including the next page token if available
search_params = {
"query": f"{category} in {city}",
"key": api_key
}
if next_page_token:
search_params["pagetoken"] = next_page_token
# Perform request
response = requests.get(search_url, params=search_params)
data = response.json()
# Append current page results
results = data.get("results", [])
for result in results:
place_id = result["place_id"]
details = get_place_details(place_id, api_key, city)
if details:
all_companies.append(details)
# Check for next page token and increment page count
next_page_token = data.get("next_page_token")
if not next_page_token:
break # No more pages
page += 1
# Google may require a short delay before using next_page_token
time.sleep(2)
return all_companies
def get_place_details(place_id, api_key, city):
# Get details for a specific place ID with regionCode to ensure international phone format
details_url = "https://maps.googleapis.com/maps/api/place/details/json"
details_params = {
"place_id": place_id,
"fields": "name,website,international_phone_number,formatted_address",
"key": api_key
}
response = requests.get(details_url, params=details_params)
result = response.json().get("result", {})
# Extract relevant fields, including email using the find_email function
website = result.get("website", "")
email = find_email(website) if website else ""
return {
"name": result.get("name", ""),
"website": website,
"phone": result.get("international_phone_number", ""),
"email": email,
"address": result.get("formatted_address", ""),
"city": city,
"postcode": get_postcode(result.get("formatted_address", ""))
}
def find_email(website):
"""Tries to find an email address on the given website."""
return find_regex_html(website, email_regex, ignore_ext) or ""
def find_regex_html(url, regex, false_hit_chars=[]):
"""Fetches a URL and applies a regex to find email addresses."""
headers = {'User-Agent': 'Mozilla/5.0'}
try:
rsp = requests.get(url, headers=headers, timeout=10)
if rsp.status_code == 200:
matches = re.findall(regex, rsp.text)
if matches:
for match in matches:
if not any(ext in match[0] for ext in false_hit_chars):
return match[0]
except:
print(f"Error fetching or processing {url}")
return None
def get_postcode(address):
# Extract postal code from address if available
if address:
# Use regex to capture the postal code (assuming it's typically numeric)
match = re.search(r"\b\d{4,5}\b", address)
if match:
return match.group(0)
return ""
def save_to_json(data, filename):
with open(filename, "w", encoding='utf-8') as f:
json.dump(data, f, indent=4, ensure_ascii=False)
# Call functions for each city
for city in CITIES:
city = city.strip() # Remove any extra whitespace around city names
companies = get_companies(city, API_KEY, CATEGORY)
filename = f"{CATEGORY}-{city}.json".replace(" ", "_").lower()
save_to_json(companies, filename)
print(f"Data saved to {filename}")