-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
153 lines (114 loc) · 4.81 KB
/
api.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from backend import json
import urllib.request
import socket
def collected_servers():
"""
Get server link from www.radio-browser.info
:return: List sontaining all avaiable server for requests
"""
try:
hosts = []
ips = socket.getaddrinfo('all.api.radio-browser.info', 80, 0, 0, socket.IPPROTO_TCP)
for ip_tupple in ips:
ip = ip_tupple[4][0]
host_addr = socket.gethostbyaddr(ip)
if host_addr[0] not in hosts:
hosts.append(host_addr[0])
hosts.sort()
return list(map(lambda x: "https://" + x, hosts))
except Exception as e:
print("COLLECT_SERVERS Unable to download from api url: ", e)
pass
def collect_country_info(term):
"""
First step for a searcha based on countries. Get names of all the available countries
:param term: "name", "iso_3166_1" or "stationcount" as terms
:return: list of countries, abreviations of the countries or number of stations in every country
"""
url = collected_servers()
for server in url:
try:
req = urllib.request.Request(f"{server}/json/countries")
response = urllib.request.urlopen(req)
data = response.read()
response.close()
countries_info = json.loads(data)
country_info_list = [country_name[term] for country_name in countries_info]
return country_info_list
except Exception as e:
print("COLLECT_COUNTRY_INFO Unable to download from api url: ", e)
pass
def search_by_country(term, country):
"""
Get all available radio stations from a selected country
:param term: "name", "url", "stationuuid", "codec", "bitrate", "description", "tags", "countrycode" or "homepage"
:param country: Country name, can be obtained from de function: collect_country_info()
:return: List of names, urls or the selected term from the selected country
"""
url = collected_servers()
for server in url:
try:
count = country.replace(" ", "_")
req = urllib.request.Request(f"{server}/json/stations/bycountry/{count}")
response = urllib.request.urlopen(req)
data = response.read()
response.close()
stations_by_country_info = json.loads(data)
stations_by_country_list = [station[term] for station in stations_by_country_info]
return stations_by_country_list
except Exception as e:
print("SEARCH_BY_COUNTRY Unable to download from api url: ", e)
pass
def search_by_tag(term, tag):
"""
Get all the radio station marked with the selected tag (NO EXACT SEARCH)
:param term: "name", "url", "stationuuid", "codec", "bitrate", "description", "tags", "countrycode" or "homepage"
:param tag: selected tag for searching on the api
:return: a list of the stations asigned to the selected tag
"""
url = collected_servers()
for server in url:
try:
req = urllib.request.Request(f"{server}/json/stations/bytag/{tag}")
response = urllib.request.urlopen(req)
data = response.read()
response.close()
stations_by_tag_info = json.loads(data)
stations_by_tag_list = [station[term] for station in stations_by_tag_info]
return stations_by_tag_list
except Exception as e:
print("SEARCH_BY_TAG Unable to download from api url: ", e)
pass
def search_by_name(term, name):
"""
Get all the radio station marked with the selected name (NO EXACT SEARCH)
:param term: "name", "url", "stationuuid", "codec", "bitrate", "description", "tags", "countrycode" or "homepage"
:param name: selected name for searching on the api
:return: a list of the stations asigned to the selected tag
"""
url = collected_servers()
for server in url:
try:
req = urllib.request.Request(f"{server}/json/stations/byname/{name}")
response = urllib.request.urlopen(req)
data = response.read()
response.close()
stations_by_name_info = json.loads(data)
stations_by_name_list = [station[term] for station in stations_by_name_info]
return stations_by_name_list
except Exception as e:
print("SEARCH_BY_NAME Unable to download from api url: ", e)
pass
""" EXAMPLES """
"""
print(collected_servers())
print(collect_country_info("name"))
print(collect_country_info("iso_3166_1"))
print(collect_country_info("stationcount"))
print(search_by_country("name", "The_Netherlands"))
print(search_by_country("url", "The_Netherlands"))
# print(search_by_tag('name', 'rock'))
# print(search_by_tag('url', "rock"))
print(search_by_name('name', 'slam'))
print(search_by_name('url', 'slam'))
"""