summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--patternutils/commands/prss.py91
-rw-r--r--setup.py2
2 files changed, 93 insertions, 0 deletions
diff --git a/patternutils/commands/prss.py b/patternutils/commands/prss.py
new file mode 100644
index 0000000..814672e
--- /dev/null
+++ b/patternutils/commands/prss.py
@@ -0,0 +1,91 @@
+import os
+import os.path
+from typing import Generator
+
+import click
+import feedparser
+
+from .. import utils
+
+
+def walk(dir: str) -> Generator[os.DirEntry, None, None]:
+ for entry in os.scandir(dir):
+ if entry.is_file():
+ yield entry
+ else:
+ continue
+
+
+@click.command(context_settings={"help_option_names": ("-h", "--help", "-?")})
+@click.option("-d", "--directory", default="./")
+@click.option(
+ "--stream",
+ is_flag=True,
+ default=False,
+ help="Output a stream of objects instead a list of objects",
+)
+@click.option(
+ "--human-readable",
+ is_flag=True,
+ default=False,
+ help="output the JSON in a human readable format",
+)
+@click.option(
+ "-v", "--verbose", is_flag=True, default=False, help="explain what is being done"
+)
+@click.version_option()
+@click.argument("url")
+def prss(
+ url: str, directory: str, stream: bool, human_readable: bool, verbose: bool
+) -> None:
+
+ parsed_feed = feedparser.parse(url)
+
+ default_values = {
+ "feed_title": parsed_feed.feed.title,
+ "feed_link": parsed_feed.feed.link,
+ "feed_description": parsed_feed.feed.description,
+ }
+
+ files = [f for f in walk(directory)]
+
+ matches = []
+
+ for entry in parsed_feed.entries:
+
+ href = entry.enclosures[0]["href"]
+
+ splitted_href = href.split("/")
+
+ file_to_search = splitted_href[-1]
+
+ try:
+ file_index = files.index(file_to_search)
+ except ValueError:
+ click.secho(f"{file_to_search} not found!", fg="red", err=True)
+ else:
+ filename = file_to_search[file_index]
+ entry_values = {
+ "title": entry.title,
+ "link": entry.link,
+ "description": entry.description,
+ "id": entry.id,
+ "enclosure_href": entry.enclosures[0]["href"],
+ "_path": filename.path,
+ "_name": filename.name,
+ "_abspath": os.path.abspath(f.path),
+ "_realpath": os.path.realpath(f.path),
+ "_relpath": os.path.relpath(f.path),
+ }
+ entry_values.update(default_values)
+ matches.append(entry_values)
+
+ if stream:
+ for m in matches:
+ click.echo(utils.json_dumps(m, human_readable))
+ else:
+ click.echo(utils.json_dumps(matches, human_readable))
+
+
+if __name__ == "__main__":
+ prss()
diff --git a/setup.py b/setup.py
index 95a892f..1cb4252 100644
--- a/setup.py
+++ b/setup.py
@@ -17,6 +17,7 @@ ENTRY_POINTS = {
"tmv=patternutils.commands.tmv:tmv",
"tln=patternutils.commands.tln:tln",
"patterncfg=patternutils.commands.patterncfg:patterncfg",
+ "prss=patternutils.commands.prss:prss",
],
}
@@ -28,6 +29,7 @@ REQUIRED = [
OPTIONAL = {
"jinja2": ["Jinja2==2.10.1"],
"pygments": ["Pygments==2.4.2"],
+ "prss": ["feedparser==6.0.2"],
}
setup(