forked from EvilCult/iptv-m3u-maker
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
128 lines (103 loc) · 3.43 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tools
import db
import time
import re
import json
from plugins import base
from plugins import lista
from plugins import listb
from plugins import dotpy
class Iptv (object):
def __init__ (self) :
self.T = tools.Tools()
self.DB = db.DataBase()
def run(self) :
Base = base.Source()
urlList = Base.getSource()
for item in urlList :
self.addData(item)
listA = lista.Source()
urlList = listA.getSource()
for item in urlList :
self.addData(item)
listB = listb.Source()
urlList = listB.getSource()
for item in urlList :
self.addData(item)
Dotpy = dotpy.Source()
urlList = Dotpy.getSource()
for item in urlList :
self.addData(item)
self.outPut()
self.outJson()
print("DONE!!")
def addData (self, data) :
sql = "SELECT * FROM %s WHERE url = '%s'" % (self.DB.table, data['url'])
result = self.DB.query(sql)
if len(result) == 0 :
data['enable'] = 1
self.DB.insert(data)
else :
id = result[0][0]
self.DB.edit(id, data)
def outPut (self) :
sql = """SELECT * FROM
(SELECT * FROM %s WHERE online = 1 ORDER BY delay DESC) AS delay
GROUP BY LOWER(delay.title)
HAVING delay.title != '' and delay.title != 'CCTV-' AND delay.delay < 500
ORDER BY level ASC, length(title) ASC, title ASC
""" % (self.DB.table)
result = self.DB.query(sql)
with open('tv.m3u8', 'w') as f:
f.write("#EXTM3U\n")
for item in result :
className = '其他频道'
if item[4] == 1 :
className = '中央频道'
elif item[4] == 2 :
className = '地方频道'
elif item[4] == 3 :
className = '地方频道'
elif item[4] == 7 :
className = '广播频道'
else :
className = '其他频道'
f.write("#EXTINF:-1, group-title=\"%s\", %s\n" % (className, item[1]))
f.write("%s\n" % (item[3]))
def outJson (self) :
sql = """SELECT * FROM
(SELECT * FROM %s WHERE online = 1 ORDER BY delay DESC) AS delay
GROUP BY LOWER(delay.title)
HAVING delay.title != '' and delay.title != 'CCTV-' AND delay.delay < 500
ORDER BY level ASC, length(title) ASC, title ASC
""" % (self.DB.table)
result = self.DB.query(sql)
fmtList = {
'cctv': [],
'local': [],
'other': [],
'radio': []
}
for item in result :
tmp = {
'title': item[1],
'url': item[3]
}
if item[4] == 1 :
fmtList['cctv'].append(tmp)
elif item[4] == 2 :
fmtList['local'].append(tmp)
elif item[4] == 3 :
fmtList['local'].append(tmp)
elif item[4] == 7 :
fmtList['radio'].append(tmp)
else :
fmtList['other'].append(tmp)
jsonStr = json.dumps(fmtList)
with open('tv.json', 'w') as f:
f.write(jsonStr)
if __name__ == '__main__':
obj = Iptv()
obj.run()