-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.py
149 lines (103 loc) · 3.42 KB
/
radio.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
import socket
import sys
import time
import os
import urllib2
import contextlib
import sqlite3
import re
import string
DATABASE = '/codes/radio/radiostream.db'
link = ''
def insert_song(song_stream):
print song_stream
connection=sqlite3.connect(DATABASE)
with connection:
cursor=connection.cursor()
print cursor.execute('INSERT OR IGNORE INTO Songs VALUES (?, ?,?)',song_stream)
def parse_headers(response):
headers = {}
while True:
line = response.readline()
if line == '\r\n':
break # end of headers
if ':' in line:
key, value = line.split(':', 1)
headers[key] = value
return headers
def parse_flv_meta(response):
song=''
flv_line=response.readline()
if flv_line.find("FLV")!=-1: #CONTAINS THE FLV HEADER WHICH MIGHT INDICATE WE HAVE SOMETHING
response.readline()
response.readline()
response.readline()
imp_line=response.readline()
l_index=imp_line.find('Artist')+6 #parsing in format Artist - Song
r_index=imp_line.find('BuyNowURL')
if l_index!=-1 and r_index!=-1:
song=imp_line[l_index:r_index]
l_index=imp_line.find('Title')+5
r_index=imp_line.find('Type')
if l_index!=-1 and r_index!=-1:
song=song+'-'+imp_line[l_index:r_index]
song=filter(lambda x: x in string.printable,song) #removing weird shit
song=" ".join(song.split())
song_stream=((str(song),str(link)))
return song_stream
def parse_icy_meta(response):
headers = parse_headers(response)
meta_interval = int(headers['icy-metaint'])
response.read(meta_interval) # throw away the data until the meta interval
length = ord(response.read(1)) * 16 # length is encoded in the stream
metadata = response.read(length)
l_index=metadata.find('=')+2
#print "left index", l_index
r_index=metadata.find('\';')
#print 'right index', r_index
song=metadata[l_index:r_index]
l_index=r_index+13
r_index=metadata.rfind('\';')
stream=metadata[l_index:r_index]
song_stream=((song,stream))
return song_stream
def parse_meta(response):
if 'Content-Type' not in response.info(): #checking the content type if it even exists or not Icecast has no http header in info() have to parse
song_stream=parse_icy_meta(response) #nothing in content type...header might possibly be not there or could be a icecast stream.
elif response.info()['Content-type']=='application/flv': #can assume it to be a flv stream
song_stream=parse_flv_meta(response)
else:
song_stream=(("",""))
return (song_stream)
def poll_radio(stream="http://8313.live.streamtheworld.com/WPOIFMAAC"):
global link
link=stream
request = urllib2.Request(stream, headers = {'User-Agent' : 'User-Agent: VLC/2.0.5 LibVLC/2.0.5','Icy-MetaData' : '1','Range' : 'bytes=0-',})
# the connection will be close on exit from with block
with contextlib.closing(urllib2.urlopen(request)) as response:
song_stream=parse_meta(response)
return song_stream
def run_stations():
try:
station_con = sqlite3.connect(DATABASE)
with station_con:
station_cur = station_con.cursor()
station_rows=station_cur.execute("SELECT station FROM Stations")
rows=station_rows.fetchall()
for row in rows:
#print row[0]
song_stream=poll_radio(row[0])
song_stream=song_stream+(0,)
insert_song(song_stream)
#print song_stream
except:
pass
def hours_run(hours):
while hours>0:
run_stations()
time.sleep(120)
hours-=1
try:
hours_run(10000)
except:
pass