@Danfocus/

HomelyPrivateType

Python

No description

fork
loading
Files
  • main.py
  • somafm_42.m3u8
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
from requests import get

BASE_URL = 'http://ice4.somafm.com/'

JSON_URL = '{}status-json.xsl'.format(BASE_URL)

FORMAT_INDX = {
    'mp3': 1,
    'aac': 2,
}

INDX_FORMAT = {
    1: 'mp3',
    2: 'aac',
}


def main():
    res = get(JSON_URL)
    data = res.json()
    stations_ = {}
    for block in data['icestats']['source']:
        listenurl = block['listenurl']
        server_name = block['server_name']
        baseurl, bitrate, fmt = listenurl.split('-')[-3:]
        st_url = baseurl.split('/')[-1]
        if st_url in stations_:
            stations_[st_url].append((int(bitrate), FORMAT_INDX[fmt], server_name))
        else:
            stations_[st_url] = [(int(bitrate), FORMAT_INDX[fmt], server_name), ]

    stations = []
    for st_url in stations_:
        stations_[st_url].sort(reverse=True)
        stations.append(
            (
                stations_[st_url][0][2],
                '{}{}-{}-{}'.format(BASE_URL, st_url, stations_[st_url][0][0], INDX_FORMAT[stations_[st_url][0][1]])
            )
        )

    sorted_stations = sorted(stations, key=lambda tup: tup[0].lower())
    with open('somafm_{}.m3u8'.format(len(sorted_stations)), 'wb') as f:
        f.write(b'#EXTM3U\r\n')
        f.write(b'#PLAYLIST:SOMAFM\r\n')
        for title, url in sorted_stations:
            f.write('#EXTINF:-1,{}\r\n'.format(title).encode('utf-8'))
            f.write('{}\r\n'.format(url).encode('utf-8'))
    print(len(sorted_stations))


if __name__ == "__main__":
    main()