From 0d8f3b6b11917d61111efbb88e7cac1084c7f985 Mon Sep 17 00:00:00 2001
From: Magnus Walbeck <mw@mwalbeck.org>
Date: Fri, 30 Jun 2017 13:06:19 +0200
Subject: [PATCH 1/4] Add exception handling for timeout and connection errors

---
 podfox/__init__.py | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/podfox/__init__.py b/podfox/__init__.py
index ce2efa5..3450c5d 100755
--- a/podfox/__init__.py
+++ b/podfox/__init__.py
@@ -317,14 +317,39 @@ def download_multiple(feed, maxnum):
 def download_single(folder, url, filename):
     print(url)
     base = CONFIGURATION['podcast-directory']
+
+    if 'connection_timeout' in CONFIGURATION:
+        connection_timeout = CONFIGURATION['connection_timeout']
+    else:
+        connection_timeout = 10
+    
+    if 'retries' in CONFIGURATION:
+        retries = CONFIGURATION['retries']
+    else:
+        retries = 3
+    
     if filename is None:
         filename = get_original_filename(url)
+    
     print_green("{:s} downloading".format(filename))
-    r = requests.get(url.strip(), stream=True)
-    with open(os.path.join(base, folder, filename), 'wb') as f:
-        for chunk in r.iter_content(chunk_size=1024**2):
-            f.write(chunk)
-    print("done.")
+
+    for i in range(retries):
+        try:
+            r = requests.get(url.strip(), stream=True, timeout=connection_timeout)
+            with open(os.path.join(base, folder, filename), 'wb') as f:
+                for chunk in r.iter_content(chunk_size=1024**2):
+                    f.write(chunk)
+        except ConnectionError:
+            if i == retries-1:
+                print("Connection failed")
+            continue
+        except Timeout:
+            if i == retries-1:
+                print("Connection to server timed out")
+            continue
+        else:
+            print("done.")
+            break
 
 
 def available_feeds():

From 16d6bee16e49135bdc443711109456b79290d867 Mon Sep 17 00:00:00 2001
From: Magnus Walbeck <mw@mwalbeck.org>
Date: Sat, 1 Jul 2017 18:10:30 +0200
Subject: [PATCH 2/4] Exception handling for connections error and timeouts

---
 podfox/__init__.py | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/podfox/__init__.py b/podfox/__init__.py
index 3450c5d..bff6dc7 100755
--- a/podfox/__init__.py
+++ b/podfox/__init__.py
@@ -38,7 +38,7 @@ import re
 # how-to-parse-a-rfc-2822-date-time-into-a-python-datetime
 
 from email.utils import parsedate
-from time import time, mktime, gmtime, strftime
+from time import time, mktime, gmtime, strftime, sleep
 
 CONFIGURATION = {}
 
@@ -322,30 +322,35 @@ def download_single(folder, url, filename):
         connection_timeout = CONFIGURATION['connection_timeout']
     else:
         connection_timeout = 10
-    
-    if 'retries' in CONFIGURATION:
-        retries = CONFIGURATION['retries']
+
+    if 'connection_retries' in CONFIGURATION:
+        connection_retries = CONFIGURATION['connection_retries']
     else:
-        retries = 3
-    
+        connection_retries = 3
+
     if filename is None:
         filename = get_original_filename(url)
     
     print_green("{:s} downloading".format(filename))
-
-    for i in range(retries):
+    for i in range(connection_retries):
         try:
             r = requests.get(url.strip(), stream=True, timeout=connection_timeout)
             with open(os.path.join(base, folder, filename), 'wb') as f:
                 for chunk in r.iter_content(chunk_size=1024**2):
                     f.write(chunk)
-        except ConnectionError:
-            if i == retries-1:
-                print("Connection failed")
-            continue
-        except Timeout:
-            if i == retries-1:
+        except requests.Timeout:
+            if i == connection_retries-1:
                 print("Connection to server timed out")
+            else:
+                print("Connection timed out, retrying...")
+                sleep(1)
+            continue
+        except requests.ConnectionError:
+            if i == connection_retries-1:
+                print("Failed to establish connection with server")
+            else:
+                print("Connection failed, retrying...")
+                sleep(1)
             continue
         else:
             print("done.")

From c2684778258570c3b4c73e2d6355d6052920cfb2 Mon Sep 17 00:00:00 2001
From: Magnus Walbeck <mw@mwalbeck.org>
Date: Mon, 3 Jul 2017 13:33:23 +0200
Subject: [PATCH 3/4] Episodes not downloaded due to connection issue are not
 marked as downloaded

---
 podfox/__init__.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/podfox/__init__.py b/podfox/__init__.py
index bff6dc7..f674498 100755
--- a/podfox/__init__.py
+++ b/podfox/__init__.py
@@ -308,8 +308,8 @@ def download_multiple(feed, maxnum):
                                           episode["title"], episode["url"])
             else:
                 filename = generic_episode_name(feed['shortname'], episode['url'])
-            download_single(feed['shortname'], episode['url'], filename)
-            episode['downloaded'] = True
+            if download_single(feed['shortname'], episode['url'], filename) is True:
+                episode['downloaded'] = True
             maxnum -= 1
     overwrite_config(feed)
 
@@ -330,7 +330,7 @@ def download_single(folder, url, filename):
 
     if filename is None:
         filename = get_original_filename(url)
-    
+
     print_green("{:s} downloading".format(filename))
     for i in range(connection_retries):
         try:
@@ -355,6 +355,10 @@ def download_single(folder, url, filename):
         else:
             print("done.")
             break
+    else:
+        return False
+
+    return True
 
 
 def available_feeds():

From 6672a77654019e756b391c60e24030844ddf661c Mon Sep 17 00:00:00 2001
From: Magnus Walbeck <mw@mwalbeck.org>
Date: Mon, 3 Jul 2017 17:40:42 +0200
Subject: [PATCH 4/4] Remove file if download failed

---
 podfox/__init__.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/podfox/__init__.py b/podfox/__init__.py
index f674498..e9aedca 100755
--- a/podfox/__init__.py
+++ b/podfox/__init__.py
@@ -289,6 +289,10 @@ def file_exists(shortname, filename):
     return False
 
 
+def remove_file(path):
+    os.remove(path)
+
+
 def generic_episode_name(folder, url):
     filename = get_original_filename(url)
 
@@ -356,6 +360,8 @@ def download_single(folder, url, filename):
             print("done.")
             break
     else:
+        if file_exists(folder, filename):
+            remove_file(os.path.join(base, folder, filename))
         return False
 
     return True