@ -1,5 +1,6 @@
use sqlx ::sqlite ::SqliteConnectOptions ;
use sqlx ::sqlite ::SqlitePool ;
use sqlx ::Row ;
use std ::{ env , str ::FromStr } ;
use tokio ::process ::Command ;
use walkdir ::DirEntry ;
@ -53,12 +54,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut mpvctl = MpvCtl ::connect ( "/tmp/recordwatchsocket" ) . await ? ;
let mut end_file_listener = mpvctl . listen ( & [ "end-file" ] ) ? ;
let client_name = mpvctl . get_client_name ( ) . await ? ;
for f in files {
let already_watched_count = sqlx ::query ( r#"SELECT 1 FROM watched WHERE path = ? AND profile = (SELECT id FROM PROFILE WHERE name = ?)"#)
let already_watched_count : i64 = sqlx ::query ( r#"SELECT count(*) FROM watched WHERE path = ? AND profile = (SELECT id FROM PROFILE WHERE name = ?)"#)
. bind ( f . path ( ) . as_os_str ( ) . to_str ( ) )
. bind ( & profile )
. execute ( & pool ) . await ? . rows_affected ( ) ;
. fetch_one ( & pool ) . await ? . try_get ( "count(*)" ) ? ;
println! ( "Already watched count: {}" , already_watched_count ) ;
if already_watched_count > 0 {
println! ( "Skipping: Already watched {}" , f . path ( ) . display ( ) ) ;
@ -74,12 +75,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some ( serde_json ::Value ::String ( reason_body ) ) = reason {
if reason_body = = "eof" {
// Watched the video until the end
let insert = sqlx ::query ( r#"INSERT INTO watched (profile, path, watched_at) SELECT (SELECT id FROM PROFILE WHERE name = ?) profile, ? path, DATETIME('now') watched_at"# )
let insert : u64 = sqlx ::query ( r#"INSERT INTO watched (profile, path, watched_at) SELECT (SELECT id FROM PROFILE WHERE name = ?) profile, ? path, DATETIME('now') watched_at"# )
. bind ( & profile )
. bind ( f . path ( ) . as_os_str ( ) . to_str ( ) )
. execute ( & pool ) . await ? ;
. execute ( & pool ) . await ? .rows_affected ( ) ;
println! ( "Insert: {:?}" , insert ) ;
assert! ( insert = = 1 ) ;
continue ;
}
}
@ -88,8 +89,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
break ;
}
println! ( "done {}" , client_name ) ;
Ok ( ( ) )
}