Advertisement
BERKYT

Auto open sites

Apr 7th, 2022
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. import webbrowser
  2. import os
  3. import json
  4.  
  5. from tkinter import *
  6.  
  7.  
  8. def write_json_file(path_to_file: str, data: dict) -> None:
  9.     try:
  10.         with open(f'{create_path_if_not_found(path_to_file)}/links.json', 'r') as json_file:
  11.             json_data = json.load(json_file)
  12.  
  13.             for key in data:
  14.                 json_data[key] = data[key]
  15.  
  16.             json.dump(json_data, open(f'{create_path_if_not_found(path_to_file)}/links.json', 'w'))
  17.     except FileNotFoundError:
  18.         default_links = {
  19.             'youtube': 'https://www.youtube.com/',
  20.             'rutube': 'https://rutube.ru/',
  21.             'yandex': 'https://yandex.ru/',
  22.             'lamoda': 'https://www.lamoda.ru/',
  23.             'ozon': 'https://www.ozon.ru/',
  24.             'wildberries': 'https://www.wildberries.ru/'
  25.         }
  26.  
  27.         with open(f'{create_path_if_not_found(path_to_file)}/links.json', 'w') as json_file:
  28.             json.dump(default_links, json_file)
  29.     finally:
  30.         print('Сайт добавлен.')
  31.  
  32.  
  33. def read_json_file(path_to_file: str) -> None:
  34.     try:
  35.         with open(f'{path_to_file}/links.json') as json_file:
  36.             data = json.load(json_file)
  37.             for name_site, link_to_site in data.items():
  38.                 yield name_site, link_to_site
  39.     except FileNotFoundError as e_fnf:
  40.         print(f'Ошибка! Подробнее: {e_fnf}')
  41.  
  42.  
  43. def create_path_if_not_found(path_to_file: str) -> str:
  44.     if not os.path.exists(f'{path_to_file}'):
  45.         os.mkdir(f'{path_to_file}')
  46.  
  47.     return path_to_file
  48.  
  49.  
  50. def add_site():
  51.     dict_link = {
  52.         txt_name.get(): txt_link.get()
  53.     }
  54.     write_json_file('sites', dict_link)
  55.  
  56.  
  57. def start_sites():
  58.     for name_site, link_to_site in read_json_file('sites'):
  59.         webbrowser.open(link_to_site, new=2)
  60.         print(f'Сайт {name_site} загружается! Ссылка: {link_to_site}')
  61.  
  62.  
  63. def get_list_sites():
  64.     print('Список сайтов: ')
  65.  
  66.     for name_site, link_to_site in read_json_file('sites'):
  67.         print(f'\tИмя сайта {name_site} Ссылка: {link_to_site}')
  68.  
  69.  
  70. def clear_list_sites():
  71.     if input('Вы уверены, что хотите удалить все сайты? Нажмите 1, если да, и любую кнопку, если нет. \n>>>') == '1':
  72.         try:
  73.             path = os.path.join(str(os.path.abspath(os.path.dirname(__file__))) + '/sites', 'links.json')
  74.             os.remove(path)
  75.         except FileNotFoundError:
  76.             pass
  77.         finally:
  78.             print('Список очищен.')
  79.  
  80.  
  81. window = Tk()
  82. window.title("Добро пожаловать в приложение PythonRu")
  83. window.geometry('1024x720')
  84.  
  85. lbl_name = Label(window, text="Название сайта.")
  86. lbl_name.grid(column=0, row=0)
  87. txt_name = Entry(window, width=10)
  88. txt_name.grid(column=1, row=0)
  89.  
  90. lbl_link = Label(window, text="Ссылка на сайт.")
  91. lbl_link.grid(column=0, row=1)
  92. txt_link = Entry(window, width=10)
  93. txt_link.grid(column=1, row=1)
  94.  
  95. btn_add = Button(window, text="Добавить сайт в список.", command=add_site)
  96. btn_add.grid(column=0, row=3)
  97.  
  98. btn_start = Button(window, text="Запустить сайты.", command=start_sites)
  99. btn_start.grid(column=0, row=4)
  100.  
  101. btn_get_sites = Button(window, text="Получить список сайтов.", command=get_list_sites)
  102. btn_get_sites.grid(column=0, row=5)
  103.  
  104. btn_get_sites = Button(window, text="Очистить список сайтов.", command=clear_list_sites)
  105. btn_get_sites.grid(column=0, row=6)
  106.  
  107.  
  108. window.mainloop()
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement