forked from ksanjeev284/reddit-universal-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
149 lines (120 loc) · 5.07 KB
/
Copy pathconfig.py
File metadata and controls
149 lines (120 loc) · 5.07 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
"""
Reddit Scraper Suite - Configuration
"""
import os
from pathlib import Path
# Load environment variables from .env file if it exists
env_path = Path(__file__).parent / ".env"
if env_path.exists():
with open(env_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, val = line.split("=", 1)
os.environ[key.strip()] = val.strip().strip('"').strip("'")
# --- PATHS ---
BASE_DIR = Path(__file__).parent
DATA_DIR = BASE_DIR / "data"
DB_PATH = DATA_DIR / "reddit_scraper.db"
# --- SCRAPER SETTINGS ---
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
# Sources: old.reddit.com for residential IPs, mirrors for data centers
MIRRORS = [
"https://old.reddit.com",
"https://redlib.privadency.com",
"https://redlib.orangenet.cc",
"https://red.artemislena.eu"
]
# Rate limiting
REQUEST_TIMEOUT = 15
COOLDOWN_SECONDS = 3
RETRY_WAIT = 30
# Media settings
MAX_IMAGES_PER_POST = 10
MAX_VIDEOS_PER_POST = 2
MAX_GALLERY_IMAGES = 15
# Comment settings
MAX_COMMENT_DEPTH = 5
# --- ASYNC SETTINGS ---
ASYNC_MAX_CONCURRENT = 10
ASYNC_BATCH_SIZE = 50
# --- NOTIFICATION SETTINGS ---
DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL", "")
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", "")
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID", "")
# --- DASHBOARD SETTINGS ---
DASHBOARD_HOST = "0.0.0.0"
DASHBOARD_PORT = 8501
# --- SCHEDULER SETTINGS ---
SCHEDULER_TIMEZONE = "Asia/Kolkata"
# --- PROXY SETTINGS ---
# Generic proxy URL (e.g. http://username:password@host:port)
PROXY_URL = os.getenv("PROXY_URL", "")
PROXY_COUNTRY = os.getenv("PROXY_COUNTRY", "")
PROXY_SESSION_ID = os.getenv("PROXY_SESSION_ID", "")
PROXY_AUTO_ROTATE = os.getenv("PROXY_AUTO_ROTATE", "true").lower() in ("true", "1", "yes")
def get_formatted_proxy_url(proxy_url, country=None, session_id=None, force_rotate=False):
"""
Format ScrapingAnt proxy URL to append country and session ID dynamically.
For standard proxies, returns the URL unchanged.
"""
if not proxy_url:
return proxy_url
import random
import string
from urllib.parse import urlparse, urlunparse
try:
parsed = urlparse(proxy_url)
username = parsed.username
if not username or not username.startswith("customer-"):
return proxy_url # Not ScrapingAnt proxy
# Parse username parts (format: customer-USERNAME[-country-cc][-sessionid-id])
parts = username.split("-")
# Determine target country
target_country = country if country is not None else PROXY_COUNTRY
if target_country and target_country.lower() == "none":
target_country = ""
# Determine target session ID
target_session = session_id if session_id is not None else PROXY_SESSION_ID
if target_session and target_session.lower() == "auto":
target_session = ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))
elif target_session and target_session.lower() == "none":
target_session = ""
# Build new username
# Keep customer-USERNAME part (typically parts[0] is 'customer' and parts[1] is the username)
new_username_parts = parts[:2] if len(parts) >= 2 else parts
# Handle country code
# Check if country was in original username and not overwritten
original_country = ""
original_session = ""
i = 2
while i < len(parts):
if parts[i] == "country" and i + 1 < len(parts):
original_country = parts[i+1]
i += 2
elif parts[i] == "sessionid" and i + 1 < len(parts):
original_session = parts[i+1]
i += 2
else:
new_username_parts.append(parts[i])
i += 1
# Apply country override or retain original if not specified
final_country = target_country if target_country is not None else original_country
if final_country:
new_username_parts.extend(["country", final_country.lower()])
# Apply session ID override or retain original if not specified
final_session = target_session if target_session is not None else original_session
if final_session:
new_username_parts.extend(["sessionid", final_session])
new_username = "-".join(new_username_parts)
# Reconstruct network location
netloc = f"{new_username}:{parsed.password}@{parsed.hostname}"
if parsed.port:
netloc += f":{parsed.port}"
return urlunparse((parsed.scheme, netloc, parsed.path, parsed.params, parsed.query, parsed.fragment))
except Exception:
return proxy_url
# --- DATABASE SETTINGS ---
DATABASE_URL = os.getenv("DATABASE_URL", f"sqlite:///{DB_PATH}")
# Ensure data directory exists
DATA_DIR.mkdir(exist_ok=True)