From 92b5c86d7115c0cafc903a9daebf746d906470a7 Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Tue, 13 Aug 2019 04:00:52 -0500
Subject: [PATCH] ptgdp: Save and resolve file extensions This should make mpd
 a lot more jumpy

---
 ptgdp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/ptgdp b/ptgdp
index 59bf3dc..b252189 100755
--- a/ptgdp
+++ b/ptgdp
@@ -6,10 +6,6 @@
 # Distributed under terms of the MIT license.
 #
 
-# TODO:
-# * Provide integration with other players, like mpd
-# * Add a quick way to append to a playlist
-
 # Variables
 _name="$(basename -- "$0")"
 _tmpdir="${XDG_CACHE_HOME:-$HOME/.cache}/$_name"
@@ -48,6 +44,14 @@ notify() {
 		"$1" \
 		"$2" > /dev/null 2>&1
 }
+checkforsong() {
+	# $1: A song name to validate
+	[ -z "$1" ] && return 1
+	# Very rudimentary implementation
+	# TODO: Make this a lot more thorough, maybe ensure MIME is good
+	[ -f "$_musicdir/$1.*" ] && return 0
+	[ -f "$_musicdir/$1" ]
+}
 validatedeps() {
 	# $@: Dependencies to validate
 	for prog in "$@"; do
@@ -160,7 +164,7 @@ playlist() {
 		sanitize "$line"
 		filename="$_musicdir/$_return"
 		if [ -z "$_optdryrun" ]; then
-			if ! [ -f "$filename" ]; then
+			if ! checkforsong "$_return"; then
 				log "Finding a song for \"$line\""
 				youtube-dl \
 					--add-metadata \
@@ -173,7 +177,8 @@ playlist() {
 					> /dev/null 2>&1 &
 				if wait $!; then
 					dlsuccess+=1
-					mv "$_tmpfile"* "$filename"
+					local extension="${_tmpfile##*.}"
+					mv "$_tmpfile"* "$filename.$extension"
 				else
 					dlfailure+=1
 					notify "Could not download song" \