From 06ab00d491a6b342a120e832423aa2d35ebe356e Mon Sep 17 00:00:00 2001 From: Peter Taylor Date: Wed, 20 Oct 2021 14:33:39 +0100 Subject: [PATCH 1/5] initial reader code --- hstp/__init__.py | 1 + hstp/__main__.py | 29 +++++++++++++++++++++++++++++ hstp/reader.py | 15 +++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 hstp/__main__.py create mode 100644 hstp/reader.py diff --git a/hstp/__init__.py b/hstp/__init__.py index 342884a..3be1315 100644 --- a/hstp/__init__.py +++ b/hstp/__init__.py @@ -1,3 +1,4 @@ from .info import * from .episode import * from .podcast import * +from .reader import * diff --git a/hstp/__main__.py b/hstp/__main__.py new file mode 100644 index 0000000..a4adf08 --- /dev/null +++ b/hstp/__main__.py @@ -0,0 +1,29 @@ +import argparse +import os + +import hstp + + +parser = argparse.ArgumentParser( + prog='python -m hstp', + description='Build an ' +) + +parser.add_argument("-v", "--verbose", help="increase output verbosity", + action="store_true") +parser.add_argument("-i", "--input", help="input directory") +parser.add_argument("-o", "--output", help="output directory", default="../out") + +args = parser.parse_args() + +i = hstp.Info() + +if not args.input or (not os.path.isdir(args.input)): + i.error("Input directory does not exist") + exit(1) + +if not args.output or (not os.path.isdir(args.input)): + i.error("Input directory does not exist") + exit(1) + +r = hstp.Reader(args.input) \ No newline at end of file diff --git a/hstp/reader.py b/hstp/reader.py new file mode 100644 index 0000000..e378cf0 --- /dev/null +++ b/hstp/reader.py @@ -0,0 +1,15 @@ +import hstp +import os + + +class Reader: + """ Read in an input tree to a station object """ + + def __init__(self, info, input_path): + self.info = info + self.input_path = input_path + + def load_podcasts(self): + """ loads the podcasts from the input tree""" + dirs = [x for x in os.listdir(self.input_path) if os.isdir(x)] + print(dirs) From 704b0ef4d8a1ed14267b75fb5d0e4e4d945a8ffb Mon Sep 17 00:00:00 2001 From: Peter Taylor Date: Wed, 20 Oct 2021 15:13:35 +0000 Subject: [PATCH 2/5] work on reader, It appears to be loading things in --- audioboom/channel.py | 2 +- hstp/__main__.py | 3 ++- hstp/episode.py | 12 ++++++------ hstp/reader.py | 35 +++++++++++++++++++++++++++++++++-- hstp/utils.py | 12 ++++++++++++ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/audioboom/channel.py b/audioboom/channel.py index 2194f3c..885174c 100644 --- a/audioboom/channel.py +++ b/audioboom/channel.py @@ -103,7 +103,7 @@ def save(self, root): path_ = join(path, ep.slug) while exists(path_): - ep.slug += '_' + ep.slug += '-' path_ = join(path, ep.slug) mkdir(path_) diff --git a/hstp/__main__.py b/hstp/__main__.py index a4adf08..e423a9f 100644 --- a/hstp/__main__.py +++ b/hstp/__main__.py @@ -26,4 +26,5 @@ i.error("Input directory does not exist") exit(1) -r = hstp.Reader(args.input) \ No newline at end of file +r = hstp.Reader(i, args.input) +r.load_podcasts() diff --git a/hstp/episode.py b/hstp/episode.py index 49737d9..065ac96 100644 --- a/hstp/episode.py +++ b/hstp/episode.py @@ -68,20 +68,20 @@ def __init__(self, info, name, slug, description, date, file, thumb=None): if not thumb: info.warn(f"A thumbnail is highly reccommended.") - elif not isinstance(description, str): + elif not isinstance(thumb, str): info.error( f"Thumbnail is required to be a string, " - f"got {type(description)}" + f"got {type(thumb)}" ) valid = False else: - if not file.endswith(".jpg"): + if not thumb.endswith(".jpg"): info.warn( - f"Thumbnail `{file}` does not have extension jpg, " + f"Thumbnail `{thumb}` does not have extension jpg, " f"proceed with caution" ) - if not os.path.isfile(file): - info.error(f"Thumbnail `{file}` does not exist") + if not os.path.isfile(thumb): + info.error(f"Thumbnail `{thumb}` does not exist") valid = False if not valid: diff --git a/hstp/reader.py b/hstp/reader.py index e378cf0..c879d68 100644 --- a/hstp/reader.py +++ b/hstp/reader.py @@ -1,4 +1,6 @@ import hstp +import hstp.utils +from dateutil.parser import parse import os @@ -11,5 +13,34 @@ def __init__(self, info, input_path): def load_podcasts(self): """ loads the podcasts from the input tree""" - dirs = [x for x in os.listdir(self.input_path) if os.isdir(x)] - print(dirs) + pods = hstp.utils.subdirectories(self.input_path) + self.podcasts = [] + + for slug in pods: + p = None + with open(f"{self.input_path}/{slug}/description.txt") as desc: + lines = desc.read().split("\n") + title = lines[0] + d = '\n'.join(lines[1:]) + + p = hstp.Podcast(self.info, title, slug, d, f"{self.input_path}{slug}/image.jpg") + + eps = hstp.utils.subdirectories(f"{self.input_path}/{slug}") + for ep_slug in eps: + with open(f"{self.input_path}/{slug}/{ep_slug}/description.txt") as desc: + lines = desc.read().split("\n") + title = lines[0] + date = lines[1] + d = '\n'.join(lines[2:]) + + e = hstp.Episode( + self.info, + title, + ep_slug, + d, + parse(date), + f"{self.input_path}/{slug}/{ep_slug}/audio.mp3", + thumb=hstp.utils.path_or_none(f"{self.input_path}/{slug}/{ep_slug}/image.jpg") + ) + p.add_episode(e) + self.podcasts.append(p) diff --git a/hstp/utils.py b/hstp/utils.py index 203df13..9e14209 100644 --- a/hstp/utils.py +++ b/hstp/utils.py @@ -1,4 +1,16 @@ +import os def is_slug(string): """ Function to test if a URL slug is valid """ return all([s in '0123456789-abcdefghijklmnopqrstuvwxyz' for s in string]) + +def subdirectories(dir): + return [ + x + for x in os.listdir(dir) + if os.path.isdir(f"{dir}/{x}") + ] + +def path_or_none(file): + if os.path.exists(file): + return file \ No newline at end of file From aff57998e05c40158d453773adcd6cfb6a7390eb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 20 Oct 2021 16:50:44 +0100 Subject: [PATCH 3/5] autopep8 action fixes (#17) Co-authored-by: Emersont1 --- hstp/__main__.py | 6 +++++- hstp/reader.py | 5 +++-- hstp/utils.py | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/hstp/__main__.py b/hstp/__main__.py index e423a9f..f67f4ee 100644 --- a/hstp/__main__.py +++ b/hstp/__main__.py @@ -12,7 +12,11 @@ parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") parser.add_argument("-i", "--input", help="input directory") -parser.add_argument("-o", "--output", help="output directory", default="../out") +parser.add_argument( + "-o", + "--output", + help="output directory", + default="../out") args = parser.parse_args() diff --git a/hstp/reader.py b/hstp/reader.py index c879d68..d01f4b3 100644 --- a/hstp/reader.py +++ b/hstp/reader.py @@ -23,8 +23,9 @@ def load_podcasts(self): title = lines[0] d = '\n'.join(lines[1:]) - p = hstp.Podcast(self.info, title, slug, d, f"{self.input_path}{slug}/image.jpg") - + p = hstp.Podcast(self.info, title, slug, d, + f"{self.input_path}{slug}/image.jpg") + eps = hstp.utils.subdirectories(f"{self.input_path}/{slug}") for ep_slug in eps: with open(f"{self.input_path}/{slug}/{ep_slug}/description.txt") as desc: diff --git a/hstp/utils.py b/hstp/utils.py index 9e14209..60e4bdf 100644 --- a/hstp/utils.py +++ b/hstp/utils.py @@ -1,9 +1,11 @@ import os + def is_slug(string): """ Function to test if a URL slug is valid """ return all([s in '0123456789-abcdefghijklmnopqrstuvwxyz' for s in string]) + def subdirectories(dir): return [ x @@ -11,6 +13,7 @@ def subdirectories(dir): if os.path.isdir(f"{dir}/{x}") ] + def path_or_none(file): if os.path.exists(file): - return file \ No newline at end of file + return file From 6113c66f7286346b0141f7a7a3df3d2b602ddfa2 Mon Sep 17 00:00:00 2001 From: Peter Taylor Date: Wed, 20 Oct 2021 17:17:23 +0100 Subject: [PATCH 4/5] Builder is fixed - ready for 1.0 release --- hstp/__main__.py | 1 + hstp/podcast.py | 1 + hstp/reader.py | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/hstp/__main__.py b/hstp/__main__.py index f67f4ee..d294c87 100644 --- a/hstp/__main__.py +++ b/hstp/__main__.py @@ -32,3 +32,4 @@ r = hstp.Reader(i, args.input) r.load_podcasts() +r.save(args.output) \ No newline at end of file diff --git a/hstp/podcast.py b/hstp/podcast.py index 711678d..a88d228 100644 --- a/hstp/podcast.py +++ b/hstp/podcast.py @@ -88,6 +88,7 @@ def dump(self, include_episodes=True): if include_episodes: e = [e.dump() for e in self.episodes.values()] e.sort(key=lambda x: x["date"]) + e.reverse() data["episodes"] = e return data diff --git a/hstp/reader.py b/hstp/reader.py index d01f4b3..432f45e 100644 --- a/hstp/reader.py +++ b/hstp/reader.py @@ -2,6 +2,8 @@ import hstp.utils from dateutil.parser import parse import os +import json +from shutil import copyfile class Reader: @@ -24,7 +26,7 @@ def load_podcasts(self): d = '\n'.join(lines[1:]) p = hstp.Podcast(self.info, title, slug, d, - f"{self.input_path}{slug}/image.jpg") + f"{self.input_path}/{slug}/image.jpg") eps = hstp.utils.subdirectories(f"{self.input_path}/{slug}") for ep_slug in eps: @@ -41,7 +43,36 @@ def load_podcasts(self): d, parse(date), f"{self.input_path}/{slug}/{ep_slug}/audio.mp3", - thumb=hstp.utils.path_or_none(f"{self.input_path}/{slug}/{ep_slug}/image.jpg") + hstp.utils.path_or_none( + f"{self.input_path}/{slug}/{ep_slug}/image.jpg" + ) ) p.add_episode(e) self.podcasts.append(p) + + def save(self, output_path): + """ saves the output tree """ + + # hstp.json + hstp_out = dict() + hstp_out['podcasts'] = [] + for p in self.podcasts: + hstp_out['podcasts'].append(p.dump(False)) + with open(f"{output_path}/{p.slug}.json", 'w') as f: + f.write(json.dumps(p.dump(True))) + + if not os.path.exists(f"{output_path}/{p.slug}"): + os.makedirs(f"{output_path}/{p.slug}") + + copyfile(p.thumb, f"{output_path}/{p.slug}.jpg") + + for e in p.episodes.values(): + copyfile(e.file, f"{output_path}/{p.slug}/{e.slug}.mp3") + if e.thumb is not None: + copyfile(e.thumb, f"{output_path}/{p.slug}/{e.slug}.jpg") + + hstp_out['podcasts'].sort(key=lambda x: x['last-updated']) + hstp_out['podcasts'].reverse() + + with open(f"{output_path}/hstp.json", 'w') as f: + f.write(json.dumps(hstp_out)) From f237b81c52c4beecdea5fad721cb940d2d957e6f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 20 Oct 2021 17:18:03 +0100 Subject: [PATCH 5/5] autopep8 action fixes (#18) Co-authored-by: Emersont1 --- hstp/__main__.py | 2 +- hstp/reader.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hstp/__main__.py b/hstp/__main__.py index d294c87..82130b2 100644 --- a/hstp/__main__.py +++ b/hstp/__main__.py @@ -32,4 +32,4 @@ r = hstp.Reader(i, args.input) r.load_podcasts() -r.save(args.output) \ No newline at end of file +r.save(args.output) diff --git a/hstp/reader.py b/hstp/reader.py index 432f45e..e191690 100644 --- a/hstp/reader.py +++ b/hstp/reader.py @@ -60,10 +60,10 @@ def save(self, output_path): hstp_out['podcasts'].append(p.dump(False)) with open(f"{output_path}/{p.slug}.json", 'w') as f: f.write(json.dumps(p.dump(True))) - + if not os.path.exists(f"{output_path}/{p.slug}"): os.makedirs(f"{output_path}/{p.slug}") - + copyfile(p.thumb, f"{output_path}/{p.slug}.jpg") for e in p.episodes.values():