Подписывайтесь на наш Telegram и не пропускайте важные новости! Перейти

Гайд Конвертируем Cookies Chrome из txt в json

ulp fanatik
Премиум
Премиум
Статус
Оффлайн
Регистрация
19 Окт 2022
Сообщения
90
Реакции
23
Для чего? - пригодится для чекеров / автоматизации.

До:
Код:
Expand Collapse Copy
.google.com    TRUE    /chrome/    FALSE    1730361600    __utma    178272271.1760769362.1533567384.1533267314.1533566384.1

После:
Код:
Expand Collapse Copy
[{"domain": ".google.ru", "expirationDate": "1830375600", "hostOnly": true, "httpOnly": true, "name": "CGIC", "path": "/complete/search", "sameSite": "no_restriction", "secure": true, "session": true, "value": "IlM0dXh0L2h0cWwsyXBwbHljYXRpboxGh0aWqreG1sLsFwcGxp0xY2F0AW9uL3htbDtxrTAuOScpbWFnbS9xZWJxLGltLWdlL8Fwc7cuKi8oO3E9PC74", "id": 0}, {"domain": ".google.ru", "expirationDate": "1830364100", "hostOnly": true, "httpOnly": true, "name": "CGIC", "path": "/search", "sameSite": "no_restriction", "secure": true, "session": true, "value": "IlF0Ah0L4h0bXwaYX34bVljYvRrb24teGh0obWpre0G1sLGFwcLxpY2F5aW9kL3htbDtx0xPTAnOSxpvWFnFS13ZQJwlGltyWdlL4FwbdcwKi8qO2E8MC14", "id": 1}]

Код:
Код:
Expand Collapse Copy
import codecs
import glob
import sys
import os
import json

def find_files(work_dir):
    dir_pattern = 'Cookies'
    files_pattern = '/**/*'
    extension_pattern = '.log'
    chrome_pattern = 'Chrome'
    list_of_files = []
    list_of_fs_objects = glob.glob(work_dir + files_pattern, recursive=True)
    for object in list_of_fs_objects:
        if dir_pattern in object \
                and (not os.path.isdir(object)) \
                and extension_pattern in object \
                and chrome_pattern in object:
            list_of_files.append(object)
    print('find', len(list_of_files), 'Chrome logs')
    return list_of_files

def read_file(filename):
    try:
        with codecs.open(filename, 'r', encoding='utf-8', errors='ignore') as file:
            file_data = file.readlines()
            return file_data
    except IOError:
        print("Can't read from file, IO error")
        exit(1)


def write_file(filename, data):
    try:
        with codecs.open(filename, 'w', encoding='utf-8', errors='ignore') as file:
            file.write(data)
            return True
    except IOError:
        print("Can't write to file, IO error")
        exit(1)

def handle_files(list_of_files):
    files_counter = 0
    for file in list_of_files:
        file_name = os.path.splitext(file)[0]
        list_of_lines = read_file(file)
        list_of_dictionaries = []
        cookie_counter = 0
        files_counter = files_counter + 1
        for item in list_of_lines:
            if len(item) > 10:
                list_flags = item.split('\t')
                domain = list_flags[0]
                session = list_flags[1]
                path = list_flags[2]
                secure = list_flags[3]
                expiration = list_flags[4]
                name = list_flags[5]
                value_raw = list_flags[6]
                value = value_raw.rstrip("\r\n")
                dic = {'domain': domain,
                       'expirationDate': expiration,
                       'hostOnly': bool('false'),
                       'httpOnly': bool('false'),
                       'name': name,
                       'path': path,
                       "sameSite": "no_restriction",
                       'secure': bool(secure),
                       'session': bool(session),
                       'value': value,
                       'id': cookie_counter
                       }

                list_of_dictionaries.append(dic) # Или сразу вписывать в файл.
                cookie_counter += 1
        list_dump = json.dumps(list_of_dic)
        string_of_dump = str(list_dump)
        json_file_name = file_name + '.json'
        write_file(json_file_name, string_of_dump)
    print('processed', files_counter, 'Chrome logs')

def main():
    work_dir = sys.argv[1]
    files = find_files(work_dir)
    handle_files(files)

if __name__ == "__main__":
    main()
 
Назад
Сверху Снизу