forked from Axel-Erfurt/m3uEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m3u_to_csv.py
68 lines (55 loc) · 1.92 KB
/
m3u_to_csv.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
if not len(sys.argv) == 3:
print("Usage: \npython3 ./m3u_to_csv.py input.m3u output.csv")
sys.exit()
m3u_file = sys.argv[1]
csv_file = sys.argv[2]
mylist = open(m3u_file, 'r').read().splitlines()
group = ""
ch = ""
url = ""
id = ""
logo = ""
csv_content = ""
headers = ['tvg-name', 'group-title', 'tvg-logo', 'tvg-id', 'url']
csv_content += '\t'.join(headers)
csv_content += "\n"
for x in range(1, len(mylist)-1):
line = mylist[x]
nextline = mylist[x+1]
if line.startswith("#EXTINF") and not "**********" in line:
if 'tvg-name="' in line:
ch = line.partition('tvg-name="')[2].partition('" ')[0]
elif 'tvg-name=' in line:
ch = line.partition('tvg-name=')[2].partition(' tvg')[0]
else:
ch = line.rpartition(',')[2]
if ch == "":
ch = "No Name"
ch = ch.replace('"', '')
if 'group-title="' in line:
group = line.partition('group-title="')[2].partition('"')[0]
elif "group-title=" in line:
group = line.partition('group-title=')[2].partition(' tvg')[0]
else:
group = "TV"
group = group.replace('"', '')
if 'tvg-id="' in line:
id = line.partition('tvg-id="')[2].partition('"')[0]
elif 'tvg-id=' in line:
id = line.partition('tvg-id=')[2].partition(' ')[0]
else:
id = ""
id = id.replace('"', '')
url = nextline
if 'tvg-logo="' in line:
logo = line.partition('tvg-logo="')[2].partition('"')[0]
elif 'tvg-logo=' in line:
logo = line.partition('tvg-logo=')[2].partition(' ')[0]
else:
logo = ""
csv_content += (f'{ch}\t{group}\t{logo}\t{id}\t{url}\n')
with open(csv_file, 'w') as f:
f.write(csv_content)