@Danfocus/

HomelyPrivateType

Python

No description

fork
loading
Files
  • main.py
  • somafm_42.m3u8

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8674498576900582","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"ZnJvbSByZXF1ZXN0cyBpbXBvcnQgZ2V0CgpCQVNFX1VSTCA9ICdodHRwOi8vaWNlNC5zb21hZm0uY29tLycKCkpTT05fVVJMID0gJ3t9c3RhdHVzLWpzb24ueHNsJy5mb3JtYXQoQkFTRV9VUkwpCgpGT1JNQVRfSU5EWCA9IHsKICAgICdtcDMnOiAxLAogICAgJ2FhYyc6IDIsCn0KCklORFhfRk9STUFUID0gewogICAgMTogJ21wMycsCiAgICAyOiAnYWFjJywKfQoKCmRlZiBtYWluKCk6CiAgICByZXMgPSBnZXQoSlNPTl9VUkwpCiAgICBkYXRhID0gcmVzLmpzb24oKQogICAgc3RhdGlvbnNfID0ge30KICAgIGZvciBibG9jayBpbiBkYXRhWydpY2VzdGF0cyddWydzb3VyY2UnXToKICAgICAgICBsaXN0ZW51cmwgPSBibG9ja1snbGlzdGVudXJsJ10KICAgICAgICBzZXJ2ZXJfbmFtZSA9IGJsb2NrWydzZXJ2ZXJfbmFtZSddCiAgICAgICAgYmFzZXVybCwgYml0cmF0ZSwgZm10ID0gbGlzdGVudXJsLnNwbGl0KCctJylbLTM6XQogICAgICAgIHN0X3VybCA9IGJhc2V1cmwuc3BsaXQoJy8nKVstMV0KICAgICAgICBpZiBzdF91cmwgaW4gc3RhdGlvbnNfOgogICAgICAgICAgICBzdGF0aW9uc19bc3RfdXJsXS5hcHBlbmQoKGludChiaXRyYXRlKSwgRk9STUFUX0lORFhbZm10XSwgc2VydmVyX25hbWUpKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHN0YXRpb25zX1tzdF91cmxdID0gWyhpbnQoYml0cmF0ZSksIEZPUk1BVF9JTkRYW2ZtdF0sIHNlcnZlcl9uYW1lKSwgXQoKICAgIHN0YXRpb25zID0gW10KICAgIGZvciBzdF91cmwgaW4gc3RhdGlvbnNfOgogICAgICAgIHN0YXRpb25zX1tzdF91cmxdLnNvcnQocmV2ZXJzZT1UcnVlKQogICAgICAgIHN0YXRpb25zLmFwcGVuZCgKICAgICAgICAgICAgKAogICAgICAgICAgICAgICAgc3RhdGlvbnNfW3N0X3VybF1bMF1bMl0sCiAgICAgICAgICAgICAgICAne317fS17fS17fScuZm9ybWF0KEJBU0VfVVJMLCBzdF91cmwsIHN0YXRpb25zX1tzdF91cmxdWzBdWzBdLCBJTkRYX0ZPUk1BVFtzdGF0aW9uc19bc3RfdXJsXVswXVsxXV0pCiAgICAgICAgICAgICkKICAgICAgICApCgogICAgc29ydGVkX3N0YXRpb25zID0gc29ydGVkKHN0YXRpb25zLCBrZXk9bGFtYmRhIHR1cDogdHVwWzBdLmxvd2VyKCkpCiAgICB3aXRoIG9wZW4oJ3NvbWFmbV97fS5tM3U4Jy5mb3JtYXQobGVuKHNvcnRlZF9zdGF0aW9ucykpLCAnd2InKSBhcyBmOgogICAgICAgIGYud3JpdGUoYicjRVhUTTNVXHJcbicpCiAgICAgICAgZi53cml0ZShiJyNQTEFZTElTVDpTT01BRk1cclxuJykKICAgICAgICBmb3IgdGl0bGUsIHVybCBpbiBzb3J0ZWRfc3RhdGlvbnM6CiAgICAgICAgICAgIGYud3JpdGUoJyNFWFRJTkY6LTEse31cclxuJy5mb3JtYXQodGl0bGUpLmVuY29kZSgndXRmLTgnKSkKICAgICAgICAgICAgZi53cml0ZSgne31cclxuJy5mb3JtYXQodXJsKS5lbmNvZGUoJ3V0Zi04JykpCiAgICBwcmludChsZW4oc29ydGVkX3N0YXRpb25zKSkKCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgbWFpbigpCg=="},"asBuffer":null},"loaded":true}}
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()