-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_cookies.py
55 lines (45 loc) · 2.43 KB
/
export_cookies.py
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
import browser_cookie3
import tempfile
import os
def export_cookies():
try:
# Try Chrome first
cookies = browser_cookie3.chrome(domain_name='.youtube.com')
cookies_file = os.path.join(tempfile.gettempdir(), 'youtube_cookies.txt')
with open(cookies_file, 'w', encoding='utf-8') as f:
f.write("# Netscape HTTP Cookie File\n")
f.write("# https://curl.haxx.se/rfc/cookie_spec.html\n")
f.write("# This file was generated by browser_cookie3\n\n")
for cookie in cookies:
if '.youtube.com' in cookie.domain:
secure = "TRUE" if cookie.secure else "FALSE"
http_only = "TRUE" if cookie.has_nonstandard_attr('HttpOnly') else "FALSE"
# Use a far future expiration date
expires = "1735689600" # 2025-01-01
f.write(f"{cookie.domain}\tTRUE\t{cookie.path}\t{secure}\t{expires}\t{cookie.name}\t{cookie.value}\n")
print(f"Cookies saved to: {cookies_file}")
return cookies_file
except Exception as e:
print(f"Error with Chrome: {e}")
try:
# Try Edge if Chrome fails
cookies = browser_cookie3.edge(domain_name='.youtube.com')
cookies_file = os.path.join(tempfile.gettempdir(), 'youtube_cookies.txt')
with open(cookies_file, 'w', encoding='utf-8') as f:
f.write("# Netscape HTTP Cookie File\n")
f.write("# https://curl.haxx.se/rfc/cookie_spec.html\n")
f.write("# This file was generated by browser_cookie3\n\n")
for cookie in cookies:
if '.youtube.com' in cookie.domain:
secure = "TRUE" if cookie.secure else "FALSE"
http_only = "TRUE" if cookie.has_nonstandard_attr('HttpOnly') else "FALSE"
# Use a far future expiration date
expires = "1735689600" # 2025-01-01
f.write(f"{cookie.domain}\tTRUE\t{cookie.path}\t{secure}\t{expires}\t{cookie.name}\t{cookie.value}\n")
print(f"Cookies saved to: {cookies_file}")
return cookies_file
except Exception as e:
print(f"Error with Edge: {e}")
return None
if __name__ == '__main__':
export_cookies()