Skip to content

Timeshift catchup #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/buildAndUpload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1

- name: Download mpv
if: matrix.os == 'windows-latest'
Expand Down
7 changes: 7 additions & 0 deletions flatpak/dev.fredol.open-tv.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
</categories>
<launchable type="desktop-id">dev.fredol.open-tv.desktop</launchable>
<releases>
<release version="1.6.0" date="2025-04-15">
<description>
<p>
Please see the official changelog at github.com/fredolx/open-tv/releases
</p>
</description>
</release>
<release version="1.5.2" date="2025-03-12">
<description>
<p>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "open-tv",
"version": "1.5.2",
"version": "1.6.0",
"scripts": {
"ng": "ng",
"tauri": "tauri",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "open_tv"
version = "1.5.2"
version = "1.6.0"
description = "Fast And Powerful IPTV App"
authors = ["Frédéric Lachapelle"]
license = ""
Expand Down
56 changes: 45 additions & 11 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::sync::LazyLock;
use std::sync::{
Arc, LazyLock,
atomic::{AtomicBool, Ordering},
};

use anyhow::{Context, Error};
use tauri::{
AppHandle, Manager, State,
menu::{Menu, MenuItem},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
AppHandle, Manager, State,
};
use tokio::sync::Mutex;
use types::{
AppState, Channel, CustomChannel, CustomChannelExtraData, EPGNotify, Filters, Group, IdName,
NetworkInfo, Settings, Source, EPG,
AppState, Channel, CustomChannel, CustomChannelExtraData, EPG, EPGNotify, Filters, Group,
IdName, NetworkInfo, Settings, Source,
};

pub mod epg;
Expand Down Expand Up @@ -97,7 +100,8 @@ pub fn run() {
share_restream,
add_last_watched,
backup_favs,
restore_favs
restore_favs,
abort_download
])
.setup(|app| {
app.manage(Mutex::new(AppState {
Expand Down Expand Up @@ -380,16 +384,46 @@ fn update_source(source: Source) -> Result<(), String> {

#[tauri::command]
async fn get_epg(channel: Channel) -> Result<Vec<EPG>, String> {
xtream::get_short_epg(channel)
.await
.map_err(map_err_frontend)
xtream::get_epg(channel).await.map_err(map_err_frontend)
}

#[tauri::command]
async fn download(app: AppHandle, channel: Channel) -> Result<(), String> {
utils::download(app, channel)
async fn download(
state: State<'_, Mutex<AppState>>,
app: AppHandle,
name: String,
url: String,
download_id: String,
) -> Result<(), String> {
let stop = Arc::new(AtomicBool::new(false));
let stop_clone = stop.clone();
{
let mut state = state.lock().await;
state.download_stop.insert(download_id.clone(), stop);
}
let result = utils::download(stop_clone, app, name, url, &download_id)
.await
.map_err(map_err_frontend)
.map_err(map_err_frontend);
{
let mut state = state.lock().await;
state.download_stop.remove(&download_id);
}
result
}

#[tauri::command]
async fn abort_download(
state: State<'_, Mutex<AppState>>,
download_id: String,
) -> Result<(), String> {
let mutex = state.lock().await;
let download = mutex
.download_stop
.get(&download_id)
.context("download not found")
.map_err(map_err_frontend)?;
download.store(true, Ordering::Relaxed);
Ok(())
}

#[tauri::command]
Expand Down
7 changes: 6 additions & 1 deletion src-tauri/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
sync::{atomic::AtomicBool, Arc},
collections::HashMap,
sync::{Arc, atomic::AtomicBool},
thread::JoinHandle,
};

Expand Down Expand Up @@ -131,6 +132,9 @@ pub struct EPG {
pub start_time: String,
pub start_timestamp: i64,
pub end_time: String,
pub timeshift_url: Option<String>,
pub has_archive: bool,
pub now_playing: bool,
}

#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
Expand All @@ -146,6 +150,7 @@ pub struct AppState {
pub notify_stop: Arc<AtomicBool>,
pub thread_handle: Option<JoinHandle<Result<(), anyhow::Error>>>,
pub restream_stop_signal: Arc<AtomicBool>,
pub download_stop: HashMap<String, Arc<AtomicBool>>,
}

#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
Expand Down
53 changes: 32 additions & 21 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ use crate::{
m3u,
settings::{get_default_record_path, get_settings},
source_type, sql,
types::{Channel, Source},
types::Source,
xtream,
};
use anyhow::{anyhow, bail, Context, Result};
use anyhow::{Context, Result, anyhow, bail};
use chrono::{DateTime, Local, Utc};
use regex::Regex;
use reqwest::Client;
use serde::Serialize;
use std::{
env::{consts::OS, current_exe},
io::Write,
path::Path,
sync::LazyLock,
sync::{
Arc, LazyLock,
atomic::{AtomicBool, Ordering::Relaxed},
},
};
use tauri::{AppHandle, Emitter};
use tokio::io::AsyncWriteExt;
use which::which;

const MACOS_POTENTIAL_PATHS: [&str; 3] = [
Expand Down Expand Up @@ -53,31 +56,38 @@ pub fn get_local_time(timestamp: i64) -> Result<DateTime<Local>> {
Ok(DateTime::<Local>::from(datetime))
}

pub async fn download(app: AppHandle, channel: Channel) -> Result<()> {
pub async fn download(
stop: Arc<AtomicBool>,
app: AppHandle,
name: String,
url: String,
download_id: &str,
) -> Result<()> {
let client = Client::new();
let mut response = client
.get(channel.url.as_ref().context("no url")?)
.send()
.await?;
let mut response = client.get(&url).send().await?;
let total_size = response.content_length().unwrap_or(0);
let mut downloaded = 0;
let mut file = std::fs::File::create(get_download_path(get_filename(
channel.name,
channel.url.context("no url")?,
)?)?)?;
let mut send_threshold: u8 = 5;
let file_path = get_download_path(get_filename(name, url)?)?;
let mut file = tokio::fs::File::create(&file_path).await?;
let mut send_threshold: f64 = 0.1;
if !response.status().is_success() {
let error = response.status();
bail!("Failed to download movie: HTTP {error}")
}
while let Some(chunk) = response.chunk().await? {
file.write(&chunk)?;
if stop.load(Relaxed) {
drop(file);
tokio::fs::remove_file(file_path).await?;
bail!("download aborted");
}
file.write(&chunk).await?;
downloaded += chunk.len() as u64;
if total_size > 0 {
let progress: u8 = ((downloaded as f64 / total_size as f64) * 100.0) as u8;
let progress: f64 = (downloaded as f64 / total_size as f64) * 100.0;
let progress = (progress * 10.0).trunc() / 10.0;
if progress > send_threshold {
app.emit("progress", progress)?;
send_threshold = progress + 5;
app.emit(&format!("progress-{}", download_id), progress)?;
send_threshold = progress + 0.1 as f64;
}
}
}
Expand All @@ -86,9 +96,10 @@ pub async fn download(app: AppHandle, channel: Channel) -> Result<()> {

fn get_filename(channel_name: String, url: String) -> Result<String> {
let extension = url
.split(".")
.last()
.context("url has no extension")?
.rsplit(".")
.next()
.filter(|ext| !ext.starts_with("php?"))
.unwrap_or("mp4")
.to_string();
let channel_name = sanitize(channel_name);
let filename = format!("{channel_name}.{extension}").to_string();
Expand Down
Loading
Loading