Spaces:
Sleeping
Sleeping
File size: 10,732 Bytes
087ac11 |
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 |
"""
Stage 0: Web Scraping (App Store & Play Store)
Scrapes reviews and stores in database
This integrates with your existing scraper or can be used standalone
"""
import os
import sqlite3
import requests
import json
import time
from datetime import datetime
from typing import List, Dict, Any
import re
class Stage0WebScraper:
"""
Stage 0: Web scraping for App Store and Play Store reviews
Integrates with existing database structure
"""
def __init__(self, db_file: str = "review_database.db"):
self.db_file = db_file
print(f" π Database: {db_file}")
def create_reviews_table(self):
"""
Create reviews table if it doesn't exist
This is your Stage 0 schema
"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS reviews (
id INTEGER PRIMARY KEY AUTOINCREMENT,
review_id TEXT UNIQUE,
product_url TEXT,
platform TEXT,
app_name TEXT,
user_name TEXT,
review_text TEXT,
rating INTEGER,
review_date TEXT,
app_version TEXT,
scraped_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
# Create index for faster lookups
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_review_id
ON reviews(review_id)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_platform
ON reviews(platform)
""")
conn.commit()
conn.close()
print(" β
Reviews table ready (Stage 0)")
def scrape_app_store_rss(self, app_id: str, country: str = "us",
limit: int = 100) -> List[Dict]:
"""
Scrape App Store reviews using RSS feed
This is a simple, free method (no API key needed)
Args:
app_id: App Store app ID (e.g., "1234567890")
country: Country code (e.g., "us", "ae", "uk")
limit: Number of reviews to fetch (max 500 per request)
"""
print(f" π Scraping App Store: {app_id} ({country})")
# App Store RSS feed URL
url = f"https://itunes.apple.com/{country}/rss/customerreviews/id={app_id}/sortBy=mostRecent/json"
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
data = response.json()
reviews = []
entries = data.get('feed', {}).get('entry', [])
# Skip first entry (it's the app info)
if entries and 'author' not in entries[0]:
entries = entries[1:]
for entry in entries[:limit]:
try:
review = {
'review_id': entry.get('id', {}).get('label', ''),
'platform': 'app_store',
'app_name': data.get('feed', {}).get('title', {}).get('label', 'Unknown'),
'user_name': entry.get('author', {}).get('name', {}).get('label', 'Anonymous'),
'review_text': entry.get('content', {}).get('label', ''),
'rating': int(entry.get('im:rating', {}).get('label', '3')),
'review_date': entry.get('updated', {}).get('label', ''),
'app_version': entry.get('im:version', {}).get('label', ''),
'product_url': entry.get('link', {}).get('attributes', {}).get('href', '')
}
reviews.append(review)
except Exception as e:
print(f" β οΈ Error parsing review: {e}")
continue
print(f" β
Scraped {len(reviews)} reviews")
return reviews
except Exception as e:
print(f" β Error scraping App Store: {e}")
return []
def scrape_play_store_api(self, app_id: str, country: str = "us",
limit: int = 100) -> List[Dict]:
"""
Scrape Google Play Store reviews
Note: This is a simplified version. For production, use google-play-scraper library
Args:
app_id: Play Store package name (e.g., "com.company.app")
country: Country code
limit: Number of reviews to fetch
"""
print(f" π€ Scraping Play Store: {app_id} ({country})")
try:
# Using unofficial API endpoint (works without auth)
# For production, recommend: pip install google-play-scraper
from google_play_scraper import Sort, reviews_all
result = reviews_all(
app_id,
sleep_milliseconds=0,
lang='en',
country=country,
sort=Sort.NEWEST
)
reviews = []
for r in result[:limit]:
review = {
'review_id': r.get('reviewId', ''),
'platform': 'play_store',
'app_name': app_id,
'user_name': r.get('userName', 'Anonymous'),
'review_text': r.get('content', ''),
'rating': r.get('score', 3),
'review_date': r.get('at', '').isoformat() if r.get('at') else '',
'app_version': r.get('reviewCreatedVersion', ''),
'product_url': f"https://play.google.com/store/apps/details?id={app_id}"
}
reviews.append(review)
print(f" β
Scraped {len(reviews)} reviews")
return reviews
except ImportError:
print(" β οΈ google-play-scraper not installed")
print(" Run: pip install google-play-scraper")
return []
except Exception as e:
print(f" β Error scraping Play Store: {e}")
return []
def save_reviews_to_db(self, reviews: List[Dict]) -> int:
"""
Save scraped reviews to database
Returns number of new reviews saved
"""
if not reviews:
return 0
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
saved_count = 0
for review in reviews:
try:
cursor.execute("""
INSERT OR IGNORE INTO reviews
(review_id, product_url, platform, app_name, user_name,
review_text, rating, review_date, app_version)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
review.get('review_id'),
review.get('product_url', ''),
review.get('platform'),
review.get('app_name', ''),
review.get('user_name'),
review.get('review_text'),
review.get('rating'),
review.get('review_date', ''),
review.get('app_version', '')
))
if cursor.rowcount > 0:
saved_count += 1
except Exception as e:
print(f" β οΈ Error saving review: {e}")
continue
conn.commit()
conn.close()
print(f" β
Saved {saved_count} new reviews to database")
return saved_count
def scrape_from_urls_file(self, urls_file: str = "urls.txt") -> int:
"""
Scrape reviews from URLs listed in a text file
URLs file format (one per line):
app_store:1234567890:us
play_store:com.company.app:us
"""
print(f"\n π Reading URLs from: {urls_file}")
if not os.path.exists(urls_file):
print(f" β οΈ File not found: {urls_file}")
return 0
total_saved = 0
with open(urls_file, 'r') as f:
urls = [line.strip() for line in f if line.strip() and not line.startswith('#')]
print(f" β
Found {len(urls)} URLs")
for i, url in enumerate(urls, 1):
print(f"\n [{i}/{len(urls)}] Processing: {url}")
parts = url.split(':')
if len(parts) < 2:
print(f" β οΈ Invalid format: {url}")
continue
platform = parts[0].lower()
app_id = parts[1]
country = parts[2] if len(parts) > 2 else 'us'
if platform == 'app_store':
reviews = self.scrape_app_store_rss(app_id, country)
elif platform == 'play_store':
reviews = self.scrape_play_store_api(app_id, country)
else:
print(f" β οΈ Unknown platform: {platform}")
continue
saved = self.save_reviews_to_db(reviews)
total_saved += saved
# Be nice to servers
time.sleep(2)
return total_saved
def get_review_count(self) -> int:
"""Get total number of reviews in database"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM reviews")
count = cursor.fetchone()[0]
conn.close()
return count
if __name__ == "__main__":
# Run Stage 0 scraper - reads from urls.txt
print("\n" + "="*70)
print("π·οΈ STAGE 0: WEB SCRAPER")
print("="*70)
scraper = Stage0WebScraper(db_file="review_database.db")
# Create table if not exists
print("\nπ Ensuring database table exists...")
scraper.create_reviews_table()
# Scrape from urls.txt
print("\nπ Starting scraping from urls.txt...")
total_saved = scraper.scrape_from_urls_file("urls.txt")
# Show results
total_reviews = scraper.get_review_count()
print("\n" + "="*70)
print("β
SCRAPING COMPLETE!")
print("="*70)
print(f"π New reviews saved: {total_saved}")
print(f"π Total reviews in database: {total_reviews}")
print("\nπ― Next step: Run the analysis!")
print(" python main_langgraph.py")
print("="*70 + "\n") |