-
Notifications
You must be signed in to change notification settings - Fork 13
/
board_handler.py
260 lines (215 loc) · 10.9 KB
/
board_handler.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# This file is part of Radio-Browser-Plugin for Rhythmbox.
# Copyright (C) 2012 <[email protected]>
# This is a derivative of software originally created by <[email protected]> 2009
#
# Radio-Browser-Plugin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Radio-Browser-Plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radio-Browser-Plugin. If not, see <http://www.gnu.org/licenses/>.
import os
import urllib.request, urllib.parse, urllib.error
from gi.repository import Gtk
import xml.sax.handler
from radio_station import RadioStation
from feed import Feed
from feed import FeedAction
from feed import FeedStationAction
from constants import _Const
CONST = _Const()
class BoardHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.entries = []
self.languages = []
self.countries = []
def startElement(self, name, attributes):
if name == "station":
self.entry = RadioStation()
self.entry.type = "Board"
self.entry.id = attributes.get("id")
self.entry.server_name = attributes.get("name")
self.entry.genre = attributes.get("tags")
if (self.entry.genre == None):
self.entry.genre = ""
self.entry.genre = ",".join(self.entry.genre.split(" "))
self.entry.listen_url = attributes.get("url")
self.entry.language = attributes.get("language")
self.entry.country = attributes.get("country")
self.entry.votes = attributes.get("votes")
self.entry.negativevotes = attributes.get("negativevotes")
self.entry.homepage = attributes.get("homepage")
self.entry.icon_src = attributes.get("favicon")
self.entry.bitrate = attributes.get("bitrate")
self.entry.server_type = attributes.get("codec")
try:
self.entry.clickcount = attributes.get("clickcount")
except:
self.entry.clickcount = 0
self.entries.append(self.entry)
if self.entry.country.title() not in self.countries:
self.countries.append(self.entry.country.title())
if self.entry.language.title() not in self.languages:
self.languages.append(self.entry.language.title())
class PostStationDialog(Gtk.Dialog):
def __init__(self):
super(PostStationDialog, self).__init__()
title_label = Gtk.Label()
title_label.set_markup("<span font='20.0'>" + _("Post new station") + "</span>")
self.get_content_area().pack_start(title_label, False, False, 0)
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
self.add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
table = Gtk.Table(3, 8)
table.attach(Gtk.Label(_("Examples")), 2, 3, 0, 1)
table.attach(Gtk.Label(_("Name")), 0, 1, 1, 2)
self.StationName = Gtk.Entry()
table.attach(self.StationName, 1, 2, 1, 2)
table.attach(Gtk.Label(_("My Station")), 2, 3, 1, 2)
table.attach(Gtk.Label(_("URL")), 0, 1, 2, 3)
self.StationUrl = Gtk.Entry()
table.attach(self.StationUrl, 1, 2, 2, 3)
table.attach(Gtk.Label(_("http://listen.to.my/station.pls")), 2, 3, 2, 3)
table.attach(Gtk.Label(_("Homepage URL")), 0, 1, 3, 4)
self.StationHomepage = Gtk.Entry()
table.attach(self.StationHomepage, 1, 2, 3, 4)
table.attach(Gtk.Label(_("http://very.cool.site")), 2, 3, 3, 4)
table.attach(Gtk.Label(_("Favicon URL")), 0, 1, 4, 5)
self.StationFavicon = Gtk.Entry()
table.attach(self.StationFavicon, 1, 2, 4, 5)
table.attach(Gtk.Label(_("http://very.cool.site/favicon.ico")), 2, 3, 4, 5)
table.attach(Gtk.Label(_("Country")), 0, 1, 5, 6)
self.StationCountry = Gtk.ComboBoxText.new_with_entry()
#self.StationCountry.set_hexpand(True)
table.attach(self.StationCountry, 1, 2, 5, 6)
table.attach(Gtk.Label(_("Utopia")), 2, 3, 5, 6)
table.attach(Gtk.Label(_("Language")), 0, 1, 6, 7)
self.StationLanguage = Gtk.ComboBoxText.new_with_entry()
#self.StationLanguage.set_hexpand(True)
table.attach(self.StationLanguage, 1, 2, 6, 7)
table.attach(Gtk.Label(_("Esperanto")), 2, 3, 6, 7)
table.attach(Gtk.Label(_("Tags")), 0, 1, 7, 8)
self.StationTags = Gtk.Entry()
table.attach(self.StationTags, 1, 2, 7, 8)
table.attach(Gtk.Label(_("Classical Jazz Talk")), 2, 3, 7, 8)
self.get_content_area().pack_start(table, False, False, 0)
self.set_title(_("Post new station"))
self.set_resizable(False)
self.set_position(Gtk.WindowPosition.CENTER)
self.show_all()
class FeedBoard(Feed):
def __init__(self, cache_dir, status_change_handler):
Feed.__init__(self)
print("init board feed")
self.handler = BoardHandler()
self.cache_dir = cache_dir
self.filename = os.path.join(self.cache_dir, "board.xml")
self.uri = CONST.BOARD_ROOT + "xml/stations"
self.status_change_handler = status_change_handler
def name(self):
return "Board"
def getDescription(self):
return _("Community radio station board. Click the homepage and help!")
def getHomepage(self):
return "http://www.radio-browser.info"
def search(self, term):
foundEntries = []
for entry in self.entries():
if entry.server_name.lower().find(term.lower()) >= 0:
foundEntries.append(entry)
return foundEntries
""" vote for station on board """
def vote_station(self, source, station):
message = Gtk.MessageDialog(message_format=_("Vote for station"), buttons=Gtk.ButtonsType.YES_NO,
type=Gtk.MessageType.QUESTION)
message.format_secondary_text(_(
"Do you really want to vote for this station? It means, that you like it, and you want more people to know, that this is a good station."))
response = message.run()
if response == Gtk.ResponseType.YES:
f = urllib.request.urlopen(urllib.request.Request(CONST.BOARD_ROOT + "xml/vote/%d" % station.getId(), headers={'User-Agent': CONST.USER_AGENT}))
f.read()
source.refill_list()
message.destroy()
""" mark station as bad on board """
def bad_station(self, source, station):
message = Gtk.MessageDialog(message_format=_("Mark station as broken"), buttons=Gtk.ButtonsType.YES_NO,
type=Gtk.MessageType.WARNING)
message.format_secondary_text(_(
"Do you really want to mark this radio station as broken? It will eventually get deleted if enough people do that! More information on that on the feeds homepage: http://www.radio-browser.info/"))
response = message.run()
if response == Gtk.ResponseType.YES:
params = urllib.parse.urlencode({'action': 'negativevote', 'id': station.id})
f = urllib.request.urlopen("http://www.radio-browser.info/?%s" % params)
f.read()
source.refill_list()
message.destroy()
""" post new station to board """
def post_new_station(self, source):
dialog = PostStationDialog()
LanguageList = Gtk.ListStore(str)
print(self.handler.languages)
for language in self.handler.languages:
LanguageList.append([language])
LanguageList.set_sort_column_id(0, Gtk.SortType.ASCENDING)
dialog.StationLanguage.set_model(LanguageList)
dialog.StationLanguage.set_entry_text_column(0)
CountryList = Gtk.ListStore(str)
print(self.handler.countries)
for country in self.handler.countries:
CountryList.append([country])
CountryList.set_sort_column_id(0, Gtk.SortType.ASCENDING)
dialog.StationCountry.set_model(CountryList)
dialog.StationCountry.set_entry_text_column(0)
while True:
def show_message(message):
info_dialog = Gtk.MessageDialog(parent=dialog, buttons=Gtk.ButtonsType.OK, message_format=message)
info_dialog.run()
info_dialog.destroy()
response = dialog.run()
if response == Gtk.ResponseType.CANCEL:
break
if response == Gtk.ResponseType.OK:
Name = dialog.StationName.get_text().strip()
URL = dialog.StationUrl.get_text().strip()
Homepage = dialog.StationHomepage.get_text().strip()
Favicon = dialog.StationFavicon.get_text().strip()
Tags = dialog.StationTags.get_text().strip()
Country = dialog.StationCountry.get_child().get_text().strip()
Language = dialog.StationLanguage.get_child().get_text().strip()
if Name == "" or URL == "":
show_message(_("Name and URL are necessary"))
continue
if not (URL.lower().startswith("http://") or URL.lower().startswith("mms://")):
show_message(_("URL needs to start with http:// or mms://"))
continue
if Homepage != "":
if not Homepage.lower().startswith("http://"):
show_message(_("Homepage URL needs to start with http://"))
continue
if Favicon != "":
if not Favicon.lower().startswith("http://"):
show_message(_("Favicon URL needs to start with http://"))
continue
params = urllib.parse.urlencode(
{'name': Name, 'url': URL, 'homepage': Homepage, 'favicon': Favicon, 'tags': Tags,
'language': Language, 'country': Country})
f = urllib.request.urlopen(urllib.request.Request(CONST.BOARD_ROOT + "add?%s" % params, headers={'User-Agent': CONST.USER_AGENT}))
f.read()
show_message(_("Station successfully posted"))
source.refill_list()
break
dialog.destroy()
def get_feed_actions(self):
actions = []
actions.append(FeedAction(self, _("Post new station"), self.post_new_station))
return actions
def get_station_actions(self):
actions = []
actions.append(FeedStationAction(self, _("Vote for station"), self.vote_station))
actions.append(FeedStationAction(self, _("Station is broken"), self.bad_station))
return actions