-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcookie_converter.py
83 lines (68 loc) · 2.57 KB
/
cookie_converter.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
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
import json
import os
import random
import tkinter
from tkinter import filedialog
import shutil
while True:
print("\n<<< Select Netscape cookies folder >>>\n\n")
tkinter.Tk().withdraw()
folder_path = filedialog.askdirectory()
if folder_path != '':
break
rand_number = random.randint(1, 99999)
def convert_netscape_cookie_to_json(cookie_file_content):
cookies = []
for line in cookie_file_content.splitlines():
fields = line.strip().split('\t')
if len(fields) >= 7:
cookie = {
'domain': fields[0],
'flag': fields[1],
'path': fields[2],
'secure': fields[3] == 'TRUE',
'expiration': fields[4],
'name': fields[5],
'value': fields[6]
}
cookies.append(cookie)
json_data = json.dumps(cookies, indent=4)
return json_data
path = 'json_cookies'
try:
os.mkdir(path)
print("Folder %s created!" % path)
for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
if os.path.isfile(filepath):
with open(filepath, 'r') as file:
content = file.read()
json_data = convert_netscape_cookie_to_json(content)
f = open(f"json_cookies/{filename}", 'w')
f.write(json_data)
print(f'{filename} - DONE!')
except FileExistsError:
if input("Do you want to remove old cookies folder? (y/n)\n [y] Recommended \n > : ") == 'y':
shutil.rmtree(path)
os.mkdir(path)
for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
if os.path.isfile(filepath):
with open(filepath, 'r') as file:
content = file.read()
json_data = convert_netscape_cookie_to_json(content)
f = open(f"json_cookies/{filename}", 'w')
f.write(json_data)
print(f'{filename} - DONE!')
else:
os.mkdir(str(f"temp {rand_number}"))
for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
if os.path.isfile(filepath):
with open(filepath, 'r') as file:
content = file.read()
json_data = convert_netscape_cookie_to_json(content)
f = open(f"temp {rand_number}/{filename}", 'w')
f.write(json_data)
print(f'{filename} - DONE!')
print(f"\n\nsaved cookies to the temp folder - temp {rand_number}")