Skip to content

Commit

Permalink
Detects json cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
matheeshapathirana committed May 3, 2024
1 parent e2fabe6 commit b0c66f4
Showing 1 changed file with 68 additions and 76 deletions.
144 changes: 68 additions & 76 deletions cookie_converter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
import json
import os
import random
import shutil
import sys
from colorama import Fore


def identify_file(file_name):
try:
with open(file_name, 'r') as file_content:
# Try to parse the file as JSON
json.load(file_content)
return "json"
except json.JSONDecodeError:
# If it fails to parse as JSON, assume it's a Netscape file
return "netscape"
except Exception as e:
# If any other error occurred, print it
print(f"An error occurred while processing {file_name}: {str(e)}")
return "error"


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].replace("www", ""),
"flag": fields[1],
"path": fields[2],
"secure": fields[3] == "TRUE",
"expiration": fields[4],
"name": fields[5],
"value": fields[6],
}
cookies.append(cookie)

json_content = json.dumps(cookies, indent=4)
return json_content


no_of_cookies = 0

try:
if os.name == "posix":
folder_path = "cookies"

else:
while True:
import tkinter
Expand Down Expand Up @@ -43,90 +77,48 @@
else:
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].replace("www", ""),
"flag": fields[1],
"path": fields[2],
"secure": fields[3] == "TRUE",
"expiration": fields[4],
"name": fields[5],
"value": fields[6],
}
cookies.append(cookie)

json_content = json.dumps(cookies, indent=4)
return json_content

path = "json_cookies"
try:
os.mkdir(path)
print(Fore.RED + f"Folder {path} created!\n" + Fore.RESET)
try:
for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
if os.path.isfile(filepath):
with open(filepath, "r", encoding="utf-8") as file:
content = file.read()

json_data = convert_netscape_cookie_to_json(content)

with open(f"json_cookies/{filename}", "w", encoding="utf-8") as f:
f.write(json_data)
print(Fore.GREEN + f"[✔️] {filename} - DONE!" + Fore.RESET)
no_of_cookies += 1

except FileNotFoundError:
print(
Fore.RED
+ "[⚠️] Error Occurred: Default 'cookies' folder not found, please select a valid folder"
+ Fore.RESET
)
os.rmdir(path)

except FileExistsError:
if (
input(
Fore.YELLOW
+ "Do you want to remove old cookies folder? (y/n)\n [y] Recommended \n [n] New cookies will be appended > : "
+ Fore.RESET
)
== "y"
input(
Fore.YELLOW
+ "Do you want to remove old cookies folder? (y/n)\n [y] Recommended \n [n] New cookies will be appended > : "
+ Fore.RESET
)
== "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", encoding="utf-8") as file:
content = file.read()

json_data = convert_netscape_cookie_to_json(content)

with open(f"json_cookies/{filename}", "w", encoding="utf-8") as f:
f.write(json_data)
print(Fore.GREEN + f"[✔️] {filename} - DONE!" + Fore.RESET)
no_of_cookies += 1

else:
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)

with open(f"json_cookies/{filename}", "w", encoding="utf-8") as f:
f.write(json_data)
print(Fore.GREEN + f"[✔️] {filename} - DONE!" + Fore.RESET)
no_of_cookies += 1
print(
Fore.RED
+ "[⚠️] Error Occurred: Failed to create 'json_cookies' folder, Exiting..."
+ Fore.RESET
)
sys.exit()

for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
if os.path.isfile(filepath):
file_type = identify_file(filepath)
if file_type == "json":
shutil.copy(filepath, os.path.join(path, filename))
print(Fore.GREEN + f"[✔️] {filename} - Copied to 'json_cookies' folder!" + Fore.RESET)
elif file_type == "netscape":
with open(filepath, "r", encoding="utf-8") as file:
content = file.read()

json_data = convert_netscape_cookie_to_json(content)

with open(f"json_cookies/{filename}", "w", encoding="utf-8") as f:
f.write(json_data)
print(Fore.GREEN + f"[✔️] {filename} - DONE!" + Fore.RESET)
no_of_cookies += 1
else:
print(Fore.RED + f"[⚠️] {filename} - Error: File type could not be identified!" + Fore.RESET)

print(
Fore.YELLOW
Expand Down

0 comments on commit b0c66f4

Please sign in to comment.