A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
9
return {10
playing: false,11
loading: false,12
error: false,13
title: '- No song title available -',13
locale: 'en'14
interval: null15
};16
},17
components: {18
VButton,19
VIcon20
},21
watch: {22
station: function(station) {23
this.playing = false;24
this.error = false;25
if(station !== null) {26
this.loading = true;27
this.title = '- No song title available -';28
this.updateMediaSession();29
}30
}31
},32
methods: {33
$lang,34
async setLike(is_favorite) {
41
console.log(error);42
}43
},44
setPlaying(playing, event = false) {45
if(this.error) {46
return;47
}48
this.playing = playing;49
this.$refs.player[this.playing ? 'play' : 'pause']();50
if(this.playing) {51
this.error = false;52
if(!event) {53
this.updateSong();54
}55
}56
},57
onLoad() {58
this.loading = false;59
this.playing = true;60
this.updateSong();61
},62
onError(event) {63
this.error = true;64
this.loading = false;65
this.playing = false;66
},67
updateMediaSession() {68
if('mediaSession' in navigator) {69
navigator.mediaSession.metadata = new MediaMetadata({70
title: this.title,71
artist: this.station.name,72
artwork: [73
{ src: this.station.favicon, sizes: '96x96', type: 'image/jpg' },74
{ src: this.station.favicon, sizes: '128x128', type: 'image/jpg' },75
{ src: this.station.favicon, sizes: '192x192', type: 'image/jpg' },76
{ src: this.station.favicon, sizes: '256x256', type: 'image/jpg' },77
{ src: this.station.favicon, sizes: '384x384', type: 'image/jpg' },78
{ src: this.station.favicon, sizes: '512x512', type: 'image/jpg' }79
]80
});81
}82
},83
async fetchSong() {84
try {85
const song = await http`get::/api/song`({ url: this.station.stream_url });86
this.title = song === '' ? '- No song title available -' : song;87
}88
catch(err) {89
this.title = '- No song title available -';90
}91
this.updateMediaSession();92
},93
updateSong() {94
this.fetchSong();95
clearInterval(this.interval);96
this.interval = setInterval(async () => {97
if(!this.playing) {98
clearInterval(this.interval);99
return;100
}101
await this.fetchSong();102
}, 10000);103
}104
},105
template: `106
<div class="sticky top-[98px] z-30">
108
v-if="station"109
class="flex flex-col bg-black pb-6"110
>39
<audio controls autoplay :src="station.stream_url" class="select-none pointer-events-none w-0 h-0 opacity-0">111
<audio112
ref="player"113
controls autoplay114
:src="station.stream_url"115
@loadeddata="onLoad"116
@play="setPlaying(true, true)"117
@pause="setPlaying(false, true)"118
@error="onError"119
class="select-none pointer-events-none w-0 h-0 opacity-0"120
>121
<source :src="station.stream_url" type="audio/mpeg">122
<source :src="station.stream_url" type="audio/ogg">123
<source :src="station.stream_url" type="audio/aac">
163
@click="setLike(!station.is_favorite)"164
></v-button>165
<v-button85
:icon="[ loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play') ]"86
:title="[ playing ? $lang('global.pause') : $lang('global.play') ]"87
:animation="[ loading ? 'animate-spin' : '' ]"166
:icon="[ error ? 'alert-circle' : (loading ? 'rotate-clockwise' : (playing ? 'player-pause' : 'player-play')) ]"167
:title="[ error ? $lang('global.error') : (playing ? $lang('global.pause') : $lang('global.play')) ]"168
:animation="[ error ? '' : (loading ? 'animate-spin' : '') ]"169
class="bg-zinc-900 hover:bg-white focus:ring-[6px] ring-white/10"170
@click="setPlaying(!playing)"171
></v-button>
1
use serde::Deserialize;2
use serde::Serialize;4
use crate::model;
6
use crate::utils::to_array;7
use crate::utils::upgrade_to_https;9
#[derive(Debug, Default, Deserialize)]10
pub struct Song {11
pub url: String,12
}14
#[derive(Debug, Serialize)]15
pub struct Account {16
pub username: Option<String>,
1
use data::Song;2
use sqlx::postgres::Postgres;3
use tide_compress::CompressMiddleware;4
use tide_sqlx::SQLxMiddleware;
75
}76
});78
app.at("/api/song")79
.get(|req: tide::Request<()>| async move {80
let song = match req.query::<Song>() {81
Ok(song) => song,82
Err(err) => return Response::throw(ErrorKind::Arguments, &err.to_string()),83
};84
match Service::get_song(song).await {85
Ok(data) => Response::with(data),86
Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),87
}88
});90
app.at("/api/like")91
.get(|req: tide::Request<()>| async move {92
match Service::get_likes(req).await {
1
use async_std::io::ReadExt;2
use regex::Regex;3
use sqlx::postgres::Postgres;4
use sqlx::prelude::*;5
use tide_sqlx::SQLxRequestExt;7
use crate::api::{RadioBrowserApi, Search};6
use crate::data;8
use crate::data::{self, Song};9
use crate::error::{Error, ErrorKind, Result};10
use crate::model;
43
Ok(data)44
}46
pub async fn get_song(song: Song) -> Result<Option<String>> {47
let mut response = match surf::get(&song.url).header("icy-metadata", "1").await {48
Ok(response) => response,49
Err(_) => return Err(Error::new(ErrorKind::Fetch, "Unable to fetch metadata!")),50
};51
let meta_int = match response.header("icy-metaint") {52
Some(header) => header.as_str(),53
None => return Err(Error::new(ErrorKind::Parse, "Invalid icy metadata!")),54
};55
let interval = match meta_int.parse::<usize>() {56
Ok(value) => value * 2,57
Err(_) => {58
return Err(Error::new(59
ErrorKind::Parse,60
"Unable to parse icy metadata!",61
))62
}63
};64
let mut count = 0;65
let mut bytes = vec![];66
let mut buf = [0; 10000];67
let mut data = None;68
loop {69
let len = response.read(&mut buf).await.unwrap();70
bytes.extend_from_slice(&buf);71
if count + len > interval {72
let metadata = String::from_utf8_lossy(&bytes);73
for cap in Regex::new("StreamTitle='([^;]*)';")74
.unwrap()75
.captures_iter(&metadata)76
{77
data = Some(cap[1].to_string());78
break;79
}80
break;81
}82
count += len;83
}84
Ok(data)85
}87
pub async fn get_likes(req: tide::Request<()>) -> Result<Vec<data::Station>> {88
let account = req.ext::<model::Account>().unwrap();89
let query = format!(
10
"global.unlike": "Unlike",11
"global.play": "Play",12
"global.pause": "Pause",13
"global.error": "Not reachable",14
"global.country": "Country",15
"global.votes": "Votes",16
"global.trend": "Trend",
48
"global.unlike": "Mag ich nicht",49
"global.play": "Abspielen",50
"global.pause": "Pausieren",51
"global.error": "Nicht erreichbar",52
"global.country": "Land",53
"global.votes": "Bewertung",54
"global.trend": "Trend",