-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadFile.py
More file actions
123 lines (116 loc) · 4.25 KB
/
DownloadFile.py
File metadata and controls
123 lines (116 loc) · 4.25 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
import logging
import shutil
import time
import gzip
import sys
import os
"""
Скрипт скачивает ЕПГ по ссылке с сайта
и разархивирует его в указанную директорию
"""
url = 'https://iptvx.one/EPG'
epg_path = 'D:\\EPG'
archive_path = 'C:\\Users\\evandrushenko\\Downloads\\epg.xml.gz'
def Remove(EnterPath, EnterFile):
path = os.path.join(os.path.abspath(\
os.path.dirname(EnterPath)), EnterFile)
os.remove(path)
def Download(url_, path_):
try:
r = requests.get(url_)
if r.status_code != 200:
result = False
else:
with open(path_, 'wb') as out:
try:
out.write(r.content)
except (Exception, IOError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
log.info('Архив с EPG загружен')
result = True
except (Exception, OSError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
finally:
return result
def Extract_archive(url_, download_path, file_path):
if Download(url_, download_path):
try:
with gzip.open(download_path, 'rb') as archive_,\
open(file_path, 'wb') as file_:
shutil.copyfileobj(archive_, file_)
except (Exception, OSError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
log.info('Файл с EPG извлечён из архива')
else:
log.info('Ошибка при загрузке файла с ЕПГ')
def MainFunction():
try:
exist_archive = os.path.isfile(archive_path)
full_path = '\\'.join([epg_path, 'epg.xml'])
exist_file = os.path.isfile(full_path)
result = False
except (Exception, OSError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
if exist_archive and exist_file:
try:
Remove(archive_path, EnterFile = 'epg.xml.gz')
for file in os.scandir(epg_path):
os.remove(file.path)
Extract_archive(url, archive_path, full_path)
except (Exception, OSError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
result = True
elif exist_file and not exist_archive:
try:
for file in os.scandir(epg_path):
os.remove(file.path)
Extract_archive(url, archive_path, full_path)
except (Exception, OSError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
result = True
elif exist_archive and not exist_file:
try:
Remove(archive_path, EnterFile = 'epg.xml.gz')
Extract_archive(url, archive_path, full_path)
except (Exception, OSError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
result = True
else:
Extract_archive(url, archive_path, full_path)
result = True
finally:
yield result
if __name__ == '__main__':
try:
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
f_format = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
handler.setFormatter(f_format)
log.addHandler(handler)
except Exception as exc:
print(exc)
time.sleep(20)
else:
MF = MainFunction()
if next(MF):
os.startfile('D:/Network/КИТ Интернет телепрограмма/КИТ Интернет телепрограмма.exe')
input("Нажмите Enter для выхода.")
else:
log.info('Ошибка')
time.sleep(20)