-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from URN/builder
Builder
- Loading branch information
Showing
7 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |