-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
executable file
·146 lines (132 loc) · 4.39 KB
/
main.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3
# Purpose:
# Usage:
# Author: Timmy93
# Date:
# Version:
# Disclaimer:
from M3uParser import M3uParser
from RememberFile import RememberFile
import configparser
import subprocess
import os
import datetime
import logging
import yaml
#Rename all the file in this directory
def rename(src, dst, oldName, newName):
filename, file_extension = os.path.splitext(oldName)
os.rename(src+oldName, dst+newName+file_extension)
#Downlad the file in a certain directory - Return True if correctly downloaded
def startDownload(downloader, url, temp, completed):
#Start the download of a certain file using an sh script
res = subprocess.run([downloader, url, temp, completed], stdout=subprocess.PIPE)
return res.returncode == 1
#Get time elapsed between two time
def time_in_range(start, end):
"""Return true if x is in the range [start, end]"""
now = datetime.datetime.now()
h = now.hour
m = now.minute
x = datetime.time(int(h), int(m), 0)
#Set time as datetime
h,m = start.split(":")
start = datetime.time(int(h), int(m), 0)
h,m = end.split(":")
end = datetime.time(int(h), int(m), 0)
#Start comparison
if start <= end:
return start <= x <= end
else:
return start <= x or x <= end
def debugTypes(myFile):
old = ""
for file in myFile.getList():
now = file["tvg-group"]
if old != now:
print(now)
old = now
#Check if the given path is an absolute path
def createAbsolutePath(path):
if not os.path.isabs(path):
currentDir = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(currentDir, path)
return path
def main():
configFile = "config.yml"
logFile = "M3uParser.log"
#Set logging file
logging.basicConfig(filename=createAbsolutePath(logFile),level=logging.ERROR,format='%(asctime)s %(levelname)-8s %(message)s')
#Load config
with open(createAbsolutePath(configFile), 'r') as stream:
try:
config = yaml.safe_load(stream)
logging.getLogger().setLevel(config['Settings']['logLevel'])
logging.info('Loaded settings started')
except yaml.YAMLError as exc:
print("Cannot load file: ["+configFile+"] - Error: "+exc)
logging.error("Cannot load file: ["+configFile+"] - Error: "+exc)
exit()
#Start parser
myFile = M3uParser(logging)
myFile.downloadM3u(config['Settings']['url'], config['Settings']['filename'])
logging.info('Downloaded m3u file')
#Set filters
#Remove extensions
for val in config['ExtensionFilterOut']['value']:
myFile.filterOutFilesEndingWith(val)
#Remove groups
for val in config['GroupFilterOut']['value']:
myFile.filterOutFilesOfGroupsContaining(val)
#Select extensions
myFile.filterInFilesEndingWith(config['ExtensionFilterIn']['value'])
#Select groups
myFile.filterInFilesOfGroupsContaining(config['GroupFilterIn']['value'])
#Create DB
db = RememberFile(createAbsolutePath(config['Download']['db']))
logging.info('DB file found')
logging.info("File left after filtering: "+str(len(myFile.getList())))
while len(myFile.getList()) and time_in_range(
config['Time']['start_time'],
config['Time']['end_time']
):
#Extract file
file = myFile.getFile(config['Download']['shuffle'])
#Check if it is a new file
if db.isAlreadyDownloaded(file["title"]):
logging.debug("Skip file already downloaded: "+file["title"])
continue
#Download file
if startDownload(
createAbsolutePath(config['Download']['downloader']),
file["link"],
createAbsolutePath(config['Download']['temp_path']),
createAbsolutePath(config['Download']['completed'])
):
try:
#Move renamed file
rename(
createAbsolutePath(config['Rename']['source_to_rename']),
createAbsolutePath(config['Rename']['new_dir']),
file["titleFile"],
file["title"]
)
#Save that the file has been renamed
db.appendTitle(file["title"])
logging.info("Downloaded: "+file["title"])
except FileNotFoundError:
logging.error("Error: No file found, "+file["titleFile"]+" ("+file["titleFile"]+") not found")
else:
logging.warning("Problem downloading: "+file["title"]+" - No time to download or no space in the disk")
if not len(myFile.getList()):
print("Downloaded every file")
logging.info("STOP: No more file to download")
elif not time_in_range(
config['Time']['start_time'],
config['Time']['end_time']
):
print("It is not time to download")
logging.info("STOP: Out of download time range")
else:
logging.warning("STOP: Unexpected stop")
main()