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gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def test_scrapingbee_login(email, password, api_key):
|
| 7 |
+
"""Test ScrapingBee login and extract cookies"""
|
| 8 |
+
|
| 9 |
+
logs = []
|
| 10 |
+
logs.append("=" * 70)
|
| 11 |
+
logs.append("TESTING SCRAPINGBEE LOGIN")
|
| 12 |
+
logs.append("=" * 70)
|
| 13 |
+
logs.append("")
|
| 14 |
+
|
| 15 |
+
if not all([email, password, api_key]):
|
| 16 |
+
logs.append("β Missing required fields!")
|
| 17 |
+
return "\n".join(logs), None
|
| 18 |
+
|
| 19 |
+
login_url = "https://www.dreamstime.com/login"
|
| 20 |
+
|
| 21 |
+
js_scenario = {
|
| 22 |
+
"instructions": [
|
| 23 |
+
{"wait": 3000},
|
| 24 |
+
{"fill": ["input[name='uname']", email]},
|
| 25 |
+
{"fill": ["input[name='pass']", password]},
|
| 26 |
+
{"click": "a.page-form__submit.btn--green"},
|
| 27 |
+
{"wait": 10000},
|
| 28 |
+
{"evaluate": "document.cookie"}
|
| 29 |
+
]
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
params = {
|
| 33 |
+
"api_key": api_key,
|
| 34 |
+
"url": login_url,
|
| 35 |
+
"render_js": "True",
|
| 36 |
+
"stealth_proxy": "True",
|
| 37 |
+
"json_response": "True",
|
| 38 |
+
"block_resources": "False",
|
| 39 |
+
"js_scenario": json.dumps(js_scenario)
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
logs.append("π Sending ScrapingBee request...")
|
| 44 |
+
logs.append(f" URL: {login_url}")
|
| 45 |
+
logs.append(f" Timeout: 180s")
|
| 46 |
+
logs.append("")
|
| 47 |
+
|
| 48 |
+
response = requests.get(
|
| 49 |
+
"https://app.scrapingbee.com/api/v1/",
|
| 50 |
+
params=params,
|
| 51 |
+
timeout=180
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
logs.append(f"β
Response received")
|
| 55 |
+
logs.append(f" Status code: {response.status_code}")
|
| 56 |
+
logs.append("")
|
| 57 |
+
|
| 58 |
+
if response.status_code != 200:
|
| 59 |
+
logs.append(f"β ScrapingBee error:")
|
| 60 |
+
logs.append(f" {response.text[:500]}")
|
| 61 |
+
return "\n".join(logs), None
|
| 62 |
+
|
| 63 |
+
json_response = response.json()
|
| 64 |
+
|
| 65 |
+
# Extract cookies from evaluate_results
|
| 66 |
+
cookies = None
|
| 67 |
+
if "evaluate_results" in json_response and json_response["evaluate_results"]:
|
| 68 |
+
logs.append(f"π Found {len(json_response['evaluate_results'])} evaluate results")
|
| 69 |
+
for result in json_response["evaluate_results"]:
|
| 70 |
+
if isinstance(result, str) and '=' in result:
|
| 71 |
+
cookie_count = len([c for c in result.split(';') if '=' in c])
|
| 72 |
+
logs.append(f"β
Extracted {cookie_count} cookies from evaluate")
|
| 73 |
+
cookies = result
|
| 74 |
+
break
|
| 75 |
+
|
| 76 |
+
# Fallback: extract from cookies field
|
| 77 |
+
if not cookies and "cookies" in json_response and json_response["cookies"]:
|
| 78 |
+
logs.append(f"π Found {len(json_response['cookies'])} cookies in response")
|
| 79 |
+
cookie_list = []
|
| 80 |
+
for cookie_obj in json_response["cookies"]:
|
| 81 |
+
name = cookie_obj.get("name", "")
|
| 82 |
+
value = cookie_obj.get("value", "")
|
| 83 |
+
if name and value:
|
| 84 |
+
cookie_list.append(f"{name}={value}")
|
| 85 |
+
|
| 86 |
+
if cookie_list:
|
| 87 |
+
cookies = "; ".join(cookie_list)
|
| 88 |
+
logs.append(f"β
Extracted {len(cookie_list)} cookies from response")
|
| 89 |
+
|
| 90 |
+
if cookies:
|
| 91 |
+
logs.append("")
|
| 92 |
+
logs.append("=" * 70)
|
| 93 |
+
logs.append("COOKIES EXTRACTED SUCCESSFULLY")
|
| 94 |
+
logs.append("=" * 70)
|
| 95 |
+
logs.append(f"Length: {len(cookies)} characters")
|
| 96 |
+
logs.append(f"Preview: {cookies[:100]}...")
|
| 97 |
+
logs.append("")
|
| 98 |
+
|
| 99 |
+
# Count cookies
|
| 100 |
+
cookie_names = [c.split('=')[0].strip() for c in cookies.split(';') if '=' in c]
|
| 101 |
+
logs.append(f"Total cookies: {len(cookie_names)}")
|
| 102 |
+
logs.append(f"Cookie names: {', '.join(cookie_names[:10])}...")
|
| 103 |
+
logs.append("")
|
| 104 |
+
|
| 105 |
+
# Check for critical auth cookies
|
| 106 |
+
critical_cookies = ['dreamstime_user', 'PHPSESSID', 'WB', 'lastid']
|
| 107 |
+
found_critical = [name for name in critical_cookies if name in cookie_names]
|
| 108 |
+
missing_critical = [name for name in critical_cookies if name not in cookie_names]
|
| 109 |
+
|
| 110 |
+
if found_critical:
|
| 111 |
+
logs.append(f"β
Found critical auth cookies: {', '.join(found_critical)}")
|
| 112 |
+
if missing_critical:
|
| 113 |
+
logs.append(f"β οΈ Missing critical auth cookies: {', '.join(missing_critical)}")
|
| 114 |
+
|
| 115 |
+
return "\n".join(logs), cookies
|
| 116 |
+
else:
|
| 117 |
+
logs.append("")
|
| 118 |
+
logs.append("β No cookies found in response")
|
| 119 |
+
return "\n".join(logs), None
|
| 120 |
+
|
| 121 |
+
except requests.exceptions.Timeout:
|
| 122 |
+
logs.append("β ScrapingBee request timed out after 180s")
|
| 123 |
+
return "\n".join(logs), None
|
| 124 |
+
except Exception as e:
|
| 125 |
+
logs.append(f"β Error: {e}")
|
| 126 |
+
import traceback
|
| 127 |
+
logs.append(traceback.format_exc())
|
| 128 |
+
return "\n".join(logs), None
|
| 129 |
+
|
| 130 |
+
# Build Gradio Interface
|
| 131 |
+
with gr.Blocks(title="ScrapingBee Login Test", theme=gr.themes.Soft()) as demo:
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
# π§ͺ ScrapingBee Login Test
|
| 134 |
+
|
| 135 |
+
Test ScrapingBee login to Dreamstime and extract cookies.
|
| 136 |
+
""")
|
| 137 |
+
|
| 138 |
+
with gr.Row():
|
| 139 |
+
with gr.Column():
|
| 140 |
+
email = gr.Textbox(label="Dreamstime Email", value="ninjagaming1379@gmail.com")
|
| 141 |
+
password = gr.Textbox(label="Dreamstime Password", type="password", value="ninjagaming1379@gmail.com")
|
| 142 |
+
api_key = gr.Textbox(label="ScrapingBee API Key", value="3HKFQDTMFCS27IOUVCIOVCDOP7EJD4TGC796YS11IAY55VUCNJBEHYBQ0C8D97N8P0RRYW2SZ723B0F6")
|
| 143 |
+
|
| 144 |
+
test_btn = gr.Button("π§ͺ Test Login", variant="primary")
|
| 145 |
+
|
| 146 |
+
with gr.Column():
|
| 147 |
+
logs = gr.Textbox(label="Logs", lines=20, max_lines=20)
|
| 148 |
+
cookies = gr.Textbox(label="Extracted Cookies", lines=5, max_lines=10)
|
| 149 |
+
|
| 150 |
+
test_btn.click(
|
| 151 |
+
test_scrapingbee_login,
|
| 152 |
+
inputs=[email, password, api_key],
|
| 153 |
+
outputs=[logs, cookies]
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|