-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathM3uParse.py
90 lines (77 loc) · 3.17 KB
/
M3uParse.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
import re
class M3uParse:
def __init__(self, filename):
self.filename = filename
self.channelsInfo = []
self.preLineInfo = '#EXTINF:-1'
self.moviesExtensions = ["mp4", "mkv"]
self.readM3u()
def readM3u(self):
self.readAllLines()
self.parseFile()
# Read all file lines
def readAllLines(self):
self.lines = [line.rstrip('\n') for line in open(self.filename)]
self.m3uHeader = self.lines[0]
return len(self.lines)
def parseFile(self):
numLines = len(self.lines)
for n in range(numLines):
line = self.lines[n]
if line[0] == "#":
self.manageLine(n)
def parseChannel(self, line, url):
m = re.search("tvg-name=\"(.*?)\"", line)
name = m.group(1) if (m is not None) else ''
m = re.search("tvg-id=\"(.*?)\"", line)
id = m.group(1) if (m is not None) else ''
m = re.search("tvg-logo=\"(.*?)\"", line)
logo = m.group(1) if (m is not None) else ''
m = re.search("group-title=\"(.*?)\"", line)
group = m.group(1) if (m is not None) else ''
m = re.search("[,](?!.*[,])(.*?)$", line)
title = m.group(1) if (m is not None) else ''
return {
"title": title,
"tvg-name": name,
"tvg-ID": id,
"tvg-logo": logo,
"tvg-group": group,
"url": url
}
def manageLine(self, n):
lineInfo = self.lines[n]
lineLink = self.lines[n + 1]
if lineInfo != self.m3uHeader:
channel = self.parseChannel(lineInfo, lineLink)
self.channelsInfo.append(channel)
def cloneGroups(self, groups):
for channel in groups:
matchedChannels = [ch for ch in self.channelsInfo if self.channelMatchs(ch, channel)]
if matchedChannels:
matchedChannel = next(iter(matchedChannels))
self.copyProperties(matchedChannel, channel)
def copyProperties(self, channel1, channel2):
wantedProperties = ["title", "tvg-name", "tvg-ID", "tvg-logo", "tvg-group"]
newProperties = dict((k,channel2[k]) for k in wantedProperties if channel2[k])
channel1.update(newProperties)
def channelMatchs(self, channel1, channel2):
sameTitle = (channel1['title'] !='' and channel2['title'] !='') and channel1['title'].lower() == channel2['title'].lower()
sameID = channel1['tvg-ID'].lower() == channel2['tvg-ID'].lower()
return sameID or sameTitle
def channelsToLines(self):
lines = []
for channel in self.channelsInfo:
lines = lines + self.channelToString(channel)
return lines
def channelToString(self, channel):
_channel = dict(channel)
url = _channel.pop('url')
title = _channel.pop('title')
info = ' '.join([f'{key}="{_channel[key]}"' for key in _channel if _channel[key] != ''])
lineInfo = f'{self.preLineInfo} {info}, {title}'
return [lineInfo, url]
def writeM3u(self):
with open(self.filename, 'w') as file:
file.write(self.m3uHeader + '\n')
file.write('\n'.join(self.channelsToLines()))