-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie_manager.py
More file actions
96 lines (78 loc) · 3.2 KB
/
Copy pathcookie_manager.py
File metadata and controls
96 lines (78 loc) · 3.2 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
import os
import sys
import json
import time
# Ensure parent directory is in sys.path for parent level imports
PARENT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if PARENT_DIR not in sys.path:
sys.path.append(PARENT_DIR)
from chrome_launch import launch_browser
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # server_code/web_agent
COOKIES_FILE = os.path.join(BASE_DIR, "saved_cookies.json")
if not os.path.exists(COOKIES_FILE):
# Fallback to parent directory (server_code)
COOKIES_FILE = os.path.join(PARENT_DIR, "saved_cookies.json")
TARGET_SITES = {
"pinterest": "https://www.pinterest.com/",
"linkedin": "https://www.linkedin.com/",
"facebook": "https://www.facebook.com/",
"amazon": "https://www.amazon.in/",
"tumblr": "https://www.tumblr.com/"
}
def export_cookies():
"""
Tumhare existing 'ChromeProfile' ko open karke, bari-bari websites pe jayega
aur wahan se decrypted cookies nikal kar JSON me save karega.
"""
print("🚀 Browser launch kar raha hoon (Cookies nikalne ke liye)...")
driver = launch_browser(profile_name="Default", headless=False)
all_cookies = {}
try:
for site_name, url in TARGET_SITES.items():
print(f"\n🌐 Visiting {site_name} ({url}) to extract cookies...")
driver.get(url)
time.sleep(5)
cookies = driver.get_cookies()
if cookies:
all_cookies[site_name] = cookies
print(f"Base: visited and extracted {len(cookies)} cookies for {site_name}")
else:
print(f"⚠️ {site_name} pe koi cookies nahi mili. Sayad logged in nahi ho?")
# Save to resolved cookies file location
with open(COOKIES_FILE, "w", encoding="utf-8") as f:
json.dump(all_cookies, f, indent=4)
print(f"\n🎉 Saari cookies '{COOKIES_FILE}' mein successfully save ho gayi hain!")
except Exception as e:
print(f"❌ Error aaya: {e}")
finally:
print("🛑 Browser band kar rahe hain...")
driver.quit()
def inject_cookies(driver, site_name):
"""
Kissi naye PC ya naye browser me cookies dalne ke liye is function ka use hoga.
"""
if not os.path.exists(COOKIES_FILE):
print(f"⚠️ {COOKIES_FILE} nahi mili! Pehle cookies export karo.")
return False
with open(COOKIES_FILE, "r", encoding="utf-8") as f:
all_cookies = json.load(f)
if site_name not in all_cookies:
print(f"⚠️ '{site_name}' ki cookies file mein nahi hain.")
return False
print(f"💉 {site_name} ki cookies inject kar rahe hain...")
success_count = 0
for cookie in all_cookies[site_name]:
try:
driver.add_cookie(cookie)
success_count += 1
except Exception as e:
pass
print(f"✅ {success_count} cookies inject ho gayi! Page refresh kar raha hoon...")
driver.refresh()
return True
if __name__ == "__main__":
print("=== COOKIE MANAGER ===")
print("1. Extract & Save Cookies (Apne main PC pe chalayein)")
choice = input("Enter choice (1): ").strip()
if choice == "1":
export_cookies()