Skip to content

Commit

Permalink
handle SIGINT
Browse files Browse the repository at this point in the history
  • Loading branch information
robotmlg committed Dec 27, 2019
1 parent afa03b5 commit 721dfe7
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions podsnatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import feedparser
import requests
import argparse
import signal
import time
import sys
import re
import os

TMP_EXT = '.part'


class Show:

Expand Down Expand Up @@ -86,10 +90,15 @@ def download(url, path, mode):
print("ERROR downloading file")


total_downloaded = 0
full_path = ''


def save_podcasts(opml, output, episode_count=None):
shows = parse_ompl(opml)
global total_downloaded
global full_path

total_downloaded = 0
shows = parse_ompl(opml)

for show in shows:
print(f'Processing show {show.title}')
Expand All @@ -115,7 +124,9 @@ def save_podcasts(opml, output, episode_count=None):

if not os.path.exists(full_path):
print('Downloading episode')
download(episode.url, full_path, 'wb')
download(episode.url, full_path + TMP_EXT, 'wb')

os.rename(full_path + TMP_EXT, full_path)

handle = open(full_path + ".txt", "w")
handle.write(str(episode))
Expand All @@ -131,6 +142,16 @@ def save_podcasts(opml, output, episode_count=None):
print(f'{total_downloaded} episodes downloaded')


def ctrl_c_handler(signum, frame):
print('Stopping...')

if os.path.exists(full_path + TMP_EXT):
os.remove(full_path + TMP_EXT)

print(f'{total_downloaded} episodes downloaded')
sys.exit(1)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download podcasts.')

Expand All @@ -144,4 +165,6 @@ def save_podcasts(opml, output, episode_count=None):
help='path to opml file to import')
args = parser.parse_args()

signal.signal(signal.SIGINT, ctrl_c_handler)

save_podcasts(args.opml_loc, args.output_loc, args.ep_cnt)

0 comments on commit 721dfe7

Please sign in to comment.