-
Notifications
You must be signed in to change notification settings - Fork 4
/
save_m3u.py
87 lines (77 loc) · 3.35 KB
/
save_m3u.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
from os import name
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
def getm3u(url, path):
rqs = urllib.request.urlopen(url, timeout=120)
html = rqs.read().decode()
soup = BeautifulSoup(html, 'lxml')
atag = soup.find_all(lambda tag: tag.name == 'a' and tag.has_attr('href') and not tag.has_attr(
'class') and tag.has_attr('rel') and tag.attrs['rel'][0] == 'nofollow')
for a in atag:
m3u_url = a.attrs['href']
if('.m3u' in m3u_url and len(a.attrs['rel']) == 1 and a.attrs['rel'][0] == 'nofollow'):
# m3u_url_arr = m3u_url.split('/')
# file_name = m3u_url_arr[len(m3u_url_arr)-1]
# chinese_file_name = urllib.parse.unquote(file_name)
chinese_file_name = a.text[:-1]
print(m3u_url)
m3u_rqs = urllib.request.urlopen(m3u_url, timeout=120)
if '陕西电信' in chinese_file_name or '贵州联通' in chinese_file_name or '山东联通' in chinese_file_name:
content = m3u_rqs.read().decode(encoding='cp1252')
else:
content = m3u_rqs.read().decode(encoding='utf-8')
with open(path + '\\' + chinese_file_name, 'wt', encoding='utf-8') as f:
f.write(content)
else:
print(m3u_url)
def getm3u_2(url, path):
# index
m3u_rqs = urllib.request.urlopen(
'https://iptv-org.github.io/iptv/index.m3u', timeout=120)
content = m3u_rqs.read().decode()
with open(path + '\\index.m3u', 'wt', encoding='utf-8') as f:
f.write(content)
rqs = urllib.request.urlopen(url, timeout=120)
html = rqs.read().decode()
soup = BeautifulSoup(html, 'lxml')
uls = soup.find_all(lambda tag: tag.name ==
'ul' and not tag.has_attr('class'))
for ul in uls:
codes = ul.find_all(name='code')
if(len(codes) <= 0):
continue
for code in codes:
m3u_url = code.text
m3u_url_arr = m3u_url.split('/')
print(m3u_url)
m3u_rqs = urllib.request.urlopen(m3u_url, timeout=120)
content = m3u_rqs.read().decode()
file_name = m3u_url_arr[len(m3u_url_arr)-1]
with open(path + '\\' + file_name, 'wt', encoding='utf-8') as f:
f.write(content)
tables = soup.find_all(name='table')
for index, table in enumerate(tables):
path_folder = path + '\\Category' if index == 0 else (
path + '\\Language' if index == 1 else path + '\\Country')
thead = table.find(name='thead')
tbody = table.find(name='tbody')
for tr in tbody.find_all(name='tr'):
tds = tr.find_all(name='td')
title = tds[0].text
count = tds[1].text
m3u_url = tds[2].text
m3u_url_arr = m3u_url.split('/')
file_name = m3u_url_arr[len(m3u_url_arr)-1]
print(m3u_url)
m3u_rqs = urllib.request.urlopen(m3u_url, timeout=120)
content = m3u_rqs.read().decode()
file_path = path_folder + '\\' + title + '_' + count + '_' + file_name
with open(file_path, 'wt', encoding='utf-8') as f:
f.write(content)
url = 'https://github.com/imDazui/Tvlist-awesome-m3u-m3u8'
path = r'd:\kodi\China'
getm3u(url, path)
url = 'https://github.com/iptv-org/iptv'
path = r'd:\kodi'
getm3u_2(url, path)