CCCPaste

Auto open sites

import webbrowser
import os
import json

from tkinter import *


def write_json_file(path_to_file: str, data: dict) -> None:
    try:
        with open(f'{create_path_if_not_found(path_to_file)}/links.json', 'r') as json_file:
            json_data = json.load(json_file)

            for key in data:
                json_data[key] = data[key]

            json.dump(json_data, open(f'{create_path_if_not_found(path_to_file)}/links.json', 'w'))
    except FileNotFoundError:
        default_links = {
            'youtube': 'https://www.youtube.com/',
            'rutube': 'https://rutube.ru/',
            'yandex': 'https://yandex.ru/',
            'lamoda': 'https://www.lamoda.ru/',
            'ozon': 'https://www.ozon.ru/',
            'wildberries': 'https://www.wildberries.ru/'
        }

        with open(f'{create_path_if_not_found(path_to_file)}/links.json', 'w') as json_file:
            json.dump(default_links, json_file)
    finally:
        print('Сайт добавлен.')


def read_json_file(path_to_file: str) -> None:
    try:
        with open(f'{path_to_file}/links.json') as json_file:
            data = json.load(json_file)
            for name_site, link_to_site in data.items():
                yield name_site, link_to_site
    except FileNotFoundError as e_fnf:
        print(f'Ошибка! Подробнее: {e_fnf}')


def create_path_if_not_found(path_to_file: str) -> str:
    if not os.path.exists(f'{path_to_file}'):
        os.mkdir(f'{path_to_file}')

    return path_to_file


def add_site():
    dict_link = {
        txt_name.get(): txt_link.get()
    }
    write_json_file('sites', dict_link)


def start_sites():
    for name_site, link_to_site in read_json_file('sites'):
        webbrowser.open(link_to_site, new=2)
        print(f'Сайт {name_site} загружается! Ссылка: {link_to_site}')


def get_list_sites():
    print('Список сайтов: ')

    for name_site, link_to_site in read_json_file('sites'):
        print(f'\tИмя сайта {name_site} Ссылка: {link_to_site}')


def clear_list_sites():
    if input('Вы уверены, что хотите удалить все сайты? Нажмите 1, если да, и любую кнопку, если нет. \n>>>') == '1':
        try:
            path = os.path.join(str(os.path.abspath(os.path.dirname(__file__))) + '/sites', 'links.json')
            os.remove(path)
        except FileNotFoundError:
            pass
        finally:
            print('Список очищен.')


window = Tk()
window.title("Добро пожаловать в приложение PythonRu")
window.geometry('1024x720')

lbl_name = Label(window, text="Название сайта.")
lbl_name.grid(column=0, row=0)
txt_name = Entry(window, width=10)
txt_name.grid(column=1, row=0)

lbl_link = Label(window, text="Ссылка на сайт.")
lbl_link.grid(column=0, row=1)
txt_link = Entry(window, width=10)
txt_link.grid(column=1, row=1)

btn_add = Button(window, text="Добавить сайт в список.", command=add_site)
btn_add.grid(column=0, row=3)

btn_start = Button(window, text="Запустить сайты.", command=start_sites)
btn_start.grid(column=0, row=4)

btn_get_sites = Button(window, text="Получить список сайтов.", command=get_list_sites)
btn_get_sites.grid(column=0, row=5)

btn_get_sites = Button(window, text="Очистить список сайтов.", command=clear_list_sites)
btn_get_sites.grid(column=0, row=6)


window.mainloop()