-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownLoader.py
More file actions
75 lines (63 loc) · 2.12 KB
/
DownLoader.py
File metadata and controls
75 lines (63 loc) · 2.12 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
import os
import requests
from abc import abstractmethod
from tqdm import tqdm
from Myrequests import SakulaReq
class Downloader:
def __init__(self) -> None:
pass
@abstractmethod
def start():
pass
class M3u8Downloader(Downloader):
def __init__(self, fileName) -> None:
if not os.path.exists("./m3u8"):
os.mkdir("./m3u8")
self.filePath = "./m3u8/" + fileName.split("/")[-1]
self.url = (
fileName
if fileName.startswith("http")
else "https://tup.yinghuacd.com/feifan/" + fileName
)
def readLinks(self):
with open(self.filePath, "r") as f:
lines = f.readlines()
links = [line[:-1] for line in lines if line[:6] == "https:"]
return links
def getData(self):
if not os.path.exists(self.filePath):
res = requests.get(self.url)
with open(self.filePath, "wb") as f:
f.write(res.content)
return self.readLinks()
def start(self, filePath):
SakulaRequest = SakulaReq()
links = self.getData()
for link in tqdm(links):
ts = SakulaRequest.Request(
method="GET", url=link, data=None, headers=None, verify=False
)
if ts["suc"]:
with open("{}".format(filePath), "ab") as f:
f.write(ts["data"].content)
else:
ts = SakulaRequest.Request(
method="GET", url=link, data=None, headers=None, verify=False
)
if ts["suc"]:
with open("{}".format(filePath), "ab") as f:
f.write(ts["data"].content)
else:
print(ts)
class DirectDownloader(Downloader):
def __init__(self, url) -> None:
self.link = url
def start(self):
ts = SakulaReq.Request(
method="GET", url=self.link, data=None, headers=None, verify=False
)
if ts["suc"]:
with open("file.ts", "ab") as f:
f.write(ts["data"].content)
else:
print(ts)