andreztz / pyradios

Client for the Radio Browser API
https://api.radio-browser.info/
MIT License
62 stars 22 forks source link

use new radio-browser api #2

Closed segler-alex closed 5 years ago

segler-alex commented 5 years ago

hi, i am the developer of the backend of radio-browser.info first i want to thank you for your help! it is great that other people contribute and providing libraries for programming languages to more easily use radio-browser api is a great way to help. it is very much appreciated!

in the future radio-browser will be a federated system to handle the load that is getting bigger every day, there are already new servers online.

if you want to use them please make the base url dynamic. here is a code snippet:

#!/bin/env python
import socket
import random

def get_radiobrowser_base_urls():
    """
    Get all base urls of all currently available radiobrowser servers

    Returns: 
    list: a list of strings

    """
    hosts = []
    # get all hosts from DNS
    ips = socket.getaddrinfo('all.api.radio-browser.info',
                             80, 0, 0, socket.IPPROTO_TCP)
    for ip_tupple in ips:
        ip = ip_tupple[4][0]

        # do a reverse lookup on every one of the ips to have a nice name for it
        host_addr = socket.gethostbyaddr(ip)
        # add the name to a list if not already in there
        if host_addr[0] not in hosts:
            hosts.append(host_addr[0])

    # sort list of names
    hosts.sort()
    # add "https://" in front to make it an url
    return list(map(lambda x: "https://" + x, hosts))

def get_radiobrowser_base_url_random():
    """
    Get a random available base url

    Returns: 
    str: a random available base url

    """
    hosts = get_radiobrowser_base_urls()
    return random.choice(hosts)

# print list of names
print("All available urls")
print("------------------")
for host in get_radiobrowser_base_urls():
    print(host)
print("")

# print random
print("Random url")
print("------------------")
print(get_radiobrowser_base_url_random())
andreztz commented 5 years ago

@segler-alex Thank you for letting me know. The dynamic base url will be part of the next release.