forked from EvilCult/iptv-m3u-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
162 lines (131 loc) · 4.62 KB
/
tools.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse
import urllib.error
import re
import ssl
import io
import gzip
import random
import socket
import time
import area
socket.setdefaulttimeout(5.0)
class Tools (object) :
def __init__ (self) :
pass
def getPage (self, url, requestHeader = [], postData = {}) :
fakeIp = self.fakeIp()
requestHeader.append('CLIENT-IP:' + fakeIp)
requestHeader.append('X-FORWARDED-FOR:' + fakeIp)
if postData == {} :
request = urllib.request.Request(url)
elif isinstance(postData, str) :
request = urllib.request.Request(url, postData)
else :
request = urllib.request.Request(url, urllib.parse.urlencode(postData).encode('utf-8'))
for x in requestHeader :
headerType = x.split(':')[0]
headerCon = x.replace(headerType + ':', '')
request.add_header(headerType, headerCon)
try :
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urllib.request.urlopen(request, context = ctx)
header = response.headers
body = response.read().decode('utf-8')
code = response.code
except urllib.error.HTTPError as e:
header = e.headers
body = e.read().decode('utf-8')
code = e.code
result = {
'code': code,
'header': header,
'body': body
}
return result
def fakeIp (self) :
fakeIpList = []
for x in range(0, 4):
fakeIpList.append(str(int(random.uniform(0, 255))))
fakeIp = '.'.join(fakeIpList)
return fakeIp
def fmtCookie (self, string) :
result = re.sub(r"path\=\/.", "", string)
result = re.sub(r"(\S*?)\=deleted.", "", result)
result = re.sub(r"expires\=(.*?)GMT;", "", result)
result = re.sub(r"domain\=(.*?)tv.", "", result)
result = re.sub(r"httponly", "", result)
result = re.sub(r"\s", "", result)
return result
def urlencode(self, str) :
reprStr = repr(str).replace(r'\x', '%')
return reprStr[1:-1]
def gzdecode(self, data) :
try:
compressedstream = io.StringIO(data)
gziper = gzip.GzipFile(fileobj = compressedstream)
html = gziper.read()
return html
except :
return data
def fmtTitle (self, string) :
pattern = re.compile(r"(cctv[-|\s]*\d*)?(.*)", re.I)
tmp = pattern.findall(string)
channelId = tmp[0][0].strip('-').strip()
channeTitle = tmp[0][1]
channeTitle = channeTitle.replace('.m3u8', '')
pattern = re.compile(r"<.*?>", re.I)
channeTitle = re.sub(pattern, "", channeTitle)
pattern = re.compile(r"(fhd|hd|sd)", re.I)
tmp = pattern.findall(channeTitle)
quality = ''
if len(tmp) > 0 :
quality = tmp[0]
channeTitle = channeTitle.replace(tmp[0], '')
result = {
'id' : channelId,
'title' : channeTitle.strip('-').strip(),
'quality': quality.strip('-').strip(),
'level' : 4,
}
if result['id'] != '':
pattern = re.compile(r"cctv[-|\s]*(\d*)", re.I)
result['id'] = re.sub(pattern, "CCTV-\\1", result['id'])
if '+' in result['title'] :
result['id'] = result['id'] + str('+')
pattern = re.compile(r"\[\d+\*\d+\]", re.I)
result['title'] = re.sub(pattern, "", result['title'])
Area = area.Area()
result['level'] = Area.classify(str(result['id']) + str(result['title']))
# Radio
pattern = re.compile(r"(radio|fm)", re.I)
tmp = pattern.findall(result['title'])
if len(tmp) > 0 :
result['level'] = 7
return result
def chkPlayable (self, url) :
try:
startTime = int(round(time.time() * 1000))
code = urllib.request.urlopen(url).getcode()
if code == 200 :
endTime = int(round(time.time() * 1000))
useTime = endTime - startTime
return int(useTime)
else:
return 0
except:
return 0
def chkCros (self, url) :
return 0
# try:
# res = urllib.request.urlopen(url).getheader('Access-Control-Allow-Origin')
# if res == '*' :
# return True
# else :
# return False
# except:
# return 0