-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessions.py
More file actions
211 lines (169 loc) · 6.67 KB
/
Copy pathsessions.py
File metadata and controls
211 lines (169 loc) · 6.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
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
"""AlterLab SDK Session & Cookie Examples.
Demonstrates authenticated scraping using stored sessions and inline cookies.
Features demonstrated:
- Inline cookies for one-off authenticated scraping
- Creating and managing stored sessions
- Session validation
- Cookie rotation (refresh)
- Synchronous wrapper usage
"""
import asyncio
from alterlab import AlterLab, AlterLabSync
async def example_inline_cookies():
"""Example 1: One-off authenticated scraping with inline cookies."""
print("\n" + "=" * 60)
print("Example 1: Inline Cookies (One-off)")
print("=" * 60)
async with AlterLab(api_key="sk_test_...") as client:
# Pass cookies directly — not stored, just used for this request
result = await client.scrape(
url="https://amazon.com/dp/B0XXXXX",
cookies={
"session-id": "abc123",
"session-token": "xyz789",
"ubid-main": "131-1234567-1234567",
},
)
print(f"Status: {result.get('status_code')}")
print(f"Content length: {len(result.get('content', ''))} characters")
print(f"BYOS applied: {result.get('byos_applied', False)}")
async def example_session_crud():
"""Example 2: Full session lifecycle — create, list, get, update, delete."""
print("\n" + "=" * 60)
print("Example 2: Session CRUD")
print("=" * 60)
async with AlterLab(api_key="sk_test_...") as client:
# Create a session
session = await client.sessions.create(
name="My Amazon Prime",
domain="amazon.com",
cookies={
"session-id": "abc123",
"session-token": "xyz789",
},
notes="Personal Prime account for product scraping",
)
print(f"Created session: {session['id']}")
print(f" Name: {session['name']}")
print(f" Domain: {session['domain']}")
print(f" Cookie names: {session['cookie_names']}")
print(f" Status: {session['status']}")
# List all sessions
result = await client.sessions.list()
print(f"\nTotal sessions: {result['total']}")
for s in result["sessions"]:
print(f" - {s['name']} ({s['domain']}) [{s['status']}]")
# Get a specific session
fetched = await client.sessions.get(session["id"])
print(f"\nFetched session: {fetched['name']}")
# Update session name
updated = await client.sessions.update(
session["id"],
name="Amazon Prime - Production",
notes="Updated for production use",
)
print(f"Updated name: {updated['name']}")
# Delete when done
await client.sessions.delete(session["id"])
print("Session deleted.")
async def example_session_validation():
"""Example 3: Validate a session to check if cookies are still valid."""
print("\n" + "=" * 60)
print("Example 3: Session Validation")
print("=" * 60)
async with AlterLab(api_key="sk_test_...") as client:
# Create a session first
session = await client.sessions.create(
name="Target Session",
domain="target.com",
cookies={"session_token": "abc123"},
)
# Validate the session
validation = await client.sessions.validate(session["id"])
print(f"Valid: {validation['is_valid']}")
print(f"Confidence: {validation['confidence']}%")
print(f"Details: {validation['details']}")
if validation.get("detected_user"):
print(f"Detected user: {validation['detected_user']}")
if validation.get("cookies_expiring_soon"):
print("Warning: Some cookies are expiring soon!")
# Cleanup
await client.sessions.delete(session["id"])
async def example_cookie_rotation():
"""Example 4: Rotate cookies when they expire."""
print("\n" + "=" * 60)
print("Example 4: Cookie Rotation (Refresh)")
print("=" * 60)
async with AlterLab(api_key="sk_test_...") as client:
session = await client.sessions.create(
name="Rotating Session",
domain="example.com",
cookies={"auth": "old-token-123"},
)
print(f"Original cookie names: {session['cookie_names']}")
# Rotate cookies with fresh values
refreshed = await client.sessions.refresh(
session["id"],
cookies={"auth": "new-token-456", "refresh": "refresh-789"},
)
print(f"Refreshed cookie names: {refreshed['cookie_names']}")
print(f"Status after refresh: {refreshed['status']}")
await client.sessions.delete(session["id"])
async def example_scrape_with_session():
"""Example 5: Scrape using a stored session."""
print("\n" + "=" * 60)
print("Example 5: Scrape with Stored Session")
print("=" * 60)
async with AlterLab(api_key="sk_test_...") as client:
# Create and store session
session = await client.sessions.create(
name="Amazon Scraping Session",
domain="amazon.com",
cookies={
"session-id": "abc123",
"session-token": "xyz789",
},
)
# Scrape using the stored session
result = await client.scrape(
url="https://amazon.com/dp/B0XXXXX",
session_id=session["id"],
)
print(f"Content length: {len(result.get('content', ''))} characters")
print(f"BYOS applied: {result.get('byos_applied', False)}")
# Cleanup
await client.sessions.delete(session["id"])
def example_sync_sessions():
"""Example 6: Synchronous session management."""
print("\n" + "=" * 60)
print("Example 6: Synchronous Wrapper")
print("=" * 60)
with AlterLabSync(api_key="sk_test_...") as client:
# All session methods work synchronously too
session = client.sessions.create(
name="Sync Session",
domain="example.com",
cookies={"token": "abc123"},
)
print(f"Created: {session['id']}")
sessions = client.sessions.list()
print(f"Total sessions: {sessions['total']}")
client.sessions.delete(session["id"])
print("Deleted.")
async def main():
"""Run all examples."""
print("=" * 60)
print("AlterLab Python SDK - Session & Cookie Examples")
print("=" * 60)
await example_inline_cookies()
await example_session_crud()
await example_session_validation()
await example_cookie_rotation()
await example_scrape_with_session()
# Sync example
example_sync_sessions()
print("\n" + "=" * 60)
print("All session examples completed!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())