第一个数据源完成
This commit is contained in:
commit
8a9a90e371
27
db.py
27
db.py
|
@ -37,7 +37,7 @@ class DataBase :
|
||||||
def create (self) :
|
def create (self) :
|
||||||
if self.connStat == False : return False
|
if self.connStat == False : return False
|
||||||
|
|
||||||
sql = 'create table ' + self.table + ' (id integer PRIMARY KEY autoincrement, title text, quality text, url text, enable integer, online integer, update text)'
|
sql = 'create table ' + self.table + ' (id integer PRIMARY KEY autoincrement, title text, quality text, url text, enable integer, online integer, delay integer, udTime text)'
|
||||||
self.cur.execute(sql)
|
self.cur.execute(sql)
|
||||||
|
|
||||||
def query (self, sql) :
|
def query (self, sql) :
|
||||||
|
@ -48,6 +48,14 @@ class DataBase :
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
def execute (self, sql) :
|
||||||
|
try :
|
||||||
|
if self.connStat == False : return False
|
||||||
|
self.cur.execute(sql)
|
||||||
|
return True
|
||||||
|
except :
|
||||||
|
return False
|
||||||
|
|
||||||
def insert (self, data):
|
def insert (self, data):
|
||||||
if self.connStat == False : return False
|
if self.connStat == False : return False
|
||||||
|
|
||||||
|
@ -65,6 +73,23 @@ class DataBase :
|
||||||
self.cur.execute(sql)
|
self.cur.execute(sql)
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
|
def edit (self, id, data):
|
||||||
|
if self.connStat == False : return False
|
||||||
|
|
||||||
|
import sys
|
||||||
|
reload(sys)
|
||||||
|
sys.setdefaultencoding('utf-8')
|
||||||
|
|
||||||
|
param = ''
|
||||||
|
for k, v in data.iteritems():
|
||||||
|
param = param + ", `%s` = '%s'" %(k, str(v).replace('"','\"').replace("'","''"))
|
||||||
|
|
||||||
|
param = param[1:]
|
||||||
|
|
||||||
|
sql = "update " + self.table + " set %s WHERE id = %s" % (param, id)
|
||||||
|
self.cur.execute(sql)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
def disConn (self) :
|
def disConn (self) :
|
||||||
if self.connStat == False : return False
|
if self.connStat == False : return False
|
||||||
|
|
||||||
|
|
71
main.py
71
main.py
|
@ -2,12 +2,25 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import tools
|
import tools
|
||||||
|
import db
|
||||||
|
import time
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
reload(sys)
|
||||||
|
sys.setdefaultencoding('utf8')
|
||||||
|
|
||||||
class Iptv :
|
class Iptv :
|
||||||
|
|
||||||
def __init__ (self) :
|
def __init__ (self) :
|
||||||
self.T = tools.Tools()
|
self.T = tools.Tools()
|
||||||
|
self.DB = db.DataBase()
|
||||||
|
self.now = int(time.time() * 1000)
|
||||||
|
|
||||||
|
def run(self) :
|
||||||
|
self.getSourceA()
|
||||||
|
self.outPut()
|
||||||
|
print("DONE!!")
|
||||||
|
|
||||||
def getSourceA (self) :
|
def getSourceA (self) :
|
||||||
url = 'https://www.jianshu.com/p/2499255c7e79'
|
url = 'https://www.jianshu.com/p/2499255c7e79'
|
||||||
|
@ -25,14 +38,22 @@ class Iptv :
|
||||||
sourceList = pattern.findall(tmp[0])
|
sourceList = pattern.findall(tmp[0])
|
||||||
sourceList = sourceList + pattern.findall(tmp[1])
|
sourceList = sourceList + pattern.findall(tmp[1])
|
||||||
|
|
||||||
|
|
||||||
for item in sourceList :
|
for item in sourceList :
|
||||||
playable = self.chkPlayable(item[1])
|
netstat = self.chkPlayable(item[1])
|
||||||
|
|
||||||
if playable == True :
|
if netstat > 0 :
|
||||||
info = self.fmtTitle(item[0])
|
info = self.fmtTitle(item[0])
|
||||||
print('title: ' + str(info['id']) + ' ' + str(info['title']))
|
|
||||||
print('url: ' + str(item[1]))
|
data = {
|
||||||
|
'title' : str(info['id']) + str(info['title']),
|
||||||
|
'url' : str(item[1]),
|
||||||
|
'quality': str(info['quality']),
|
||||||
|
'delay' : netstat,
|
||||||
|
'enable' : 1,
|
||||||
|
'online' : 1,
|
||||||
|
'udTime' : self.now,
|
||||||
|
}
|
||||||
|
self.addData(data)
|
||||||
else :
|
else :
|
||||||
pass # MAYBE later :P
|
pass # MAYBE later :P
|
||||||
else :
|
else :
|
||||||
|
@ -40,18 +61,40 @@ class Iptv :
|
||||||
|
|
||||||
def chkPlayable (self, url) :
|
def chkPlayable (self, url) :
|
||||||
try:
|
try:
|
||||||
|
startTime = int(round(time.time() * 1000))
|
||||||
res = self.T.getPage(url)
|
res = self.T.getPage(url)
|
||||||
|
|
||||||
if res['code'] == 200 :
|
if res['code'] == 200 :
|
||||||
return True
|
endTime = int(round(time.time() * 1000))
|
||||||
|
useTime = endTime - startTime
|
||||||
|
return int(useTime)
|
||||||
else:
|
else:
|
||||||
return False
|
return 0
|
||||||
except:
|
except:
|
||||||
return False
|
return 0
|
||||||
|
|
||||||
|
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 :
|
||||||
|
print('add:' + str(data['title']))
|
||||||
|
self.DB.insert(data)
|
||||||
|
else :
|
||||||
|
print('update:' + str(data['title']))
|
||||||
|
id = result[0][0]
|
||||||
|
self.DB.edit(id, data)
|
||||||
|
|
||||||
|
def outPut (self) :
|
||||||
|
sql = "SELECT * FROM %s GROUP BY title HAVING online = 1 ORDER BY id ASC" % (self.DB.table)
|
||||||
|
result = self.DB.query(sql)
|
||||||
|
|
||||||
|
with open('tv.m3u8', 'w') as f:
|
||||||
|
f.write("#EXTM3U\n")
|
||||||
|
for item in result :
|
||||||
|
f.write("#EXTINF:-1,%s\n" % (item[1]))
|
||||||
|
f.write("%s\n" % (item[3]))
|
||||||
|
|
||||||
def baseFilter (self) :
|
|
||||||
pass
|
|
||||||
|
|
||||||
def fmtTitle (self, string) :
|
def fmtTitle (self, string) :
|
||||||
pattern = re.compile(r"(cctv[-|\s]*\d*)*\s*?([^fhd|^hd|^sd|^\.m3u8]*)\s*?(fhd|hd|sd)*", re.I)
|
pattern = re.compile(r"(cctv[-|\s]*\d*)*\s*?([^fhd|^hd|^sd|^\.m3u8]*)\s*?(fhd|hd|sd)*", re.I)
|
||||||
|
@ -66,4 +109,10 @@ class Iptv :
|
||||||
return result
|
return result
|
||||||
|
|
||||||
obj = Iptv()
|
obj = Iptv()
|
||||||
obj.getSourceA()
|
obj.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue