-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
59 lines (50 loc) · 1.68 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
from config import config
from cache import Cache
import requests
import time
import xml.etree.ElementTree as ET
class API(object):
def __init__(self):
pass
@staticmethod
@Cache
def get_file_content(url):
"""
Gets XML data file
:param url: Link of XML file
:return: Object
"""
r = requests.get(url)
r.encoding = 'utf-8'
if r.status_code == 200:
return r.text
def get_all_channels(self):
"""
Returns a list of all digital radio stations
:return:
"""
xml = self.get_file_content(config['urls']['radius_data_file'])
if not xml:
return []
root = ET.fromstring(xml.encode('utf-8'))
return [{
'name': channel.find('name').text.encode('utf-8'),
'thumbnail': channel.find('img').text,
'label': channel.find('font').text,
'stream': channel.find('Feed').text,
'meta': channel.find('nowplaying').text,
'description': channel.find('desc').text.encode('utf-8'),
} for channel in root.findall('Channel')]
def get_current_song(self, url):
"""
Returns current song details
:return:
"""
xml = self.get_file_content('{0}?rand={1}'.format(url, str(time.time())))
if not xml:
return {}
root = ET.fromstring(xml.encode('utf-8'))
return {
'name': root.find('name').text.encode('utf-8') if root.find('name').text else '',
'artist': root.find('artist').text.encode('utf-8') if root.find('artist').text else ''
}