-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudice.py
166 lines (145 loc) · 4.68 KB
/
cloudice.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
154
155
156
157
158
159
160
161
162
163
164
165
166
import soundcloud
import settings
import stream_settings as ss
import shout
import sys
import pycurl
from urllib import urlencode
from cStringIO import StringIO
from time import sleep
import unicodedata
import logging
from subprocess import Popen, PIPE
from select import select
logging.basicConfig(filename='./cloud.log', level=logging.DEBUG)
logger = logging.getLogger()
# IMPORTANT
# if no certificates are found, you must provide CURL_CA_BUNDLE env var
#
# CURL_CA_BUNDLE=/opt/local/share/curl/curl-ca-bundle.crt
def SoundCloudGen():
logger.info("Connecting to soundcloud")
client = soundcloud.Client(client_id=settings.client_id)
limit = 100
offset = 0
while True:
yield client.get(
"/tracks",
license="cc-by",
filter="steamable",
tags=settings.tags,
types="original",
limit=limit,
duration={'from': 90000},
offset=offset)
offset = offset + limit
proc = None
def create_shout():
_shout = shout.Shout()
_shout.port = ss.port
_shout.host = ss.host
_shout.user = ss.user
_shout.password = ss.password
_shout.mount = ss.mount_point
_shout.protocol = "http"
_shout.format = "mp3"
_shout.name = ss.name
_shout.genre = ss.genre
_shout.description = ss.description
_shout.agent = ss.user_agent
_shout.audio_info = {
shout.SHOUT_AI_SAMPLERATE: str(ss.samplerate),
shout.SHOUT_AI_BITRATE: str(ss.bitrate),
shout.SHOUT_AI_CHANNELS: str(ss.channels)
}
return _shout
def cbk_write(buf):
global proc, icecast
data = StringIO(buf)
data_l = len(buf)
while data_l > data.tell():
try:
proc.stdin.write(data.read(4096))
#logger.debug("Data: %d", data.tell())
except IOError:
logger.error("Restarting transcoder")
proc.terminate()
proc = Popen(settings.transcoder.split(
' '), stdout=PIPE, stdin=PIPE)
except Exception as e:
logger.error(e)
try:
ready = select([proc.stdout], [], [], 0.5)
if not len(ready[0]):
continue
tok = proc.stdout.read(4096)
#logger.debug("Transcoded data: %d ", len(tok))
except IOError:
logger.error("Restarting transcoder")
proc.terminate()
proc = Popen(settings.transcoder.split(
' '), stdout=PIPE, stdin=PIPE)
except Exception as e:
logger.error(e)
try:
icecast.send(tok)
except Exception as e:
logger.error(e)
icecast.close()
sleep(1)
icecast = create_shout()
icecast.open()
logger.error("Failed connection with icecast server")
if __name__ == "__main__":
try:
gen = SoundCloudGen()
logger.info("Connecting to icecast")
except Exception as e:
logger.error(e)
sys.exit(1)
errorcount = 0.0
playcount = 10.0
curl = pycurl.Curl()
curl.setopt(pycurl.FOLLOWLOCATION, 1)
curl.setopt(pycurl.MAXREDIRS, 5)
curl.setopt(pycurl.CONNECTTIMEOUT, 30)
curl.setopt(pycurl.TIMEOUT, 300)
curl.setopt(pycurl.NOSIGNAL, 1)
curl.setopt(pycurl.WRITEFUNCTION, cbk_write)
if len(settings.transcoder.strip()):
proc = Popen(settings.transcoder.split(' '), stdout=PIPE, stdin=PIPE)
else:
sys.exit(1)
while errorcount / playcount <= 1:
try:
tracks = gen.next()
except Exception as e:
logger.error("client.get: %s" % e)
logger.error("[ErrorRatio] %d %d %f" % (
errorcount, playcount, errorcount / playcount))
errorcount = errorcount + 1
continue
playcount = playcount + 1
global icecast
icecast = create_shout()
icecast.open()
for track in tracks:
try:
curl.setopt(pycurl.URL, "%s?client_id=%s" % (
track.stream_url, settings.client_id))
username = track.user["username"]
title = track.title
logger.info("Now playing: %s - %s" % (username, title))
icecast.set_metadata({
'song': unicodedata.normalize(
"NFKD", title).encode('ascii', 'ignore')
})
except Exception as e:
logger.error(e)
logger.error("[ErrorRatio] %d %d %f" % (
errorcount, playcount, errorcount / playcount))
try:
curl.perform()
except pycurl.error:
pass # go on, don't look back!
icecast.close()