itvtest1.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import re
  2. # 读取itvlist.txt文件,提取频道信息
  3. channels = []
  4. with open('itvlist.txt', 'r', encoding='utf-8') as file:
  5. for line in file:
  6. line = line.strip()
  7. if line:
  8. channel, address = line.split(',')
  9. channels.append((channel, address))
  10. # 对频道进行排序
  11. channels.sort()
  12. # 自定义排序函数,提取频道名称中的数字并按数字排序
  13. def channel_key(channel):
  14. match = re.search(r'\d+', channel)
  15. if match:
  16. return int(match.group())
  17. else:
  18. return float('inf') # 返回一个无穷大的数字作为关键字
  19. # 对频道进行排序
  20. channels.sort(key=lambda x: channel_key(x[0]))
  21. # 生成iptv_list.m3u文件
  22. with open('itvlist.m3u', 'w', encoding='utf-8') as file:
  23. file.write('#EXTM3U\n')
  24. for channel, address in channels:
  25. if 'cctv' in channel.lower():
  26. file.write(f'#EXTINF:-1 tvg-id="{channel}" tvg-logo="https://epg.112114.xyz/logo/{channel}.png" group-title="央视",{channel}\n{address}\n')
  27. for channel, address in channels:
  28. if '河南' in channel:
  29. file.write(f'#EXTINF:-1 tvg-id="{channel}" tvg-logo="https://epg.112114.xyz/logo/{channel}.png" group-title="河南",{channel}\n{address}\n')
  30. for channel, address in channels:
  31. if '卫视' in channel:
  32. file.write(f'#EXTINF:-1 tvg-id="{channel}" tvg-logo="https://epg.112114.xyz/logo/{channel}.png" group-title="卫视",{channel}\n{address}\n')
  33. for channel, address in channels:
  34. if '凤凰' in channel:
  35. file.write(f'#EXTINF:-1 tvg-id="{channel}" tvg-logo="https://epg.112114.xyz/logo/{channel}.png" group-title="香港",{channel}\n{address}\n')
  36. for channel, address in channels:
  37. if 'cctv' not in channel.lower() and '卫视' not in channel and '河南' not in channel and '凤凰' not in channel:
  38. file.write(f'#EXTINF:-1 tvg-id="{channel}" tvg-logo="https://epg.112114.xyz/logo/{channel}.png" group-title="其他",{channel}\n{address}\n')