import requests
import time
import sys
filename = "index.m3u8"
outputfile = "output.ts"
header = {
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.9",
"Origin": "https://recu.me",
"Sec-Ch-Ua": '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"Windows"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
}
class average:
array = []
pos = 0
size = 0
def __init__(self, size):
self.size = size
def add(self, value):
if self.pos >= self.size:
self.pos = 0
while len(self.array) <= self.pos:
self.array.append(value)
self.array[self.pos] = value
self.pos += 1
def average(self):
return sum(self.array)/len(self.array)
def download(link,header):
try:
data = requests.get(link, headers=header)
if data.status_code != 200:
return data.text, data.status_code
return data.content, None
except KeyboardInterrupt:
exit()
except:
return None, sys.exc_info()
with open(filename, 'r') as file:
indexfile = file.read()
index = indexfile.split('\n')
append = False
length = len(index)
buffer = average(25)
for i in range(length):
if index[i] != "" and index[i][0] != '#':
trys = 0
while True:
if trys > 100:
print(data)
if type(err) == int:
print(f"Status Code: {err}")
print("Either Download Expired or header formating is wrong")
exit()
print(err)
print(f"Error Downloading {(index[i]-8)/2}.ts ")
exit()
start = time.time()
data, err = download(index[i], header)
stop = time.time()
if err == None:
break
trys += 1
print("\rRetrying...",end="")
if not append:
write = 'wb'
append = True
else:
write = 'ab'
with open(outputfile+".ts", write) as file:
file.write(data)
data = None
buffer.add((stop-start)*(length-i)/2/60)
fill = " "
eta = buffer.average()
unit = "mins"
if eta > 1440:
eta = eta/1440
unit = "days"
if eta > 60:
eta = eta/60
unit = "hrs"
if eta < 1:
eta = eta *60
unit = "secs"
print(f"\rDownloaded: {(i/length*100):.1f}%\tRemaining: {eta:.1f} {unit}{fill}",end= "")
print(f"\rFinished Download{fill}{fill}")