m3u8-XZ 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
m3u8_XZ-0.0.1/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
m3u8_XZ-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.1
2
+ Name: m3u8_XZ
3
+ Version: 0.0.1
4
+ Summary: download m3u8 video
5
+ Author: XZ
6
+ Author-email: 345841407@qq.com
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires: requests
11
+ Requires: aiohttp
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # XZ_m3u8
16
+
17
+ This is a simple package,it can download video by m3u8 file.
18
+
@@ -0,0 +1,4 @@
1
+ # XZ_m3u8
2
+
3
+ This is a simple package,it can download video by m3u8 file.
4
+
@@ -0,0 +1,7 @@
1
+ # -- coding:utf-8 --
2
+ # Time:2023-03-23 11:47
3
+ # Author:XZ
4
+ # File:_init_.py
5
+ # IED:PyCharm
6
+
7
+ name = 'packaging_m3u8'
@@ -0,0 +1,133 @@
1
+ # -- coding:utf-8 --
2
+ # Time:2023-03-23 10:48
3
+ # Author:XZ
4
+ # File:xz.py
5
+ # IED:PyCharm
6
+ import time
7
+ import aiohttp
8
+ import asyncio
9
+ import requests as req
10
+ import re
11
+ import os
12
+
13
+
14
+ class M3U8:
15
+ """
16
+ url: m3u8文件的url
17
+ folder: 下载文件后存储的名字
18
+ run(): 执行下载
19
+ """
20
+
21
+ def __init__(self, url, folder='test'):
22
+ # 下载文件名
23
+ self.file_name = folder + '.mp4'
24
+ # 下载存储文件夹
25
+ self.path = './down_load/' + folder + '/'
26
+ if not os.path.exists(self.path):
27
+ os.makedirs(self.path)
28
+ # 缓存文件夹
29
+ self.temp_path = self.path + 'temp/'
30
+ if not os.path.exists(self.temp_path):
31
+ os.makedirs(self.temp_path)
32
+ # m3u8
33
+ self.url = url
34
+ self.headers = {
35
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
36
+ }
37
+ # 记录进度
38
+ self.list_length = 0
39
+ self.num = 0
40
+ # 有序存储ts列表文件
41
+ self.ts_list = list()
42
+
43
+ # 通过ts名称,转换ts网址
44
+ @staticmethod
45
+ def get_full_ts_url(url, ts_name):
46
+ # 分割ts name
47
+ tl = ts_name.split('/')
48
+ #
49
+ new_url = []
50
+ # 循环url,去掉ts name中重复的部分
51
+ for s in url.split('/')[:-1]:
52
+ if s in tl:
53
+ tl.remove(s)
54
+ new_url.append(s)
55
+ # 拼接ts name
56
+ new_url.extend(tl)
57
+ result = '/'.join(new_url)
58
+ # 返回
59
+ return result
60
+
61
+ # 通过url,获取ts列表
62
+ def get_ts_list(self) -> list:
63
+ res = req.get(self.url, headers=self.headers, verify=False)
64
+ if res.status_code != 200:
65
+ raise Exception('亲求失败,m3u8地址不存在')
66
+ text = res.text
67
+ # 去掉注释
68
+ ts_str = re.sub('#.*?\n', '', text)
69
+ # 转为列表
70
+ self.ts_list = ts_str.split('\n')
71
+ self.list_length = len(self.ts_list)
72
+ return self.ts_list
73
+
74
+ # 通过ts列表,异步缓存所有ts文件
75
+ async def get_data(self):
76
+ async with aiohttp.ClientSession(headers=self.headers) as session:
77
+ tasks = []
78
+ for index, ts in enumerate(self.get_ts_list()):
79
+ if ts:
80
+ # 给ts重命名,为了排序
81
+ temp_ts = str(index).zfill(6) + '.mp4'
82
+ # 创建task任务
83
+ task = asyncio.create_task(self.down_file(session, self.get_full_ts_url(self.url, ts), temp_ts))
84
+ tasks.append(task)
85
+ # 添加到事件循环
86
+ await asyncio.wait(tasks)
87
+
88
+ # 异步下载二进制文件
89
+ async def down_file(self, session, url, ts_name):
90
+ async with session.get(url) as res:
91
+ data = await res.read()
92
+ with open(self.temp_path + ts_name, 'wb') as f:
93
+ f.write(data)
94
+ # 打印进度
95
+ self.num += 1
96
+ print('\r下载中:{:3.0f}%| {}/{}'.format(self.num / self.list_length * 100, self.num, self.list_length),
97
+ end='', flush=True)
98
+
99
+ # 通过名称按序读取ts文件,整合成一个ts文件
100
+ def combine_ts(self):
101
+ # 获取所有缓存文件
102
+ file_list = os.listdir(self.temp_path)
103
+ if not file_list:
104
+ return
105
+ # 排序
106
+ file_list.sort(key=lambda s: s.split('.')[0])
107
+ # 文件总数
108
+ length = len(file_list)
109
+ # 开始合并文件
110
+ with open(self.path + self.file_name, 'ab') as f:
111
+ # 循环文件列表
112
+ for i, file in enumerate(file_list):
113
+ # 读取每个文件
114
+ with open(self.temp_path + file, 'rb') as rf:
115
+ # 把每个文件的内容 追加到同一个文件
116
+ data = rf.read()
117
+ f.write(data)
118
+ # 清除缓存文件
119
+ os.remove(self.temp_path + file)
120
+ # 打印进度
121
+ print('\r合并中:{:3.0f}%'.format(i / length * 100), end='', flush=True)
122
+
123
+ # 异步启动器
124
+ def run(self):
125
+ if self.url:
126
+ stime = time.time()
127
+ loop = asyncio.get_event_loop()
128
+ loop.run_until_complete(self.get_data())
129
+ print('下载完成,准备合并...')
130
+ time.sleep(2)
131
+ self.combine_ts()
132
+ print('\nover time : ', time.time() - stime)
133
+
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.1
2
+ Name: m3u8-XZ
3
+ Version: 0.0.1
4
+ Summary: download m3u8 video
5
+ Author: XZ
6
+ Author-email: 345841407@qq.com
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires: requests
11
+ Requires: aiohttp
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
15
+ # XZ_m3u8
16
+
17
+ This is a simple package,it can download video by m3u8 file.
18
+
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ setup.py
5
+ m3u8_XZ/__init__.py
6
+ m3u8_XZ/xz.py
7
+ m3u8_XZ.egg-info/PKG-INFO
8
+ m3u8_XZ.egg-info/SOURCES.txt
9
+ m3u8_XZ.egg-info/dependency_links.txt
10
+ m3u8_XZ.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ m3u8_XZ
@@ -0,0 +1,46 @@
1
+ #[build-system]
2
+ #requires = ["setuptools>=61.0"]
3
+ #build-backend = "setuptools.build_meta"
4
+ #
5
+ #[project]
6
+ #name = "m3u8_XZ"
7
+ #version = "0.0.1"
8
+ #authors = [
9
+ # { name="XZ", email="345841407@qq.com" },
10
+ #]
11
+ #description = "download m3u8 video"
12
+ #readme = "README.md"
13
+ #requires-python = ">=3.7"
14
+ #classifiers = [
15
+ # "Programming Language :: Python :: 3",
16
+ # "License :: OSI Approved :: MIT License",
17
+ # "Operating System :: OS Independent",
18
+ #]
19
+ #dependencies = [
20
+ # "requests",
21
+ # 'aiohttp',
22
+ #]
23
+ #
24
+ #[build-system]
25
+ #requires = ["setuptools>=61.0"]
26
+ #build-backend = "setuptools.build_meta"
27
+ #
28
+ #[project]
29
+ #name = "m3u8_XZ"
30
+ #version = "0.0.1"
31
+ #authors = [
32
+ # { name="XZ", email="345841407@qq.com" },
33
+ #]
34
+ #description = "download m3u8 video"
35
+ #readme = "README.md"
36
+ #requires-python = ">=3.7"
37
+ #classifiers = [
38
+ # "Programming Language :: Python :: 3",
39
+ # "License :: OSI Approved :: MIT License",
40
+ # "Operating System :: OS Independent",
41
+ #]
42
+ #dependencies = [
43
+ # "requests",
44
+ # 'aiohttp',
45
+ #]
46
+ #
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
m3u8_XZ-0.0.1/setup.py ADDED
@@ -0,0 +1,26 @@
1
+ # -- coding:utf-8 --
2
+ # Time:2023-03-23 14:44
3
+ # Author:XZ
4
+ # File:setup.py
5
+ # IED:PyCharm
6
+ import setuptools
7
+
8
+ with open("README.md", "r") as fh:
9
+ long_description = fh.read()
10
+
11
+ setuptools.setup(
12
+ name="m3u8_XZ",
13
+ version="0.0.1",
14
+ author="XZ",
15
+ author_email="345841407@qq.com",
16
+ description="download m3u8 video",
17
+ long_description=long_description,
18
+ long_description_content_type="text/markdown",
19
+ packages=setuptools.find_packages(),
20
+ classifiers=[
21
+ "Programming Language :: Python :: 3",
22
+ "License :: OSI Approved :: MIT License",
23
+ "Operating System :: OS Independent",
24
+ ],
25
+ requires=["requests", "aiohttp"],
26
+ )