Skip to content

Commit

Permalink
Merge pull request #16 from URN/builder
Browse files Browse the repository at this point in the history
Builder
  • Loading branch information
Emersont1 authored Oct 20, 2021
2 parents 8ae1452 + f237b81 commit 9d010d1
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 7 deletions.
2 changes: 1 addition & 1 deletion audioboom/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def save(self, root):
path_ = join(path, ep.slug)

while exists(path_):
ep.slug += '_'
ep.slug += '-'
path_ = join(path, ep.slug)

mkdir(path_)
Expand Down
1 change: 1 addition & 0 deletions hstp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .info import *
from .episode import *
from .podcast import *
from .reader import *
35 changes: 35 additions & 0 deletions hstp/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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(i, args.input)
r.load_podcasts()
r.save(args.output)
12 changes: 6 additions & 6 deletions hstp/episode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions hstp/podcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
78 changes: 78 additions & 0 deletions hstp/reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import hstp
import hstp.utils
from dateutil.parser import parse
import os
import json
from shutil import copyfile


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"""
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",
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))
15 changes: 15 additions & 0 deletions hstp/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
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

0 comments on commit 9d010d1

Please sign in to comment.